Commit 62f6bf1c0c8 for woocommerce
commit 62f6bf1c0c8daa9d09e0f641a0135a9f6ac90241
Author: Peerapat Pongnipakorn <39871299+Peerapatfc@users.noreply.github.com>
Date: Tue Nov 24 20:57:26 2026 +0700
Fix Store API order totals ignoring the store's number of decimals (#71092)
fix(store-api): use store price decimals for order endpoint totals
OrderSchema::get_totals() called prepare_money_response() without a
decimals argument, so every order total was formatted with the default
of 2 decimals while CurrencyFormatter reported currency_minor_unit as
wc_get_price_decimals(). On stores with a number of decimals other than
2, consumers that convert using currency_minor_unit (for example express
wallets on the order pay page) showed totals off by a power of ten.
Pass wc_get_price_decimals() to every money value in get_totals(),
matching CartSchema::get_totals().
Closes #71042
diff --git a/plugins/woocommerce/changelog/71042-fix-store-api-order-totals-decimals b/plugins/woocommerce/changelog/71042-fix-store-api-order-totals-decimals
new file mode 100644
index 00000000000..8466c13298b
--- /dev/null
+++ b/plugins/woocommerce/changelog/71042-fix-store-api-order-totals-decimals
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Store API: format order endpoint totals with the store's number of decimals so they match `currency_minor_unit`.
diff --git a/plugins/woocommerce/src/StoreApi/Schemas/V1/OrderSchema.php b/plugins/woocommerce/src/StoreApi/Schemas/V1/OrderSchema.php
index 823b1d377a1..e344f0c7ed6 100644
--- a/plugins/woocommerce/src/StoreApi/Schemas/V1/OrderSchema.php
+++ b/plugins/woocommerce/src/StoreApi/Schemas/V1/OrderSchema.php
@@ -337,14 +337,16 @@ class OrderSchema extends AbstractSchema {
* @return array
*/
protected function get_totals( $order ) {
+ $decimals = wc_get_price_decimals();
+
return [
- 'subtotal' => $this->prepare_money_response( $order->get_subtotal() ),
- 'total_discount' => $this->prepare_money_response( $order->get_total_discount() ),
- 'total_shipping' => $this->prepare_money_response( $order->get_total_shipping() ),
- 'total_fees' => $this->prepare_money_response( $order->get_total_fees() ),
- 'total_tax' => $this->prepare_money_response( $order->get_total_tax() ),
- 'total_refund' => $this->prepare_money_response( $order->get_total_refunded() ),
- 'total_price' => $this->prepare_money_response( $order->get_total() ),
+ 'subtotal' => $this->prepare_money_response( $order->get_subtotal(), $decimals ),
+ 'total_discount' => $this->prepare_money_response( $order->get_total_discount(), $decimals ),
+ 'total_shipping' => $this->prepare_money_response( $order->get_total_shipping(), $decimals ),
+ 'total_fees' => $this->prepare_money_response( $order->get_total_fees(), $decimals ),
+ 'total_tax' => $this->prepare_money_response( $order->get_total_tax(), $decimals ),
+ 'total_refund' => $this->prepare_money_response( $order->get_total_refunded(), $decimals ),
+ 'total_price' => $this->prepare_money_response( $order->get_total(), $decimals ),
'total_items' => $this->prepare_money_response(
array_sum(
array_map(
@@ -353,7 +355,8 @@ class OrderSchema extends AbstractSchema {
},
array_values( $order->get_items( OrderItemType::LINE_ITEM ) )
)
- )
+ ),
+ $decimals
),
'total_items_tax' => $this->prepare_money_response(
array_sum(
@@ -363,7 +366,8 @@ class OrderSchema extends AbstractSchema {
},
array_values( $order->get_items( OrderItemType::TAX ) )
)
- )
+ ),
+ $decimals
),
'total_fees_tax' => $this->prepare_money_response(
array_sum(
@@ -373,15 +377,16 @@ class OrderSchema extends AbstractSchema {
},
array_values( $order->get_items( OrderItemType::FEE ) )
)
- )
+ ),
+ $decimals
),
- 'total_discount_tax' => $this->prepare_money_response( $order->get_discount_tax() ),
- 'total_shipping_tax' => $this->prepare_money_response( $order->get_shipping_tax() ),
+ 'total_discount_tax' => $this->prepare_money_response( $order->get_discount_tax(), $decimals ),
+ 'total_shipping_tax' => $this->prepare_money_response( $order->get_shipping_tax(), $decimals ),
'tax_lines' => array_map(
- function( $item ) {
+ function ( $item ) use ( $decimals ) {
return [
'name' => $item->get_label(),
- 'price' => $this->prepare_money_response( $item->get_tax_total() ),
+ 'price' => $this->prepare_money_response( $item->get_tax_total(), $decimals ),
'rate' => strval( $item->get_rate_percent() ),
];
},
diff --git a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Order.php b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Order.php
index 9bf790f3fb1..280cba9cec7 100644
--- a/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Order.php
+++ b/plugins/woocommerce/tests/php/src/Blocks/StoreApi/Routes/Order.php
@@ -546,6 +546,51 @@ class Order extends ControllerTestCase {
}
+ /**
+ * Wallets such as Apple Pay and Google Pay convert these totals with `currency_minor_unit`,
+ * so both must use the store's number of decimals, as the cart endpoint does.
+ *
+ * @see https://github.com/woocommerce/woocommerce/issues/71042
+ *
+ * @testWith [0, "1740"]
+ * [2, "174000"]
+ *
+ * @testdox Order totals use the store's number of decimals ($decimals), matching currency_minor_unit.
+ *
+ * @param int $decimals Store setting for the number of decimals.
+ * @param string $expected_total Order total of 1740 in the minor unit for that setting.
+ */
+ public function test_order_totals_use_store_price_decimals( int $decimals, string $expected_total ): void {
+ add_filter(
+ 'wc_get_price_decimals',
+ function () use ( $decimals ) {
+ return $decimals;
+ }
+ );
+
+ $order = new \WC_Order();
+ $order->set_billing_email( 'guest@example.com' );
+ $order->add_product( $this->product, 174 );
+ $order->calculate_totals();
+ $order->save();
+
+ wp_set_current_user( 0 );
+
+ $request = new \WP_REST_Request( 'GET', '/wc/store/v1/order/' . $order->get_id() );
+ $request->set_param( 'key', $order->get_order_key() );
+ $request->set_param( 'billing_email', $order->get_billing_email() );
+
+ $response = rest_get_server()->dispatch( $request );
+ $this->assertSame( 200, $response->get_status() );
+
+ $totals = (array) $response->get_data()['totals'];
+
+ $this->assertSame( $decimals, $totals['currency_minor_unit'] );
+ $this->assertSame( $expected_total, $totals['total_price'] );
+ $this->assertSame( $expected_total, $totals['subtotal'] );
+ $this->assertSame( $expected_total, $totals['total_items'] );
+ }
+
/**
* Test that a guest can access a guest order with valid order key and billing email.
*/