Remove logic for storage of attribute terms for variations

A mechanism for improved filtering by attribute for variations was
introduced some time ago. This mechanism implied maintaining term
relationships for variations, where the terms were the attributes
that defined the variation.

The mechanism was reverted because it was complex and presented many
issues, but the code that created those term relationships was kept.
This pull request removes that code and the associated unit tests.
This commit is contained in:
Nestor Soriano 2020-09-22 16:26:43 +02:00
parent 0d42a0419e
commit a94ea7f51f
6 changed files with 0 additions and 199 deletions

View File

@ -598,7 +598,6 @@ class WC_Product_Variation extends WC_Product_Simple {
return false;
}
wp_delete_object_term_relationships( $variation_id, wc_get_attribute_taxonomy_names() );
return true;
}
}

View File

@ -661,7 +661,6 @@ class WC_Product_Variable_Data_Store_CPT extends WC_Product_Data_Store_CPT imple
if ( $force_delete ) {
do_action( 'woocommerce_before_delete_product_variation', $variation_id );
wp_delete_post( $variation_id, true );
wp_delete_object_term_relationships( $variation_id, wc_get_attribute_taxonomy_names() );
do_action( 'woocommerce_delete_product_variation', $variation_id );
} else {
wp_trash_post( $variation_id );

View File

@ -495,21 +495,6 @@ class WC_Product_Variation_Data_Store_CPT extends WC_Product_Data_Store_CPT impl
foreach ( $delete_attribute_keys as $key ) {
delete_post_meta( $product_id, $key );
}
// Set the attributes as regular taxonomy terms too...
$variation_attributes = array_keys( $product->get_variation_attributes( false ) );
foreach ( $attributes as $name => $value ) {
$value = strval( $value );
if ( '' !== $value && in_array( $name, $variation_attributes, true ) && term_exists( $value, $name ) ) {
wp_set_post_terms( $product_id, array( $value ), $name );
} elseif ( taxonomy_exists( $name ) ) {
wp_delete_object_term_relationships( $product_id, $name );
}
}
// ...and remove old taxonomy terms.
$attributes_to_delete = array_diff( wc_get_attribute_taxonomy_names(), array_keys( $attributes ) );
wp_delete_object_term_relationships( $product_id, $attributes_to_delete );
}
}

View File

@ -114,20 +114,4 @@ class WC_Tests_Product_Variation extends WC_Unit_Test_Case {
$actual = $sut->get_variation_attributes( $with_prefix );
$this->assertEquals( $expected, $actual );
}
/**
* @testdox Test that the delete method removes the attribute terms for the variation.
*/
public function test_delete_removes_attribute_terms() {
$product = WC_Helper_Product::create_variation_product();
$sut = wc_get_product( $product->get_children()[2] );
$id = $sut->get_id();
$sut->delete( true );
$attribute_names = wc_get_attribute_taxonomy_names();
$variation_attribute_terms = wp_get_post_terms( $id, $attribute_names );
$this->assertEmpty( $variation_attribute_terms );
}
}

View File

@ -1,29 +0,0 @@
<?php
/**
* Data Store Tests for variable products: WC_Product_Variable_Data_Store.
*
* @package WooCommerce\Tests\Product
*/
/**
* Class WC_Tests_Product_Variable_Data_Store
*/
class WC_Tests_Product_Variable_Data_Store extends WC_Unit_Test_Case {
/**
* @testdox Test that "delete" on a variation removes the associated attribute terms too.
*/
public function test_attribute_terms_are_deleted_for_deleted_variations() {
$product = WC_Helper_Product::create_variation_product();
$variation = wc_get_product( $product->get_children()[2] );
$variation_id = $variation->get_id();
$sut = new WC_Product_Variable_Data_Store_CPT();
$sut->delete_variations( $product->get_id(), true );
$attribute_names = wc_get_attribute_taxonomy_names();
$variation_attribute_terms = wp_get_post_terms( $variation_id, $attribute_names );
$this->assertEmpty( $variation_attribute_terms );
}
}

