Commit c30d53d4832 for woocommerce
commit c30d53d483259c191ee87ce83f607a0c69f57d85
Author: Vlad Olaru <vlad.olaru@automattic.com>
Date: Fri Nov 20 18:47:22 2026 +0200
Fix saves for sold-out variable products with invalid stock options (#71153)
* fix(products): Preserve stock status when an extension returns invalid options
A malformed stock options filter can abort a variable-product save.
Ignoring the invalid result only in the data store would still let the
stock setter mark a sold-out parent as in stock.
Fall back to the built-in options at the getter so both consumers can
use them. Honor every array returned by an extension, including an
empty one. Cover the getter contract and the persisted product edit.
Refs WOOAIRR-807
* chore(products): Add changelog for the stock status fallback
Document that merchants can save a sold-out variable product when an
extension supplies invalid stock status options.
Refs WOOAIRR-807
* Apply suggestion from @oaratovskyi
Co-authored-by: Oleksandr Aratovskyi <79862886+oaratovskyi@users.noreply.github.com>
---------
Co-authored-by: Oleksandr Aratovskyi <79862886+oaratovskyi@users.noreply.github.com>
diff --git a/plugins/woocommerce/changelog/fix-WOOAIRR-807-stock-status-options b/plugins/woocommerce/changelog/fix-WOOAIRR-807-stock-status-options
new file mode 100644
index 00000000000..9c154580d70
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-WOOAIRR-807-stock-status-options
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Prevent product REST API errors and incorrect in-stock statuses when an extension returns invalid stock status options.
diff --git a/plugins/woocommerce/includes/wc-product-functions.php b/plugins/woocommerce/includes/wc-product-functions.php
index 6c4dbd6b90a..7cfbda8c3c5 100644
--- a/plugins/woocommerce/includes/wc-product-functions.php
+++ b/plugins/woocommerce/includes/wc-product-functions.php
@@ -1401,14 +1401,22 @@ function wc_get_product_tax_class_options() {
* @return array
*/
function wc_get_product_stock_status_options() {
- return apply_filters(
- 'woocommerce_product_stock_status_options',
- array(
- ProductStockStatus::IN_STOCK => __( 'In stock', 'woocommerce' ),
- ProductStockStatus::OUT_OF_STOCK => __( 'Out of stock', 'woocommerce' ),
- ProductStockStatus::ON_BACKORDER => __( 'On backorder', 'woocommerce' ),
- )
+ $defaults = array(
+ ProductStockStatus::IN_STOCK => __( 'In stock', 'woocommerce' ),
+ ProductStockStatus::OUT_OF_STOCK => __( 'Out of stock', 'woocommerce' ),
+ ProductStockStatus::ON_BACKORDER => __( 'On backorder', 'woocommerce' ),
);
+
+ /**
+ * Filters the available product stock statuses.
+ *
+ * @since 3.6.0
+ *
+ * @param array $defaults Stock status labels keyed by status.
+ */
+ $options = apply_filters( 'woocommerce_product_stock_status_options', $defaults );
+
+ return is_array( $options ) ? $options : $defaults;
}
/**
diff --git a/plugins/woocommerce/tests/legacy/unit-tests/product/functions.php b/plugins/woocommerce/tests/legacy/unit-tests/product/functions.php
index 67d9eb9c706..40ea9a3649b 100644
--- a/plugins/woocommerce/tests/legacy/unit-tests/product/functions.php
+++ b/plugins/woocommerce/tests/legacy/unit-tests/product/functions.php
@@ -1264,16 +1264,19 @@ class WC_Tests_Product_Functions extends WC_Unit_Test_Case {
* @since 3.6.0
*/
public function test_wc_get_product_stock_status_options() {
- $status_options = (array) apply_filters(
- 'woocommerce_product_stock_status_options',
- array(
- ProductStockStatus::IN_STOCK => 'In stock',
- ProductStockStatus::OUT_OF_STOCK => 'Out of stock',
- ProductStockStatus::ON_BACKORDER => 'On backorder',
- )
+ $status_options = array(
+ ProductStockStatus::IN_STOCK => 'In stock',
+ ProductStockStatus::OUT_OF_STOCK => 'Out of stock',
+ ProductStockStatus::ON_BACKORDER => 'On backorder',
);
+ $this->assertSame( $status_options, wc_get_product_stock_status_options() );
+
+ add_filter( 'woocommerce_product_stock_status_options', '__return_null' );
+ $this->assertSame( $status_options, wc_get_product_stock_status_options() );
+ remove_filter( 'woocommerce_product_stock_status_options', '__return_null' );
- $this->assertEquals( $status_options, wc_get_product_stock_status_options() );
+ add_filter( 'woocommerce_product_stock_status_options', '__return_empty_array' );
+ $this->assertSame( array(), wc_get_product_stock_status_options() );
}
/**
diff --git a/plugins/woocommerce/tests/php/includes/data-stores/class-wc-product-variable-data-store-cpt-test.php b/plugins/woocommerce/tests/php/includes/data-stores/class-wc-product-variable-data-store-cpt-test.php
index 8282e661239..7cb5dd1e0d2 100644
--- a/plugins/woocommerce/tests/php/includes/data-stores/class-wc-product-variable-data-store-cpt-test.php
+++ b/plugins/woocommerce/tests/php/includes/data-stores/class-wc-product-variable-data-store-cpt-test.php
@@ -2316,6 +2316,25 @@ class WC_Product_Variable_Data_Store_CPT_Test extends WC_Unit_Test_Case {
$product->delete();
}
+ /**
+ * @testdox Saving a sold-out variable product preserves its stock status when the stock options filter returns null.
+ */
+ public function test_save_with_invalid_stock_status_options(): void {
+ $product = $this->create_variable_product_with_child_stock_statuses(
+ array( ProductStockStatus::OUT_OF_STOCK, ProductStockStatus::OUT_OF_STOCK )
+ );
+ $product->set_name( 'Renamed sold-out product' );
+ $product->set_stock_status( ProductStockStatus::IN_STOCK );
+
+ add_filter( 'woocommerce_product_stock_status_options', '__return_null' );
+ $product->save();
+ remove_filter( 'woocommerce_product_stock_status_options', '__return_null' );
+
+ $saved = new WC_Product_Variable( $product->get_id() );
+ $this->assertSame( 'Renamed sold-out product', $saved->get_name() );
+ $this->assertSame( ProductStockStatus::OUT_OF_STOCK, $saved->get_stock_status() );
+ }
+
/**
* @testdox sync_stock_status sets outofstock when the product has no children.
*/