Merge pull request #24750 from woocommerce/fix/24667

Handle 0 attribute value for variations correctly.
This commit is contained in:
Vedanshu Jain 2019-10-02 21:28:23 +05:30 committed by GitHub
commit c671e89769
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 84 additions and 9 deletions

View File

@ -1112,7 +1112,7 @@ class WC_Product_Data_Store_CPT extends WC_Data_Store_WP implements WC_Object_Da
// Loop over the variation meta keys and values i.e. what is saved to the products. Note: $attribute_value is empty when 'any' is in use. // Loop over the variation meta keys and values i.e. what is saved to the products. Note: $attribute_value is empty when 'any' is in use.
foreach ( $variation as $attribute_key => $attribute_value ) { foreach ( $variation as $attribute_key => $attribute_value ) {
$match_any_value = empty( $attribute_value ); $match_any_value = '' === $attribute_value;
if ( ! $match_any_value && ! array_key_exists( $attribute_key, $match_attributes ) ) { if ( ! $match_any_value && ! array_key_exists( $attribute_key, $match_attributes ) ) {
$match = false; // Requires a selection but no value was provide. $match = false; // Requires a selection but no value was provide.

View File

@ -119,7 +119,7 @@ class WC_Helper_Product {
$attributes = array(); $attributes = array();
$attribute = new WC_Product_Attribute(); $attribute = new WC_Product_Attribute();
$attribute_data = self::create_attribute( 'size', array( 'small', 'large' ) ); $attribute_data = self::create_attribute( 'size', array( 'small', 'large', 'huge' ) );
$attribute->set_id( $attribute_data['attribute_id'] ); $attribute->set_id( $attribute_data['attribute_id'] );
$attribute->set_name( $attribute_data['attribute_taxonomy'] ); $attribute->set_name( $attribute_data['attribute_taxonomy'] );
$attribute->set_options( $attribute_data['term_ids'] ); $attribute->set_options( $attribute_data['term_ids'] );
@ -138,6 +138,16 @@ class WC_Helper_Product {
$attribute->set_variation( true ); $attribute->set_variation( true );
$attributes[] = $attribute; $attributes[] = $attribute;
$attribute = new WC_Product_Attribute();
$attribute_data = self::create_attribute( 'number', array( '0', '1', '2' ) );
$attribute->set_id( $attribute_data['attribute_id'] );
$attribute->set_name( $attribute_data['attribute_taxonomy'] );
$attribute->set_options( $attribute_data['term_ids'] );
$attribute->set_position( 1 );
$attribute->set_visible( true );
$attribute->set_variation( true );
$attributes[] = $attribute;
$product->set_attributes( $attributes ); $product->set_attributes( $attributes );
$product->save(); $product->save();
@ -163,6 +173,40 @@ class WC_Helper_Product {
$variation_2->set_attributes( array( 'pa_size' => 'large' ) ); $variation_2->set_attributes( array( 'pa_size' => 'large' ) );
$variation_2->save(); $variation_2->save();
$variation_3 = new WC_Product_Variation();
$variation_3->set_props(
array(
'parent_id' => $product->get_id(),
'sku' => 'DUMMY SKU VARIABLE HUGE RED 0',
'regular_price' => 16,
)
);
$variation_3->set_attributes(
array(
'pa_size' => 'huge',
'pa_colour' => 'red',
'pa_number' => '0',
)
);
$variation_3->save();
$variation_4 = new WC_Product_Variation();
$variation_4->set_props(
array(
'parent_id' => $product->get_id(),
'sku' => 'DUMMY SKU VARIABLE HUGE RED 2',
'regular_price' => 17,
)
);
$variation_4->set_attributes(
array(
'pa_size' => 'huge',
'pa_colour' => 'red',
'pa_number' => '2',
)
);
$variation_4->save();
return wc_get_product( $product->get_id() ); return wc_get_product( $product->get_id() );
} }

View File

@ -216,22 +216,29 @@ class WC_Tests_Product_Data_Store extends WC_Unit_Test_Case {
$product = new WC_Product_Variable( $product->get_id() ); $product = new WC_Product_Variable( $product->get_id() );
$this->assertEquals( 2, count( $product->get_children() ) ); $this->assertEquals( 4, count( $product->get_children() ) );
$expected_prices['price'][ $children[0] ] = 8.00; $expected_prices['price'][ $children[0] ] = 8.00;
$expected_prices['price'][ $children[1] ] = 15.00; $expected_prices['price'][ $children[1] ] = 15.00;
$expected_prices['price'][ $children[2] ] = 16.00;
$expected_prices['price'][ $children[3] ] = 17.00;
$expected_prices['regular_price'][ $children[0] ] = 10.00; $expected_prices['regular_price'][ $children[0] ] = 10.00;
$expected_prices['regular_price'][ $children[1] ] = 15.00; $expected_prices['regular_price'][ $children[1] ] = 15.00;
$expected_prices['regular_price'][ $children[2] ] = 16.00;
$expected_prices['regular_price'][ $children[3] ] = 17.00;
$expected_prices['sale_price'][ $children[0] ] = 8.00; $expected_prices['sale_price'][ $children[0] ] = 8.00;
$expected_prices['sale_price'][ $children[1] ] = 15.00; $expected_prices['sale_price'][ $children[1] ] = 15.00;
$expected_prices['sale_price'][ $children[2] ] = 16.00;
$expected_prices['sale_price'][ $children[3] ] = 17.00;
$this->assertEquals( $expected_prices, $product->get_variation_prices() ); $this->assertEquals( $expected_prices, $product->get_variation_prices() );
$expected_attributes = array( $expected_attributes = array(
'pa_size' => array( 'small', 'large' ), 'pa_size' => array( 'small', 'large', 'huge' ),
'pa_colour' => array( 'blue', 'red' ), 'pa_colour' => array( 'red' ),
'pa_number' => array( '0', '2' ),
); );
$this->assertEquals( $expected_attributes, $product->get_variation_attributes() ); $this->assertEquals( $expected_attributes, $product->get_variation_attributes() );
} }
@ -1035,5 +1042,29 @@ class WC_Tests_Product_Data_Store extends WC_Unit_Test_Case {
array() array()
); );
$this->assertEquals( 0, $match ); $this->assertEquals( 0, $match );
// Test numeric attribute values.
$match = $data_store->find_matching_product_variation(
$product,
array(
'attribute_pa_size' => 'huge',
'attribute_pa_colour' => 'red',
'attribute_pa_number' => '2',
)
);
$this->assertEquals( $children[3], $match );
// Test numeric attribute value 0.
$match = $data_store->find_matching_product_variation(
$product,
array(
'attribute_pa_size' => 'huge',
'attribute_pa_colour' => 'red',
'attribute_pa_number' => '0',
)
);
$this->assertEquals( $children[2], $match );
} }
} }