View File

@ -1,137 +0,0 @@
<?php
/**
* Data Store Tests for product variations: WC_Product_Variation_Data_Store.
*
* @package WooCommerce\Tests\Product
*/
/**
* Class WC_Tests_Product_Variation_Data_Store
*/
class WC_Tests_Product_Variation_Data_Store extends WC_Unit_Test_Case {
/**
* Create and save a variable product with size and category attributes, then create a corresponding
* variation object with size "small" and color "red", and return it without saving it to database.
*
* @return WC_Product_Variation The created variation object.
*/
private function create_variation_object_for_existing_variable_product() {
$attr_size = WC_Helper_Product::create_product_attribute_object( 'size', array( 'small', 'large' ) );
$attr_color = WC_Helper_Product::create_product_attribute_object( 'color', array( 'red', 'blue' ) );
$product = new WC_Product_Variable();
$product->set_attributes( array( $attr_size, $attr_color ) );
$product->save();
$variation = WC_Helper_Product::create_product_variation_object(
$product->get_id(),
'SMALL RED THING',
10,
array(
'pa_size' => 'small',
'pa_color' => 'red',
)
);
return $variation;
}
/**
* Return a simplified list with the attribute terms for a variation object.
*
* @param int $variation_id Id of the variation product.
*
* @return array Attributes as an "attribute"=>"term" associative array.
*/
private function get_attribute_terms_for_variation( $variation_id ) {
$attribute_names = wc_get_attribute_taxonomy_names();
$variation_attribute_terms = wp_get_post_terms( $variation_id, $attribute_names );
$terms = array();
foreach ( $variation_attribute_terms as $term ) {
$terms[ $term->taxonomy ] = $term->name;
}
return $terms;
}
/**
* @testdox Test that attribute terms are created for new variations.
*/
public function test_attribute_terms_are_created_for_new_variations() {
$variation = $this->create_variation_object_for_existing_variable_product();
$sut = new WC_Product_Variation_Data_Store_CPT();
$sut->create( $variation );
$terms = $this->get_attribute_terms_for_variation( $variation->get_id() );
$expected = array(
'pa_size' => 'small',
'pa_color' => 'red',
);
$this->assertEquals( $expected, $terms );
$variation->set_attributes(
array(
'pa_size' => 'large',
'pa_color' => 'blue',
)
);
$sut->update( $variation );
$terms = $this->get_attribute_terms_for_variation( $variation->get_id() );
$expected = array(
'pa_size' => 'large',
'pa_color' => 'blue',
);
$this->assertEquals( $expected, $terms );
}
/**
* @testdox Test that attribute terms are updated for updated variations.
*/
public function test_attribute_terms_are_updated_for_modified_variations() {
$variation = $this->create_variation_object_for_existing_variable_product();
$sut = new WC_Product_Variation_Data_Store_CPT();
$sut->create( $variation );
$new_attributes = array(
'pa_size' => 'small',
'pa_color' => 'red',
);
$variation->set_attributes( $new_attributes );
$sut->update( $variation );
$terms = $this->get_attribute_terms_for_variation( $variation->get_id() );
$this->assertEquals( $new_attributes, $terms );
}
/**
* @testdox Test that attribute terms are removed for variations updated with "Any" value.
*/
public function test_attribute_terms_are_removed_for_variations_set_to_any_attribute_value() {
$variation = $this->create_variation_object_for_existing_variable_product();
$sut = new WC_Product_Variation_Data_Store_CPT();
$sut->create( $variation );
$new_attributes = array(
'pa_size' => 'small',
'pa_color' => '',
);
$variation->set_attributes( $new_attributes );
$sut->update( $variation );
$terms = $this->get_attribute_terms_for_variation( $variation->get_id() );
$expected = array( 'pa_size' => 'small' );
$this->assertEquals( $expected, $terms );
}
}