Commit b24d797259b for woocommerce
commit b24d797259b809bc172f204cf64e8ebdcc132b6c
Author: Oleksandr Aratovskyi <79862886+oaratovskyi@users.noreply.github.com>
Date: Fri Nov 27 12:13:42 2026 +0200
Add a gateway-recorded payment mode to orders and the Tracker (#70181)
* Add test-mode order totals to the WC Tracker payload
Record on each paid order whether its gateway was in test or live mode,
preferring a mode the gateway saved on the order, and report test order
totals per gateway under a new test_orders_by_gateway key. The existing
gateway totals under orders are unchanged.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* Add changelog entry for test-mode order totals in the Tracker
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* Match Payment Plugins for Stripe by base class when reading its mode
Official Stripe method IDs also start with `stripe_`, so a `_wc_stripe_mode`
left on the order by Payment Plugins could mark an official Stripe order.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* Add a PaymentMode enum and skip a full order save when recording the mode
Move the test/live values to `Enums\PaymentMode`, and treat
`_wc_payment_mode` as ephemeral meta so writing it never re-saves the order
and fires `woocommerce_update_order` a second time.
Tests now reset the container replacement, cover a `TypeError` and an
unrecognised gateway mode value, and assert the update hook fires once.
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* Add a @since tag to PaymentModeMarker::register()
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
* Let gateways record the payment mode instead of core inferring it
Replace PaymentModeMarker, which read gateway meta keys and settings,
with WC_Order::set_payment_mode() and get_payment_mode(). Core never
sets the mode, so orders whose gateway doesn't record it have none.
The Tracker reports test and live totals per gateway.
* Update changelog entry for gateway-recorded payment mode
* Clear the payment mode when an order switches gateway
A mode recorded during a failed attempt stayed on the order when a retry
used another gateway, so the Tracker counted that payment under the new
gateway's mode. Skip the check while the order is being read, since the
data stores call set_payment_method() then.
* Keep a payment mode the gateway set before switching its gateway ID
WooPayments moves an order to a split gateway ID after charging it, which cleared the mode it had just recorded. Only clear a stored mode the gateway didn't set again on this order object.
* Document recording test or live mode for payment gateways
* Clear the payment mode when the order is saved, not when the gateway is set
HPOS compatibility mode called set_payment_method() on a fresh copy of the order while syncing it, which removed a mode the gateway had just recorded from the posts tables. A recorded mode now also covers only one gateway switch.
* Report only test-mode order totals in the Tracker
Live totals would only count gateways that record a mode. Live orders are the gateway totals minus test orders.
* Document that later gateway changes also clear the payment mode
* Test payment mode after a save, with posts sync, and on two gateways
* Use the HPOS toggle trait to enable sync in the payment mode test
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Document that order copies shouldn't carry the payment mode
Subscriptions copies order meta to renewals and new subscriptions, so a
$0 renewal or a manual renewal marked paid by an admin kept a mode no
gateway recorded for it.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
* Target WooCommerce 11.5 for the payment mode API
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
---------
Co-authored-by: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
diff --git a/docs/features/payments/payment-gateway-api.md b/docs/features/payments/payment-gateway-api.md
index ab6939cadc5..9bcd91ccf9a 100644
--- a/docs/features/payments/payment-gateway-api.md
+++ b/docs/features/payments/payment-gateway-api.md
@@ -219,6 +219,26 @@ $order->add_order_note( __('IPN payment completed', 'woothemes') );
- If the order fails and has already been created, set to **Failed**
- If payment is complete, let WooCommerce handle the status and use `$order->payment_complete()`. WooCommerce will use either **Completed** or **Processing** status and handle stock. Call it only after the payment provider confirms the payment: see [Retries and uncertain outcomes](#retries-and-uncertain-outcomes).
+### Recording test or live mode
+
+Since WooCommerce 11.5, gateways can record whether an order was paid in test or live mode with `$order->set_payment_mode()`. WooCommerce never sets it, so an order whose gateway doesn't call it has no mode. Read it back with `$order->get_payment_mode()`, which returns `PaymentMode::TEST`, `PaymentMode::LIVE` or an empty string.
+
+Call it on every charge, when the gateway picks its test or live credentials and before it saves the order. That includes checkout, the pay-for-order page, subscription renewals, and charges completed by webhooks or other off-session requests. Record `live` too, so each order says which mode it was paid in.
+
+```php
+use Automattic\WooCommerce\Enums\PaymentMode;
+
+// Gateways that support WooCommerce versions before 11.5 need this check.
+if ( method_exists( $order, 'set_payment_mode' ) ) {
+ $order->set_payment_mode( $this->testmode ? PaymentMode::TEST : PaymentMode::LIVE );
+}
+$order->save();
+```
+
+When an order is saved with another payment method ID, WooCommerce clears its mode, so a mode from a failed attempt doesn't carry over to a retry with another gateway. A mode set on the same order object before the switch is kept. If your gateway changes the order's payment method ID in a later request, such as a redirect return or a webhook, call `set_payment_mode()` again in that request before saving.
+
+The mode describes one charge, so it shouldn't move to another order. Plugins that create orders by copying another order's meta, such as subscription renewals, should skip the `_wc_payment_mode` meta key. The gateway that charges the new order records its own mode.
+
## Notes on Direct Gateways
If you are creating an advanced, direct gateway (i.e., one that takes payment on the actual checkout page), there are additional steps involved. First, you need to set has_fields to true in the gateway constructor:
diff --git a/plugins/woocommerce/changelog/47284-tracker-test-orders-by-gateway b/plugins/woocommerce/changelog/47284-tracker-test-orders-by-gateway
new file mode 100644
index 00000000000..ba889cd711d
--- /dev/null
+++ b/plugins/woocommerce/changelog/47284-tracker-test-orders-by-gateway
@@ -0,0 +1,4 @@
+Significance: minor
+Type: add
+
+Let payment gateways record whether an order was paid in test or live mode, and report test-mode order totals in usage tracking.
diff --git a/plugins/woocommerce/includes/class-wc-order.php b/plugins/woocommerce/includes/class-wc-order.php
index 6052a80a959..36374e03480 100644
--- a/plugins/woocommerce/includes/class-wc-order.php
+++ b/plugins/woocommerce/includes/class-wc-order.php
@@ -7,6 +7,7 @@
*/
use Automattic\WooCommerce\Enums\OrderStatus;
+use Automattic\WooCommerce\Enums\PaymentMode;
use Automattic\WooCommerce\Enums\TaxDisplayMode;
use Automattic\WooCommerce\Utilities\FeaturesUtil;
use Automattic\WooCommerce\StoreApi\Utilities\LocalPickupUtils;
@@ -29,6 +30,13 @@ class WC_Order extends WC_Abstract_Order {
*/
protected $status_transition = false;
+ /**
+ * Whether set_payment_mode() was called on this object since the last gateway switch was saved.
+ *
+ * @var bool
+ */
+ private $payment_mode_recorded = false;
+
/**
* Whether set_status() leaves woocommerce_order_edit_status to the next save(). stage_status() sets it.
*
@@ -420,6 +428,7 @@ class WC_Order extends WC_Abstract_Order {
$this->run_deferred_actions( $deferred_actions, false );
$this->maybe_set_user_billing_email();
+ $this->maybe_clear_payment_mode();
parent::save();
if ( null === $this->get_last_save_error() ) {
$this->status_transition();
@@ -430,6 +439,24 @@ class WC_Order extends WC_Abstract_Order {
return $this->get_id();
}
+ /**
+ * Clear the payment mode when a saved order moves to another gateway, so a mode from a failed attempt doesn't
+ * carry over to a retry. A mode set on this object covers one switch, since gateways such as WooPayments move
+ * the order to another of their own IDs after charging.
+ */
+ private function maybe_clear_payment_mode(): void {
+ if ( ! $this->get_id() || ! array_key_exists( 'payment_method', $this->changes ) || $this->changes['payment_method'] === $this->data['payment_method'] ) {
+ return;
+ }
+
+ if ( $this->payment_mode_recorded ) {
+ $this->payment_mode_recorded = false;
+ return;
+ }
+
+ $this->delete_meta_data( '_wc_payment_mode' );
+ }
+
/**
* Log an error about this order is exception is encountered.
*
@@ -2799,6 +2826,43 @@ class WC_Order extends WC_Abstract_Order {
return apply_filters( 'woocommerce_order_is_created_via', $modus === $this->get_created_via(), $this, $modus );
}
+ /**
+ * Get whether the order was paid in test or live mode, as recorded by its payment gateway.
+ *
+ * @since 11.5.0
+ * @return string One of the PaymentMode constants, or an empty string when the gateway didn't record it.
+ */
+ public function get_payment_mode() {
+ $mode = $this->get_meta( '_wc_payment_mode' );
+ return in_array( $mode, array( PaymentMode::TEST, PaymentMode::LIVE ), true ) ? $mode : '';
+ }
+
+ /**
+ * Record whether the order is paid in test or live mode.
+ *
+ * Payment gateways should call this when they choose test or live credentials for a charge, before saving the order.
+ * WooCommerce never sets it, so an order whose gateway doesn't call it has no payment mode. Saving the order with
+ * another gateway clears the mode, unless this was called on the same object before that switch. That includes
+ * changes after payment, such as an admin edit or a REST update, so a cleared mode can only count a test order as live.
+ *
+ * @since 11.5.0
+ * @param string $mode One of the PaymentMode constants, or an empty string to clear it.
+ * @throws WC_Data_Exception When the mode is not valid.
+ * @return void
+ */
+ public function set_payment_mode( $mode ) {
+ if ( '' === $mode ) {
+ $this->delete_meta_data( '_wc_payment_mode' );
+ $this->payment_mode_recorded = true;
+ return;
+ }
+ if ( ! in_array( $mode, array( PaymentMode::TEST, PaymentMode::LIVE ), true ) ) {
+ $this->error( 'order_invalid_payment_mode', __( 'Invalid payment mode.', 'woocommerce' ) );
+ }
+ $this->update_meta_data( '_wc_payment_mode', $mode );
+ $this->payment_mode_recorded = true;
+ }
+
/**
* Attempts to restore the specified order back to its original status (after having been trashed).
*
diff --git a/plugins/woocommerce/includes/class-wc-tracker.php b/plugins/woocommerce/includes/class-wc-tracker.php
index 6bcd8167289..95458d745b4 100644
--- a/plugins/woocommerce/includes/class-wc-tracker.php
+++ b/plugins/woocommerce/includes/class-wc-tracker.php
@@ -13,6 +13,7 @@
use Automattic\Jetpack\Constants;
use Automattic\WooCommerce\Blocks\Domain\Services\CheckoutFields;
use Automattic\WooCommerce\Blocks\Package;
+use Automattic\WooCommerce\Enums\PaymentMode;
use Automattic\WooCommerce\Enums\ProductStatus;
use Automattic\WooCommerce\Internal\Admin\EmailImprovements\EmailImprovements;
use Automattic\WooCommerce\Internal\CLI\Migrator\Core\MigratorTracker;
@@ -320,6 +321,9 @@ class WC_Tracker {
$data['categories'] = self::get_category_counts();
$data['brands'] = self::get_brands_counts();
+ // Orders whose gateway recorded test mode. The gateway totals in `orders` count every order, test ones included.
+ $data['test_orders_by_gateway'] = self::get_orders_by_gateway( PaymentMode::TEST );
+
// Migrator CLI statistics.
$data['migrator'] = self::get_migrator_data();
@@ -915,24 +919,38 @@ class WC_Tracker {
/**
* Get order details by gateway.
*
+ * @param string $payment_mode Count only orders whose gateway recorded this PaymentMode value. Empty counts all orders.
* @return array
*/
- private static function get_orders_by_gateway() {
+ private static function get_orders_by_gateway( string $payment_mode = '' ) {
global $wpdb;
if ( OrderUtil::custom_orders_table_usage_is_enabled() ) {
$orders_table = OrdersTableDataStore::get_orders_table_name();
+ $meta_table = OrdersTableDataStore::get_meta_table_name();
// phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
+ $payment_mode_clause = $payment_mode ? $wpdb->prepare(
+ "AND id IN ( SELECT order_id FROM $meta_table WHERE meta_key = '_wc_payment_mode' AND meta_value = %s )",
+ $payment_mode
+ ) : '';
+
$orders_and_gateway_details = $wpdb->get_results(
"
SELECT IFNULL(payment_method, '') AS gateway, currency AS currency, SUM( total_amount ) AS totals, count( id ) AS counts
FROM $orders_table
WHERE status IN ( 'wc-completed', 'wc-processing', 'wc-refunded' )
+ $payment_mode_clause
GROUP BY gateway, currency;
"
);
// phpcs:enable
} else {
+ // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared
+ $payment_mode_clause = $payment_mode ? $wpdb->prepare(
+ "AND orders.id IN ( SELECT post_id FROM {$wpdb->postmeta} WHERE meta_key = '_wc_payment_mode' AND meta_value = %s )",
+ $payment_mode
+ ) : '';
+
$orders_and_gateway_details = $wpdb->get_results(
"
SELECT
@@ -950,11 +968,13 @@ class WC_Tracker {
WHERE orders.post_type = 'shop_order'
AND orders.post_status in ( 'wc-completed', 'wc-processing', 'wc-refunded' )
AND meta_key in( '_payment_method','_order_total','_order_currency')
+ $payment_mode_clause
GROUP BY orders.id
) order_gateways
GROUP BY gateway, currency
"
);
+ // phpcs:enable
}
$orders_by_gateway_currency = array();
diff --git a/plugins/woocommerce/src/Enums/PaymentMode.php b/plugins/woocommerce/src/Enums/PaymentMode.php
new file mode 100644
index 00000000000..7eaabe1b5e5
--- /dev/null
+++ b/plugins/woocommerce/src/Enums/PaymentMode.php
@@ -0,0 +1,27 @@
+<?php
+
+declare( strict_types = 1 );
+
+namespace Automattic\WooCommerce\Enums;
+
+/**
+ * Enum class for the payment mode a payment gateway records on an order with
+ * WC_Order::set_payment_mode(), which tells whether it was paid in test or live mode.
+ *
+ * @since 11.5.0
+ */
+final class PaymentMode {
+ /**
+ * The gateway charged the order in test mode.
+ *
+ * @var string
+ */
+ public const TEST = 'test';
+
+ /**
+ * The gateway charged the order in live mode.
+ *
+ * @var string
+ */
+ public const LIVE = 'live';
+}
diff --git a/plugins/woocommerce/src/Enums/README.md b/plugins/woocommerce/src/Enums/README.md
index 10f09055565..e7570c22e24 100644
--- a/plugins/woocommerce/src/Enums/README.md
+++ b/plugins/woocommerce/src/Enums/README.md
@@ -16,6 +16,7 @@ The enum classes make it easier to reference string values and avoid typos. They
- [OrderItemType](./OrderItemType.php) - Enumerates the possible types of an order line item.
- [OrderStatus](./OrderStatus.php) - Enumerates the possible statuses of an order.
- [PaymentGatewayFeatures](./PaymentGatewayFeatures.php) - Enumerates the possible features of a payment gateway.
+- [PaymentMode](./PaymentMode.php) - Enumerates the payment modes a payment gateway can record on an order.
- [ProductStatus](./ProductStatus.php) - Enumerates the possible statuses of a product.
- [ProductStockStatus](./ProductStockStatus.php) - Enumerates the possible stock statuses of a product.
- [ProductType](./ProductType.php) - Enumerates the possible types of a product.
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-order-test.php b/plugins/woocommerce/tests/php/includes/class-wc-order-test.php
new file mode 100644
index 00000000000..a4489890c8b
--- /dev/null
+++ b/plugins/woocommerce/tests/php/includes/class-wc-order-test.php
@@ -0,0 +1,188 @@
+<?php
+declare( strict_types = 1 );
+
+use Automattic\WooCommerce\Enums\PaymentMode;
+use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore;
+use Automattic\WooCommerce\RestApi\UnitTests\HPOSToggleTrait;
+use Automattic\WooCommerce\Utilities\OrderUtil;
+
+/**
+ * Tests for the WC_Order class.
+ */
+class WC_Order_Test extends WC_Unit_Test_Case {
+ use HPOSToggleTrait;
+
+ /**
+ * Ensure permanent HPOS tables exist before per-test transactions start, so the sync test can run with posts authoritative.
+ */
+ public static function wpSetUpBeforeClass(): void {
+ self::setup_cot_tables();
+ }
+
+ /**
+ * @testdox Should keep the payment mode a gateway sets after the order is saved and read again.
+ * @testWith ["test"]
+ * ["live"]
+ *
+ * @param string $mode Payment mode.
+ */
+ public function test_set_payment_mode_persists( string $mode ): void {
+ $order = wc_create_order();
+
+ $order->set_payment_mode( $mode );
+ $order->save();
+
+ $this->assertSame( $mode, wc_get_order( $order->get_id() )->get_payment_mode(), 'The saved payment mode should be read back' );
+ }
+
+ /**
+ * @testdox Should return an empty payment mode when the gateway didn't set one.
+ */
+ public function test_get_payment_mode_is_empty_by_default(): void {
+ $order = wc_create_order();
+
+ $this->assertSame( '', wc_get_order( $order->get_id() )->get_payment_mode(), 'WooCommerce should not set a payment mode' );
+ }
+
+ /**
+ * @testdox Should clear the payment mode when set to an empty string.
+ */
+ public function test_set_payment_mode_clears_with_empty_string(): void {
+ $order = wc_create_order();
+ $order->set_payment_mode( PaymentMode::TEST );
+ $order->save();
+
+ $order->set_payment_mode( '' );
+ $order->save();
+
+ $order = wc_get_order( $order->get_id() );
+ $this->assertSame( '', $order->get_payment_mode(), 'The payment mode should be cleared' );
+ $this->assertFalse( $order->meta_exists( '_wc_payment_mode' ), 'The payment mode meta should be deleted' );
+ }
+
+ /**
+ * @testdox Should clear the payment mode when the order switches to another gateway, and keep it otherwise.
+ */
+ public function test_set_payment_method_clears_payment_mode_from_another_gateway(): void {
+ $order = wc_create_order();
+ $order->set_payment_method( WC_Gateway_BACS::ID );
+ $order->set_payment_mode( PaymentMode::TEST );
+ $order->save();
+
+ $order = wc_get_order( $order->get_id() );
+ $this->assertSame( PaymentMode::TEST, $order->get_payment_mode(), 'Loading the order should keep its payment mode' );
+
+ $order->set_payment_method( WC_Gateway_BACS::ID );
+ $this->assertSame( PaymentMode::TEST, $order->get_payment_mode(), 'Setting the same gateway should keep the payment mode' );
+
+ $order->set_payment_method( WC_Gateway_COD::ID );
+ $order->save();
+
+ $this->assertSame( '', wc_get_order( $order->get_id() )->get_payment_mode(), 'Switching gateway should clear the payment mode' );
+ }
+
+ /**
+ * @testdox Should keep a payment mode the gateway set on the loaded order when it then switches the gateway ID, even after saving the mode first.
+ * @testWith [false]
+ * [true]
+ *
+ * @param bool $save_before_switch Whether the gateway saves the order after recording the mode and before switching its gateway ID.
+ */
+ public function test_set_payment_method_keeps_payment_mode_set_on_the_loaded_order( bool $save_before_switch ): void {
+ $order = wc_create_order();
+ $order->set_payment_method( WC_Gateway_BACS::ID );
+ $order->save();
+
+ $order = wc_get_order( $order->get_id() );
+ $order->set_payment_mode( PaymentMode::TEST );
+ if ( $save_before_switch ) {
+ $order->save();
+ }
+ $order->set_payment_method( WC_Gateway_COD::ID );
+ $order->save();
+
+ $this->assertSame( PaymentMode::TEST, wc_get_order( $order->get_id() )->get_payment_mode(), 'A mode set before the gateway switch should be kept' );
+
+ $order->set_payment_method( WC_Gateway_BACS::ID );
+ $order->save();
+
+ $this->assertSame( '', wc_get_order( $order->get_id() )->get_payment_mode(), 'A second gateway switch should clear the mode' );
+ }
+
+ /**
+ * @testdox Should sync a gateway switch to the backup copy of the order, keeping the mode only when the gateway recorded it before the switch.
+ * @testWith [true, "test"]
+ * [false, ""]
+ *
+ * @param bool $set_before_switch Whether the gateway records the mode on the loaded order before switching its gateway ID.
+ * @param string $expected_mode The payment mode expected in the backup copy after the switch.
+ */
+ public function test_payment_mode_gateway_switch_reaches_synced_copy( bool $set_before_switch, string $expected_mode ): void {
+ $this->enable_cot_sync();
+
+ $order = wc_create_order();
+ $order->set_payment_method( WC_Gateway_BACS::ID );
+ $order->set_payment_mode( PaymentMode::TEST );
+ $order->save();
+ $this->assertSame( PaymentMode::TEST, $this->get_synced_payment_mode( $order->get_id() ), 'The backup copy should receive the payment mode' );
+
+ $order = wc_get_order( $order->get_id() );
+ if ( $set_before_switch ) {
+ $order->set_payment_mode( PaymentMode::TEST );
+ }
+ $order->set_payment_method( WC_Gateway_COD::ID );
+ $order->save();
+
+ $this->assertSame( $expected_mode, $this->get_synced_payment_mode( $order->get_id() ), 'The backup copy should match the authoritative payment mode' );
+ }
+
+ /**
+ * Read the payment mode from the backup copy of an order: the posts copy when HPOS is authoritative, the HPOS copy otherwise.
+ *
+ * @param int $order_id Order ID.
+ * @return string
+ */
+ private function get_synced_payment_mode( int $order_id ): string {
+ global $wpdb;
+
+ if ( OrderUtil::custom_orders_table_usage_is_enabled() ) {
+ return (string) get_post_meta( $order_id, '_wc_payment_mode', true );
+ }
+
+ $meta_table = OrdersTableDataStore::get_meta_table_name();
+ return (string) $wpdb->get_var(
+ $wpdb->prepare(
+ "SELECT meta_value FROM $meta_table WHERE order_id = %d AND meta_key = '_wc_payment_mode'", // phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- Table name is provided by the data store.
+ $order_id
+ )
+ );
+ }
+
+ /**
+ * @testdox Should reject an unknown payment mode and keep the existing one.
+ */
+ public function test_set_payment_mode_rejects_unknown_value(): void {
+ $order = wc_create_order();
+ $order->set_payment_mode( PaymentMode::LIVE );
+
+ try {
+ $order->set_payment_mode( 'sandbox' );
+ $this->fail( 'An unknown payment mode should throw' );
+ } catch ( WC_Data_Exception $e ) {
+ $this->assertSame( 'order_invalid_payment_mode', $e->getErrorCode(), 'The error code should name the payment mode' );
+ }
+
+ $this->assertSame( PaymentMode::LIVE, $order->get_payment_mode(), 'The existing payment mode should be kept' );
+ }
+
+ /**
+ * @testdox Should return an empty payment mode when the stored value is not a known mode.
+ */
+ public function test_get_payment_mode_ignores_unknown_stored_value(): void {
+ $order = wc_create_order();
+ $order->update_meta_data( '_wc_payment_mode', 'sandbox' );
+ $order->save();
+
+ $this->assertSame( '', wc_get_order( $order->get_id() )->get_payment_mode(), 'An unknown stored value should not be returned' );
+ }
+}
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-tracker-test.php b/plugins/woocommerce/tests/php/includes/class-wc-tracker-test.php
index 1530d893b7c..9ed1ad07dcb 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-tracker-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-tracker-test.php
@@ -9,6 +9,8 @@ declare(strict_types=1);
use Automattic\WooCommerce\Caches\OrderCountCache;
use Automattic\WooCommerce\Enums\OrderInternalStatus;
+use Automattic\WooCommerce\Enums\OrderStatus;
+use Automattic\WooCommerce\Enums\PaymentMode;
use Automattic\WooCommerce\Internal\DataStores\Orders\OrdersTableDataStore;
use Automattic\WooCommerce\Internal\Features\FeaturesController;
use Automattic\WooCommerce\Utilities\OrderUtil;
@@ -588,6 +590,43 @@ class WC_Tracker_Test extends \WC_Unit_Test_Case {
}
}
+ /**
+ * @testDox Test orders by gateway count only paid orders recorded as test, and the gateway totals still count all orders.
+ */
+ public function test_get_tracking_data_orders_by_gateway_payment_mode(): void {
+ $orders = array(
+ array( WC_Gateway_BACS::ID, OrderStatus::PROCESSING, 10, PaymentMode::TEST ),
+ array( WC_Gateway_BACS::ID, OrderStatus::COMPLETED, 20, PaymentMode::LIVE ),
+ array( WC_Gateway_BACS::ID, OrderStatus::PENDING, 40, PaymentMode::TEST ),
+ array( WC_Gateway_COD::ID, OrderStatus::COMPLETED, 80, '' ),
+ array( WC_Gateway_COD::ID, OrderStatus::PROCESSING, 160, PaymentMode::TEST ),
+ );
+ foreach ( $orders as list( $payment_method, $status, $total, $mode ) ) {
+ $order = wc_create_order( array( 'status' => $status ) );
+ $order->set_payment_method( $payment_method );
+ $order->set_total( $total );
+ $order->set_payment_mode( $mode );
+ $order->save();
+ }
+
+ $tracking_data = WC_Tracker::get_tracking_data();
+
+ $this->assertEquals(
+ array(
+ 'gateway_bacs_USD_count' => 1,
+ 'gateway_bacs_USD_total' => 10,
+ 'gateway_cod_USD_count' => 1,
+ 'gateway_cod_USD_total' => 160,
+ ),
+ $tracking_data['test_orders_by_gateway'],
+ 'Only paid orders recorded as test should be counted, per gateway'
+ );
+ $this->assertEquals( 2, $tracking_data['orders']['gateway_bacs_USD_count'], 'Gateway counts should include test orders' );
+ $this->assertEquals( 30, $tracking_data['orders']['gateway_bacs_USD_total'], 'Gateway totals should include test orders' );
+ $this->assertEquals( 2, $tracking_data['orders']['gateway_cod_USD_count'], 'Orders without a payment mode should keep counting' );
+ $this->assertEquals( 240, $tracking_data['orders']['gateway_cod_USD_total'], 'Gateway totals should include orders without a payment mode' );
+ }
+
/**
* Persist the order matrix read by the tracker aggregate queries.
*