View File

@ -70,7 +70,7 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case {
'type' => 'variation', 'type' => 'variation',
) )
); );
$this->assertCount( 2, $products ); $this->assertCount( 4, $products );
// Test parent. // Test parent.
$products = wc_get_products( $products = wc_get_products(
@ -80,7 +80,7 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case {
'parent' => $variation->get_id(), 'parent' => $variation->get_id(),
) )
); );
$this->assertCount( 2, $products ); $this->assertCount( 4, $products );
// Test parent_exclude. // Test parent_exclude.
$products = wc_get_products( $products = wc_get_products(

View File

@ -87,7 +87,7 @@ class WC_Tests_Template_Functions extends WC_Unit_Test_Case {
public function test_wc_dropdown_variation_attribute_options_should_return_attributes_list() { public function test_wc_dropdown_variation_attribute_options_should_return_attributes_list() {
$product = WC_Helper_Product::create_variation_product(); $product = WC_Helper_Product::create_variation_product();
$this->expectOutputString( '<select id="pa_size" class="" name="attribute_pa_size" data-attribute_name="attribute_pa_size" data-show_option_none="yes"><option value="">Choose an option</option><option value="large" >large</option><option value="small" >small</option></select>' ); $this->expectOutputString( '<select id="pa_size" class="" name="attribute_pa_size" data-attribute_name="attribute_pa_size" data-show_option_none="yes"><option value="">Choose an option</option><option value="huge" >huge</option><option value="large" >large</option><option value="small" >small</option></select>' );
wc_dropdown_variation_attribute_options( wc_dropdown_variation_attribute_options(
array( array(
@ -104,7 +104,7 @@ class WC_Tests_Template_Functions extends WC_Unit_Test_Case {
$product = WC_Helper_Product::create_variation_product(); $product = WC_Helper_Product::create_variation_product();
$_REQUEST['attribute_pa_size'] = 'large'; $_REQUEST['attribute_pa_size'] = 'large';
$this->expectOutputString( '<select id="pa_size" class="" name="attribute_pa_size" data-attribute_name="attribute_pa_size" data-show_option_none="yes"><option value="">Choose an option</option><option value="large" selected=\'selected\'>large</option><option value="small" >small</option></select>' ); $this->expectOutputString( '<select id="pa_size" class="" name="attribute_pa_size" data-attribute_name="attribute_pa_size" data-show_option_none="yes"><option value="">Choose an option</option><option value="huge" >huge</option><option value="large" selected=\'selected\'>large</option><option value="small" >small</option></select>' );
wc_dropdown_variation_attribute_options( wc_dropdown_variation_attribute_options(
array( array(