From d2aaa3964563ede2d66fc138a25b986e15d1a32e Mon Sep 17 00:00:00 2001 From: bartech Date: Wed, 19 Apr 2023 19:39:51 +0200 Subject: [PATCH 1/6] Cast stock qty and no stock amount to int before comparing --- plugins/woocommerce/includes/abstracts/abstract-wc-product.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/woocommerce/includes/abstracts/abstract-wc-product.php b/plugins/woocommerce/includes/abstracts/abstract-wc-product.php index cd9385919e5..a65ac61600f 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 = ( absint( $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 ) { From c4d1e551268fb7f1297244ee042ad45e5e5aa9e5 Mon Sep 17 00:00:00 2001 From: bartech Date: Wed, 19 Apr 2023 19:40:44 +0200 Subject: [PATCH 2/6] Fix notify_no_stock_amount param type in tests --- .../tests/legacy/unit-tests/product/class-wc-tests-product.php | 2 +- .../tests/legacy/unit-tests/product/product-variable.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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. */ From 224284051b6fa035c7830a34bee3a7c84a5f1b05 Mon Sep 17 00:00:00 2001 From: bartech Date: Wed, 19 Apr 2023 19:53:55 +0200 Subject: [PATCH 3/6] Add changelog entry --- plugins/woocommerce/changelog/fix-issue-36960 | 4 ++++ 1 file changed, 4 insertions(+) create mode 100644 plugins/woocommerce/changelog/fix-issue-36960 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 From dda5fe9b1d54dccb3305e3b3f4612068320ee555 Mon Sep 17 00:00:00 2001 From: bartech Date: Wed, 19 Apr 2023 20:12:28 +0200 Subject: [PATCH 4/6] Cast woocommerce_notify_no_stock_amount value to absint on save --- .../includes/admin/settings/class-wc-settings-products.php | 1 + 1 file changed, 1 insertion(+) diff --git a/plugins/woocommerce/includes/admin/settings/class-wc-settings-products.php b/plugins/woocommerce/includes/admin/settings/class-wc-settings-products.php index 5fe4f46d5d8..d192b19b462 100644 --- a/plugins/woocommerce/includes/admin/settings/class-wc-settings-products.php +++ b/plugins/woocommerce/includes/admin/settings/class-wc-settings-products.php @@ -29,6 +29,7 @@ class WC_Settings_Products extends WC_Settings_Page { $this->label = __( 'Products', 'woocommerce' ); parent::__construct(); + add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_notify_no_stock_amount', 'absint' ); } /** From d32d08619bd5b4407354eb7d9be66492270b87f5 Mon Sep 17 00:00:00 2001 From: bartech Date: Wed, 19 Apr 2023 20:42:02 +0200 Subject: [PATCH 5/6] Change cast from absint to int for product stock quantity --- plugins/woocommerce/includes/abstracts/abstract-wc-product.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/woocommerce/includes/abstracts/abstract-wc-product.php b/plugins/woocommerce/includes/abstracts/abstract-wc-product.php index a65ac61600f..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 = ( absint( $this->get_stock_quantity() ) > absint( 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 ) { From 0da64a1eea81d8af59abf18fa999196e3ace4916 Mon Sep 17 00:00:00 2001 From: bartech Date: Thu, 20 Apr 2023 11:12:48 +0200 Subject: [PATCH 6/6] Move filter to Option Sanitizer class --- .../includes/admin/settings/class-wc-settings-products.php | 1 - plugins/woocommerce/src/Internal/Settings/OptionSanitizer.php | 2 ++ 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/plugins/woocommerce/includes/admin/settings/class-wc-settings-products.php b/plugins/woocommerce/includes/admin/settings/class-wc-settings-products.php index d192b19b462..5fe4f46d5d8 100644 --- a/plugins/woocommerce/includes/admin/settings/class-wc-settings-products.php +++ b/plugins/woocommerce/includes/admin/settings/class-wc-settings-products.php @@ -29,7 +29,6 @@ class WC_Settings_Products extends WC_Settings_Page { $this->label = __( 'Products', 'woocommerce' ); parent::__construct(); - add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_notify_no_stock_amount', 'absint' ); } /** 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' ); } /**