diff --git a/plugins/woocommerce/changelog/fix-issue-36960 b/plugins/woocommerce/changelog/fix-issue-36960 new file mode 100644 index 00000000000..ceccf00278e --- /dev/null +++ b/plugins/woocommerce/changelog/fix-issue-36960 @@ -0,0 +1,4 @@ +Significance: patch +Type: fix + +Above notification threshold when "Out of stock threshold" filed value is empty diff --git a/plugins/woocommerce/includes/abstracts/abstract-wc-product.php b/plugins/woocommerce/includes/abstracts/abstract-wc-product.php index cd9385919e5..69e4d1f46c6 100644 --- a/plugins/woocommerce/includes/abstracts/abstract-wc-product.php +++ b/plugins/woocommerce/includes/abstracts/abstract-wc-product.php @@ -1386,7 +1386,7 @@ class WC_Product extends WC_Abstract_Legacy_Product { return; } - $stock_is_above_notification_threshold = ( $this->get_stock_quantity() > get_option( 'woocommerce_notify_no_stock_amount', 0 ) ); + $stock_is_above_notification_threshold = ( (int) $this->get_stock_quantity() > absint( get_option( 'woocommerce_notify_no_stock_amount', 0 ) ) ); $backorders_are_allowed = ( 'no' !== $this->get_backorders() ); if ( $stock_is_above_notification_threshold ) { diff --git a/plugins/woocommerce/src/Internal/Settings/OptionSanitizer.php b/plugins/woocommerce/src/Internal/Settings/OptionSanitizer.php index 2ecdba823fd..28969af4e7b 100644 --- a/plugins/woocommerce/src/Internal/Settings/OptionSanitizer.php +++ b/plugins/woocommerce/src/Internal/Settings/OptionSanitizer.php @@ -38,6 +38,8 @@ class OptionSanitizer { 2 ); } + // Cast "Out of stock threshold" field to absolute integer to prevent storing empty value. + self::add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_notify_no_stock_amount', 'absint' ); } /** diff --git a/plugins/woocommerce/tests/legacy/unit-tests/product/class-wc-tests-product.php b/plugins/woocommerce/tests/legacy/unit-tests/product/class-wc-tests-product.php index 2753bcef6d2..594a8199168 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/product/class-wc-tests-product.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/product/class-wc-tests-product.php @@ -66,7 +66,7 @@ class WC_Tests_Product extends WC_Unit_Test_Case { * [3, 4, false, "outofstock"] * * @param int $stock_quantity Current stock quantity for the product. - * @param bool $notify_no_stock_amount Value for the woocommerce_notify_no_stock_amount option. + * @param int $notify_no_stock_amount Value for the woocommerce_notify_no_stock_amount option. * @param bool $accepts_backorders Whether the product accepts backorders or not. * @param string $expected_stock_status The expected stock status of the product after being saved. */ diff --git a/plugins/woocommerce/tests/legacy/unit-tests/product/product-variable.php b/plugins/woocommerce/tests/legacy/unit-tests/product/product-variable.php index 95a45fd5574..2c2d940b020 100644 --- a/plugins/woocommerce/tests/legacy/unit-tests/product/product-variable.php +++ b/plugins/woocommerce/tests/legacy/unit-tests/product/product-variable.php @@ -154,7 +154,7 @@ class WC_Tests_Product_Variable extends WC_Unit_Test_Case { * [3, 4, false, "outofstock"] * * @param int $stock_quantity Current stock quantity for the product. - * @param bool $notify_no_stock_amount Value for the woocommerce_notify_no_stock_amount option. + * @param int $notify_no_stock_amount Value for the woocommerce_notify_no_stock_amount option. * @param bool $accepts_backorders Whether the product accepts backorders or not. * @param string $expected_stock_status The expected stock status of the product after being saved. */