Merge pull request #24750 from woocommerce/fix/24667
Handle 0 attribute value for variations correctly.
This commit is contained in:
commit
c671e89769
|
@ -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.
|
||||
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 ) ) {
|
||||
$match = false; // Requires a selection but no value was provide.
|
||||
|
|
|
@ -119,7 +119,7 @@ class WC_Helper_Product {
|
|||
$attributes = array();
|
||||
|
||||
$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_name( $attribute_data['attribute_taxonomy'] );
|
||||
$attribute->set_options( $attribute_data['term_ids'] );
|
||||
|
@ -138,6 +138,16 @@ class WC_Helper_Product {
|
|||
$attribute->set_variation( true );
|
||||
$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->save();
|
||||
|
||||
|
@ -163,6 +173,40 @@ class WC_Helper_Product {
|
|||
$variation_2->set_attributes( array( 'pa_size' => 'large' ) );
|
||||
$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() );
|
||||
}
|
||||
|
||||
|
|
|
@ -216,22 +216,29 @@ class WC_Tests_Product_Data_Store extends WC_Unit_Test_Case {
|
|||
|
||||
$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[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[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[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() );
|
||||
|
||||
$expected_attributes = array(
|
||||
'pa_size' => array( 'small', 'large' ),
|
||||
'pa_colour' => array( 'blue', 'red' ),
|
||||
'pa_size' => array( 'small', 'large', 'huge' ),
|
||||
'pa_colour' => array( 'red' ),
|
||||
'pa_number' => array( '0', '2' ),
|
||||
);
|
||||
$this->assertEquals( $expected_attributes, $product->get_variation_attributes() );
|
||||
}
|
||||
|
@ -1035,5 +1042,29 @@ class WC_Tests_Product_Data_Store extends WC_Unit_Test_Case {
|
|||
array()
|
||||
);
|
||||
$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 );
|
||||
}
|
||||
}
|
||||
|
|
|
@ -70,7 +70,7 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case {
|
|||
'type' => 'variation',
|
||||
)
|
||||
);
|
||||
$this->assertCount( 2, $products );
|
||||
$this->assertCount( 4, $products );
|
||||
|
||||
// Test parent.
|
||||
$products = wc_get_products(
|
||||
|
@ -80,7 +80,7 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case {
|
|||
'parent' => $variation->get_id(),
|
||||
)
|
||||
);
|
||||
$this->assertCount( 2, $products );
|
||||
$this->assertCount( 4, $products );
|
||||
|
||||
// Test parent_exclude.
|
||||
$products = wc_get_products(
|
||||
|
|
|
@ -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() {
|
||||
$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(
|
||||
array(
|
||||
|
@ -104,7 +104,7 @@ class WC_Tests_Template_Functions extends WC_Unit_Test_Case {
|
|||
$product = WC_Helper_Product::create_variation_product();
|
||||
$_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(
|
||||
array(
|
||||
|
|
Loading…
Reference in New Issue