Commit f075cdc444d for woocommerce
commit f075cdc444d73a11bc97b068f08bb2f31d192e94
Author: Anand Rajaram <anandrajaram21@gmail.com>
Date: Wed Jul 1 21:17:15 2026 +0530
Preserve zero variation attributes in cart items (#65725)
* Preserve zero variation attributes in cart items
- Filter default variation attributes without dropping the string `0`
- Add coverage for adding a variation directly by ID
* Add changefile(s) from automation for the following project(s): woocommerce
---------
Co-authored-by: woocommercebot <woocommercebot@users.noreply.github.com>
diff --git a/plugins/woocommerce/changelog/65725-56586-variation-cart b/plugins/woocommerce/changelog/65725-56586-variation-cart
new file mode 100644
index 00000000000..8bdff5b8f40
--- /dev/null
+++ b/plugins/woocommerce/changelog/65725-56586-variation-cart
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fixes 0 value attributes from being skipped when variation is added to cart directly
\ No newline at end of file
diff --git a/plugins/woocommerce/includes/class-wc-cart.php b/plugins/woocommerce/includes/class-wc-cart.php
index 84b14cbdc09..2a01387a2c5 100644
--- a/plugins/woocommerce/includes/class-wc-cart.php
+++ b/plugins/woocommerce/includes/class-wc-cart.php
@@ -1176,7 +1176,7 @@ class WC_Cart extends WC_Legacy_Cart {
$variation_attributes = $product_data->get_variation_attributes();
// Filter out 'any' variations, which are empty, as they need to be explicitly specified while adding to cart.
- $variation_attributes = array_filter( $variation_attributes );
+ $variation_attributes = array_filter( $variation_attributes, 'wc_array_filter_default_attributes' );
// Gather posted attributes.
$posted_attributes = array();
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php b/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php
index d0744ab13e6..90543dbe637 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-cart-test.php
@@ -217,6 +217,47 @@ class WC_Cart_Test extends \WC_Unit_Test_Case {
$product->delete( true );
}
+ /**
+ * @testdox Should preserve zero variation attributes when adding a variation directly by ID.
+ */
+ public function test_add_variation_to_the_cart_directly_by_id_preserves_zero_attributes(): void {
+ $product = new WC_Product_Variable();
+ $product->set_name( 'Variable product with zero attribute' );
+
+ $attribute = new WC_Product_Attribute();
+ $attribute->set_id( 0 );
+ $attribute->set_name( 'length' );
+ $attribute->set_options( array( '0', '1' ) );
+ $attribute->set_visible( true );
+ $attribute->set_variation( true );
+
+ $product->set_attributes( array( $attribute ) );
+ $product->save();
+
+ $variation = new WC_Product_Variation();
+ $variation->set_parent_id( $product->get_id() );
+ $variation->set_attributes( array( 'length' => '0' ) );
+ $variation->set_regular_price( '10' );
+ $variation->save();
+
+ $cart_item_key = WC()->cart->add_to_cart( $variation->get_id(), 1 );
+
+ $this->assertNotFalse( $cart_item_key, 'The variation should be added to the cart.' );
+
+ $cart_item = WC()->cart->get_cart_item( (string) $cart_item_key );
+
+ $this->assertSame( $product->get_id(), $cart_item['product_id'], 'The cart item should use the parent product ID.' );
+ $this->assertSame( $variation->get_id(), $cart_item['variation_id'], 'The cart item should use the variation ID.' );
+ $this->assertSame(
+ array( 'attribute_length' => '0' ),
+ $cart_item['variation'],
+ 'The zero variation attribute should be preserved in cart item data.'
+ );
+
+ $variation->delete( true );
+ $product->delete( true );
+ }
+
/**
* @testdox should throw a notice to the cart if using variation_id
* that doesn't belong to specified variable product.