Commit 951713ef03d for woocommerce
commit 951713ef03ddd88cf72efb6d4a13c3952fb44887
Author: Yuliyan Slavchev <yuliyan.slavchev@gmail.com>
Date: Fri Dec 4 13:14:55 2026 +0200
Keep percent coupon rounding cents on lines the coupon applies to (#71814)
diff --git a/plugins/woocommerce/changelog/fix-percent-coupon-rounding-remainder b/plugins/woocommerce/changelog/fix-percent-coupon-rounding-remainder
new file mode 100644
index 00000000000..30708c60536
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-percent-coupon-rounding-remainder
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Keep percentage coupon rounding on the items the coupon applies to, when another coupon has already made an item free.
diff --git a/plugins/woocommerce/includes/class-wc-discounts.php b/plugins/woocommerce/includes/class-wc-discounts.php
index e710fd46d72..302c5b08e44 100644
--- a/plugins/woocommerce/includes/class-wc-discounts.php
+++ b/plugins/woocommerce/includes/class-wc-discounts.php
@@ -362,6 +362,8 @@ class WC_Discounts {
protected function apply_coupon_percent( $coupon, $items_to_apply ) {
$total_discount = 0;
$cart_total = 0;
+ $items_discount_total = 0;
+ $items_to_adjust = array();
$limit_usage_qty = 0;
$applied_count = 0;
$adjust_final_discount = true;
@@ -398,19 +400,25 @@ class WC_Discounts {
}
$discount = wc_round_discount( min( $discounted_price, $discount ), 0 );
- $cart_total = $cart_total + $price_to_discount;
$total_discount = $total_discount + $discount;
$applied_count = $applied_count + $apply_quantity;
// Store code and discount amount per item.
$this->discounts[ $coupon->get_code() ][ $item->key ] += $discount;
+
+ // Only lines the coupon applies to and that still have a price left can take rounding cents.
+ if ( $apply_quantity > 0 && $discount < $discounted_price ) {
+ $cart_total = $cart_total + $price_to_discount;
+ $items_discount_total = $items_discount_total + $discount;
+ $items_to_adjust[] = $item;
+ }
}
- // Work out how much discount would have been given to the cart as a whole and compare to what was discounted on all line items.
+ // Work out how much discount would have been given to these lines as a whole and compare to what was discounted on them.
$cart_total_discount = wc_round_discount( $cart_total * ( $coupon_amount / 100 ), 0 );
- if ( $total_discount < $cart_total_discount && $adjust_final_discount ) {
- $total_discount += $this->apply_coupon_remainder( $coupon, $items_to_apply, $cart_total_discount - $total_discount );
+ if ( $items_discount_total < $cart_total_discount && $adjust_final_discount ) {
+ $total_discount += $this->apply_coupon_remainder( $coupon, $items_to_adjust, $cart_total_discount - $items_discount_total );
}
return $total_discount;
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-discounts-tests.php b/plugins/woocommerce/tests/php/includes/class-wc-discounts-tests.php
index 5a939c7bc87..e1eeb24263f 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-discounts-tests.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-discounts-tests.php
@@ -644,4 +644,97 @@ class WC_Discounts_Tests extends WC_Unit_Test_Case {
$this->assertEquals( 1000, $discounts->get_discounts_by_coupon( true )[ $coupon->get_code() ], 'The discount should stop at the rounded item price' );
$this->assertSame( 0, $discounts->get_discounted_price_in_cents( current( $discounts->get_items() ) ), 'A used-up item should have nothing left to discount' );
}
+
+ /**
+ * Data provider for test_percent_coupon_discount_capped_at_line_price_does_not_move_to_other_lines.
+ *
+ * @return array[] First coupon type and amount, 50% coupon item limit, expected 50% coupon discount on the cheap line in cents.
+ */
+ public function provide_capped_percent_coupon_cases(): array {
+ return array(
+ 'percent then percent limited to 1 item' => array( 'percent', '70', '1', 0 ),
+ 'fixed product then percent, no limit' => array( 'fixed_product', '70', '', 20000 ),
+ );
+ }
+
+ /**
+ * @testdox A percent coupon capped at a line already reduced by another coupon does not move the cut-off amount to other lines.
+ *
+ * @dataProvider provide_capped_percent_coupon_cases
+ *
+ * @param string $first_type Discount type of the coupon restricted to the expensive product.
+ * @param string $first_amount Amount of that coupon.
+ * @param string $limit_items Item limit of the 50% coupon.
+ * @param int $expected_cheap Expected discount of the 50% coupon on the cheap line, in cents.
+ */
+ public function test_percent_coupon_discount_capped_at_line_price_does_not_move_to_other_lines( string $first_type, string $first_amount, string $limit_items, int $expected_cheap ): void {
+ update_option( 'woocommerce_calc_taxes', 'no' );
+ $expensive = WC_Helper_Product::create_simple_product( true, array( 'regular_price' => 100 ) );
+ $cheap = WC_Helper_Product::create_simple_product( true, array( 'regular_price' => 0.2 ) );
+
+ $first_coupon = WC_Helper_Coupon::create_coupon(
+ 'first-' . $first_type,
+ array(
+ 'discount_type' => $first_type,
+ 'coupon_amount' => $first_amount,
+ 'product_ids' => (string) $expensive->get_id(),
+ )
+ );
+ $second_coupon = WC_Helper_Coupon::create_coupon(
+ 'percent-50',
+ array(
+ 'discount_type' => 'percent',
+ 'coupon_amount' => '50',
+ 'limit_usage_to_x_items' => $limit_items,
+ )
+ );
+
+ $expensive_key = WC()->cart->add_to_cart( $expensive->get_id(), 1 );
+ $cheap_key = WC()->cart->add_to_cart( $cheap->get_id(), 2000 );
+
+ $discounts = new WC_Discounts( WC()->cart );
+ $discounts->apply_coupon( $first_coupon );
+ $discounts->apply_coupon( $second_coupon );
+
+ $by_coupon = $discounts->get_discounts( true );
+ $this->assertSame( 0, (int) $by_coupon[ $first_coupon->get_code() ][ $cheap_key ], 'The first coupon should not discount the cheap line' );
+ $this->assertSame( 3000, (int) $by_coupon[ $second_coupon->get_code() ][ $expensive_key ], 'The 50% coupon should stop at what is left of the expensive line' );
+ $this->assertSame( $expected_cheap, (int) $by_coupon[ $second_coupon->get_code() ][ $cheap_key ], 'The amount cut off at the expensive line should not move to the cheap line' );
+ }
+
+ /**
+ * @testdox A percent coupon still adds rounding cents to lines that have a price left when another line is capped.
+ */
+ public function test_percent_coupon_rounding_cents_reach_uncapped_lines_next_to_capped_line(): void {
+ update_option( 'woocommerce_calc_taxes', 'no' );
+ $expensive = WC_Helper_Product::create_simple_product( true, array( 'regular_price' => 100 ) );
+ $cheap = WC_Helper_Product::create_simple_product( true, array( 'regular_price' => 0.1 ) );
+
+ $fixed_coupon = WC_Helper_Coupon::create_coupon(
+ 'fixed-product-70',
+ array(
+ 'discount_type' => 'fixed_product',
+ 'coupon_amount' => '70',
+ 'product_ids' => (string) $expensive->get_id(),
+ )
+ );
+ $percent_coupon = WC_Helper_Coupon::create_coupon(
+ 'percent-33',
+ array(
+ 'discount_type' => 'percent',
+ 'coupon_amount' => '33',
+ )
+ );
+
+ $expensive_key = WC()->cart->add_to_cart( $expensive->get_id(), 1 );
+ $cheap_key = WC()->cart->add_to_cart( $cheap->get_id(), 3 );
+
+ $discounts = new WC_Discounts( WC()->cart );
+ $discounts->apply_coupon( $fixed_coupon );
+ $discounts->apply_coupon( $percent_coupon );
+
+ $percent_discounts = $discounts->get_discounts( true )[ $percent_coupon->get_code() ];
+ $this->assertSame( 3000, (int) $percent_discounts[ $expensive_key ], 'The percent coupon should stop at what is left of the expensive line' );
+ $this->assertSame( 10, (int) $percent_discounts[ $cheap_key ], '33% of 0.30 should round to 0.10, not stay at 0.09 or take more' );
+ }
}