Commit 13aed9e3331 for woocommerce
commit 13aed9e33318e1d582482fd598081b9363a901d9
Author: Tom Cafferkey <tjcafferkey@gmail.com>
Date: Thu Oct 15 13:12:02 2026 +0100
Add Orders API coupon decimal precision regression tests (#69801)
* Add Orders API coupon decimal precision regression tests
* Add changelog entry for coupon precision regression tests
* Check rounded coupon values in v2 decimal precision test
---------
Co-authored-by: Mike Jolley <mike.jolley@me.com>
diff --git a/plugins/woocommerce/changelog/dev-orders-api-coupon-dp-regression-tests b/plugins/woocommerce/changelog/dev-orders-api-coupon-dp-regression-tests
new file mode 100644
index 00000000000..fcf360bf20c
--- /dev/null
+++ b/plugins/woocommerce/changelog/dev-orders-api-coupon-dp-regression-tests
@@ -0,0 +1,3 @@
+Significance: patch
+Type: dev
+Comment: Cover coupon decimal precision in v2 and v3 Orders API responses; no production change.
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version2/class-wc-rest-orders-v2-controller-test.php b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version2/class-wc-rest-orders-v2-controller-test.php
index 5698d282998..db8590752eb 100644
--- a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version2/class-wc-rest-orders-v2-controller-test.php
+++ b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version2/class-wc-rest-orders-v2-controller-test.php
@@ -124,6 +124,36 @@ class WC_REST_Order_V2_Controller_Test extends WC_REST_Unit_Test_case {
$this->assertEquals( $order->get_id(), $response->data['id'] );
}
+ /**
+ * @testdox Coupon discounts in the V2 order response are rounded to the requested decimal precision.
+ *
+ * @testWith [0, "1", "0"]
+ * [3, "1.235", "0.235"]
+ * [5, "1.23460", "0.23460"]
+ *
+ * @param int $dp Requested decimal precision.
+ * @param string $expected_discount Expected formatted discount.
+ * @param string $expected_discount_tax Expected formatted discount tax.
+ */
+ public function test_coupon_lines_respect_decimal_precision( int $dp, string $expected_discount, string $expected_discount_tax ) {
+ $order = new WC_Order();
+ $coupon = new WC_Order_Item_Coupon();
+ $coupon->set_code( 'precision-coupon' );
+ $coupon->set_discount( '1.2346' );
+ $coupon->set_discount_tax( '0.2346' );
+ $order->add_item( $coupon );
+ $order->save();
+
+ $request = new WP_REST_Request( 'GET', '/wc/v2/orders/' . $order->get_id() );
+ $request->set_param( 'dp', $dp );
+ $response = $this->server->dispatch( $request );
+ $data = $response->get_data();
+
+ $this->assertSame( 200, $response->get_status() );
+ $this->assertSame( $expected_discount, $data['coupon_lines'][0]['discount'] );
+ $this->assertSame( $expected_discount_tax, $data['coupon_lines'][0]['discount_tax'] );
+ }
+
/**
* Test that `order_item_display_meta` keeps a variation attribute whose value only appears in the
* parent product name.
diff --git a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-orders-controller-tests.php b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-orders-controller-tests.php
index 73d4d1e8a45..21973db55a2 100644
--- a/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-orders-controller-tests.php
+++ b/plugins/woocommerce/tests/php/includes/rest-api/Controllers/Version3/class-wc-rest-orders-controller-tests.php
@@ -625,6 +625,12 @@ class WC_REST_Orders_Controller_Tests extends WC_REST_Unit_Test_Case {
$fee_item->set_taxes( array( 'total' => array( 1 => '0.5678' ) ) );
$order->add_item( $fee_item );
+ $coupon_item = new WC_Order_Item_Coupon();
+ $coupon_item->set_code( 'precision-coupon' );
+ $coupon_item->set_discount( '1.2345' );
+ $coupon_item->set_discount_tax( '0.2345' );
+ $order->add_item( $coupon_item );
+
$order_id = $order->save();
$refund = wc_create_refund(
array(
@@ -637,7 +643,7 @@ class WC_REST_Orders_Controller_Tests extends WC_REST_Unit_Test_Case {
);
$this->assertInstanceOf( WC_Order_Refund::class, $refund, 'The refund fixture should be created.' );
- foreach ( array( 0, 1, 3, wc_get_price_decimals() ) as $precision ) {
+ foreach ( array( 0, 1, 3, 5, wc_get_price_decimals() ) as $precision ) {
$request = new WP_REST_Request( 'GET', '/wc/v3/orders/' . $order_id );
if ( wc_get_price_decimals() !== $precision ) {
$request->set_param( 'dp', $precision );
@@ -663,6 +669,8 @@ class WC_REST_Orders_Controller_Tests extends WC_REST_Unit_Test_Case {
'shipping_lines.tax' => $data['shipping_lines'][0]['total_tax'],
'fee_lines.total' => $data['fee_lines'][0]['total'],
'fee_lines.total_tax' => $data['fee_lines'][0]['total_tax'],
+ 'coupon_lines.discount' => $data['coupon_lines'][0]['discount'],
+ 'coupon_lines.tax' => $data['coupon_lines'][0]['discount_tax'],
'refunds.total' => $data['refunds'][0]['total'],
);
foreach ( $monetary_values as $field => $value ) {