Commit 0aef6e8b265 for woocommerce
commit 0aef6e8b265db66f6da5f2af0d2d180260bed03d
Author: Kamal Hosen <kamalhosen8920@gmail.com>
Date: Fri Jul 17 16:29:50 2026 +0600
Fix leading zeros not trimmed from coupon amount on save (#66682)
* Fix leading zeros not trimmed from coupon amount on save
* Add tests for coupon amount leading-zero trimming
Adds data-driven test covering 9 scenarios: leading zeros (050),
edge cases (0, 0.50, 00.50, 0.0), normal values (20), negative
values (-10), over-100 values (150), and empty string input.
* Address reviewer feedback: fix test assertions and PHPCS
- Change assertSame to assertEquals to handle string-vs-float type mismatch
- Move negative amount test case to separate exception test
- Add @param doc tags to fix PHPCS errors
- Fix array double-arrow alignment for PHPCS warnings
* Use assertSame with string expected values for strict type guard
assertEquals allows '050' == 50.0 to pass (loose comparison), meaning the
test stays green even without the fix. Switch to assertSame with string
expectations so the test actually guards the leading-zero trimming logic.
* Use string expected values in data provider for strict assertion
assertSame('50', '050') fails without the fix while assertEquals(50.0, '050')
passed via loose type coercion, making the test a false guard.
* Fix empty string expected value to string '0'
get_amount() passes through wc_format_decimal() which returns string '0'.
diff --git a/plugins/woocommerce/changelog/fix-52333-coupon-leading-zero-trim b/plugins/woocommerce/changelog/fix-52333-coupon-leading-zero-trim
new file mode 100644
index 00000000000..ab62135d030
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-52333-coupon-leading-zero-trim
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Leading zeros are now trimmed from the coupon amount on save for cleaner display.
diff --git a/plugins/woocommerce/includes/class-wc-coupon.php b/plugins/woocommerce/includes/class-wc-coupon.php
index 32caf04a12c..55083de03b9 100644
--- a/plugins/woocommerce/includes/class-wc-coupon.php
+++ b/plugins/woocommerce/includes/class-wc-coupon.php
@@ -617,6 +617,15 @@ class WC_Coupon extends WC_Legacy_Coupon {
public function set_amount( $amount ) {
$amount = wc_format_decimal( $amount );
+ // Remove leading zeros from numeric strings (e.g., "050" becomes "50")
+ // to ensure clean display of the coupon amount.
+ if ( '' !== $amount && is_string( $amount ) ) {
+ $amount = ltrim( $amount, '0' );
+ if ( '' === $amount || '.' === $amount[0] ) {
+ $amount = '0' . $amount;
+ }
+ }
+
if ( ! is_numeric( $amount ) ) {
$amount = 0;
}
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-coupon-test.php b/plugins/woocommerce/tests/php/includes/class-wc-coupon-test.php
index 8a411adbe0c..149084cde23 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-coupon-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-coupon-test.php
@@ -231,4 +231,50 @@ class WC_Coupon_Tests extends WC_Unit_Test_Case {
'Line items associated with deleted products are not included in the discount calculation.'
);
}
+
+ /**
+ * @testdox set_amount removes leading zeros from numeric strings for clean display.
+ *
+ * @dataProvider data_provider_for_amount_leading_zeros
+ * @param mixed $input The input amount.
+ * @param mixed $expected The expected stored amount.
+ */
+ public function test_set_amount_removes_leading_zeros_from_coupon_amount(
+ $input,
+ $expected
+ ): void {
+ $coupon = new WC_Coupon();
+ $coupon->set_amount( $input );
+
+ $this->assertSame( $expected, $coupon->get_amount() );
+ }
+
+ /**
+ * Data provider for leading zero trimming tests.
+ *
+ * @return array
+ */
+ public function data_provider_for_amount_leading_zeros() {
+ return array(
+ 'leading zeros like 050' => array( '050', '50' ),
+ 'just zero' => array( '0', '0' ),
+ 'decimal with leading zero' => array( '0.50', '0.50' ),
+ 'multiple leading zeros' => array( '00.50', '0.50' ),
+ 'normal number without zeros' => array( '20', '20' ),
+ 'over 100 for non-percent' => array( 150, '150' ),
+ 'empty string becomes zero' => array( '', '0' ),
+ 'string 0.0' => array( '0.0', '0.0' ),
+ );
+ }
+
+ /**
+ * @testdox set_amount throws exception for negative amounts.
+ */
+ public function test_set_amount_throws_exception_for_negative_amounts(): void {
+ $coupon = new WC_Coupon();
+
+ $this->expectException( \WC_Data_Exception::class );
+
+ $coupon->set_amount( -10.0 );
+ }
}