Commit d39953bcdc5 for woocommerce
commit d39953bcdc554797e30bd70e3d40bf859842b66d
Author: Akanshu Singh <61490175+singhakanshu00@users.noreply.github.com>
Date: Mon Nov 23 14:35:05 2026 +0530
fix: Fixes half-cent rounding for flat rate percentage fees (#70221)
* fix: Fixes half-cent rounding for flat rate percentage fees
* test: Cover 0 and 3 decimal stores for flat rate fee rounding
---------
Co-authored-by: Ayush Pahwa <ayush.pahwa@automattic.com>
diff --git a/plugins/woocommerce/changelog/fix-flat-rate-percentage-fee-rounding b/plugins/woocommerce/changelog/fix-flat-rate-percentage-fee-rounding
new file mode 100644
index 00000000000..37c912bf8fe
--- /dev/null
+++ b/plugins/woocommerce/changelog/fix-flat-rate-percentage-fee-rounding
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Fix percentage-based Flat Rate shipping fees rounding half-cent amounts down on PHP 8.4+.
diff --git a/plugins/woocommerce/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php b/plugins/woocommerce/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php
index 7db2de075b2..a79501bead8 100644
--- a/plugins/woocommerce/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php
+++ b/plugins/woocommerce/includes/shipping/flat-rate/class-wc-shipping-flat-rate.php
@@ -9,6 +9,7 @@
defined( 'ABSPATH' ) || exit;
use Automattic\WooCommerce\Internal\Shipping\FlatRate\WeightPlaceholder;
+use Automattic\WooCommerce\Utilities\NumberUtil;
/**
* WC_Shipping_Flat_Rate class.
@@ -152,9 +153,13 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
if ( $atts['percent'] ) {
$calculated_fee = $this->fee_cost * ( floatval( $atts['percent'] ) / 100 );
- // Fix: Round to display decimals to prevent floating-point precision drift.
- // @see https://github.com/woocommerce/woocommerce/issues/62692.
- $calculated_fee = round( $calculated_fee, wc_get_price_decimals() );
+ // Round at the store's price decimals first, as cart totals do, so a fee stored just below a half (15% of 16.90 is 2.53499…) still rounds up.
+ // @see https://github.com/woocommerce/woocommerce/issues/70187.
+ $calculated_fee = wc_remove_number_precision(
+ NumberUtil::round(
+ wc_add_number_precision( $calculated_fee )
+ )
+ );
}
if ( $atts['min_fee'] && $calculated_fee < $atts['min_fee'] ) {
diff --git a/plugins/woocommerce/tests/php/includes/shipping/flat-rate/class-wc-shipping-flat-rate-test.php b/plugins/woocommerce/tests/php/includes/shipping/flat-rate/class-wc-shipping-flat-rate-test.php
index ddefaa6cc0e..dbf4fb7313a 100644
--- a/plugins/woocommerce/tests/php/includes/shipping/flat-rate/class-wc-shipping-flat-rate-test.php
+++ b/plugins/woocommerce/tests/php/includes/shipping/flat-rate/class-wc-shipping-flat-rate-test.php
@@ -98,6 +98,32 @@ class WC_Shipping_Flat_Rate_Test extends WC_Unit_Test_Case {
$this->assertEquals( 10.1, $val );
}
+ /**
+ * @testdox A 15% fee that falls exactly halfway rounds half up at $decimals price decimals, even when floating point stores it just below.
+ * @testWith [2, 16.90, 2.54]
+ * [2, 3.30, 0.50]
+ * [3, 0.19, 0.029]
+ * [0, 30, 5]
+ *
+ * @param int $decimals Store price decimals.
+ * @param float $cost Cost the fee applies to.
+ * @param float $expected Fee rounded half up at the store's price decimals.
+ */
+ public function test_evaluate_cost_percent_fee_rounds_half_up( int $decimals, float $cost, float $expected ): void {
+ update_option( 'woocommerce_price_num_decimals', $decimals );
+
+ $val = $this->call_evaluate_cost->call(
+ $this->sut,
+ '[fee percent="15"]',
+ array(
+ 'qty' => 1,
+ 'cost' => $cost,
+ )
+ );
+
+ $this->assertEquals( $expected, $val, "15% of {$cost} should round half up to {$expected} at {$decimals} decimals." );
+ }
+
/**
* Percent fee calculation keeps the decimals when the store uses a comma as the decimal separator.
*/