Commit 3216aaedb5d for woocommerce
commit 3216aaedb5d57979c9c50ab92722dd34c79b18e0
Author: Darren Ethier <darren@roughsmootheng.in>
Date: Fri Aug 21 14:18:55 2026 -0400
Ensure inventory stock thresholds use consistent sanitization (#67925)
diff --git a/plugins/woocommerce/changelog/fix-consistent-stock-threshold-sanitization b/plugins/woocommerce/changelog/fix-consistent-stock-threshold-sanitization
new file mode 100644
index 00000000000..373839c7931
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-consistent-stock-threshold-sanitization
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Ensure low-stock thresholds are stored as non-negative integers.
diff --git a/plugins/woocommerce/src/Internal/Settings/OptionSanitizer.php b/plugins/woocommerce/src/Internal/Settings/OptionSanitizer.php
index 1757458eef6..1f7af7230fc 100644
--- a/plugins/woocommerce/src/Internal/Settings/OptionSanitizer.php
+++ b/plugins/woocommerce/src/Internal/Settings/OptionSanitizer.php
@@ -34,7 +34,8 @@ class OptionSanitizer {
2
);
}
- // Cast "Out of stock threshold" field to absolute integer to prevent storing empty value.
+ // Normalize stock threshold settings to non-negative integers.
+ add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_notify_low_stock_amount', 'absint' );
add_filter( 'woocommerce_admin_settings_sanitize_option_woocommerce_notify_no_stock_amount', 'absint' );
}
diff --git a/plugins/woocommerce/tests/php/src/Internal/Settings/OptionSanitizerTest.php b/plugins/woocommerce/tests/php/src/Internal/Settings/OptionSanitizerTest.php
new file mode 100644
index 00000000000..e58ef4ed9ee
--- /dev/null
+++ b/plugins/woocommerce/tests/php/src/Internal/Settings/OptionSanitizerTest.php
@@ -0,0 +1,51 @@
+<?php
+declare( strict_types = 1 );
+
+namespace Automattic\WooCommerce\Tests\Internal\Settings;
+
+use Automattic\WooCommerce\Internal\Settings\OptionSanitizer;
+use WC_Unit_Test_Case;
+
+/**
+ * Tests for OptionSanitizer.
+ */
+class OptionSanitizerTest extends WC_Unit_Test_Case {
+ /**
+ * @testdox Stock thresholds are sanitized consistently as absolute integers.
+ * @dataProvider stock_threshold_values_provider
+ *
+ * @param string $value Submitted threshold value.
+ * @param int $expected Expected sanitized value.
+ */
+ public function test_sanitizes_stock_thresholds_as_absolute_integers( string $value, int $expected ): void {
+ new OptionSanitizer();
+
+ $low_stock_threshold = apply_filters(
+ 'woocommerce_admin_settings_sanitize_option_woocommerce_notify_low_stock_amount',
+ $value
+ );
+ $no_stock_threshold = apply_filters(
+ 'woocommerce_admin_settings_sanitize_option_woocommerce_notify_no_stock_amount',
+ $value
+ );
+
+ $this->assertSame( $expected, $low_stock_threshold, 'The low-stock threshold should be stored as an absolute integer.' );
+ $this->assertSame( $expected, $no_stock_threshold, 'The out-of-stock threshold should be stored as an absolute integer.' );
+ }
+
+ /**
+ * Provides stock threshold values and their expected sanitized forms.
+ *
+ * @return array<string, array{string, int}>
+ */
+ public function stock_threshold_values_provider(): array {
+ return array(
+ 'positive integer' => array( '3', 3 ),
+ 'zero' => array( '0', 0 ),
+ 'negative integer' => array( '-2', 2 ),
+ 'fraction' => array( '2.9', 2 ),
+ 'empty value' => array( '', 0 ),
+ 'non-numeric text' => array( 'invalid', 0 ),
+ );
+ }
+}