From 50989396a812aa63a08852ef0973ac0803e257f2 Mon Sep 17 00:00:00 2001 From: wpdev Date: Sun, 27 Sep 2026 16:29:28 +0800 Subject: [PATCH] fix(2meet-data-optimizer-woocommerce-addon): recompute commission amounts on every tracked status change and refund - the commission upsert replaces subtotal, commission, vendor_payout and commission_rate on conflict (it kept the first amounts, so a partial refund before completion never reached the row) - woocommerce_order_refunded recomputes a tracked or already recorded order; a fully refunded order keeps its row at zero, untracked orders get none - Verify: WCOrdersInterceptorTest (5 new), integration 27, unit 11, phpcs; dev36 flow_wc partial refund -> completed - PR: PR-092 --- includes/class-tmdo-wc-orders-interceptor.php | 49 ++++++++++++++- tests/integration/WCOrdersInterceptorTest.php | 63 ++++++++++++++++++- 2 files changed, 108 insertions(+), 4 deletions(-) diff --git a/includes/class-tmdo-wc-orders-interceptor.php b/includes/class-tmdo-wc-orders-interceptor.php index cda335b..155c638 100644 --- a/includes/class-tmdo-wc-orders-interceptor.php +++ b/includes/class-tmdo-wc-orders-interceptor.php @@ -49,6 +49,7 @@ class TMDO_WC_Orders_Interceptor extends TMDO_Interceptor_Base { */ public function register_hooks(): void { add_action( 'woocommerce_order_status_changed', array( $this, 'action_order_status_changed' ), 10, 3 ); + add_action( 'woocommerce_order_refunded', array( $this, 'action_order_refunded' ), 10, 2 ); } /** @@ -74,6 +75,48 @@ class TMDO_WC_Orders_Interceptor extends TMDO_Interceptor_Base { } } + /** + * Recompute the amounts after a refund, which need not change the status. + * Orders never tracked (no row, untracked status) are left alone; a + * recorded order that is now fully refunded keeps its row at zero. + * + * @param int $order_id Order ID. + * @param int $refund_id Refund ID (unused). + * @return void + */ + public function action_order_refunded( int $order_id, int $refund_id ): void { // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed + if ( ! $this->is_active() || ! function_exists( 'wc_get_order' ) ) { + return; + } + $order = wc_get_order( $order_id ); + if ( ! $order ) { + return; + } + $status = (string) $order->get_status(); + if ( ! in_array( $status, self::TRACKED_STATUSES, true ) && ! $this->has_commission( $order_id ) ) { + return; + } + + try { + $this->sync_commission( $order_id, $status ); + } catch ( \Throwable $e ) { + TMDO_Logger::error( $this->module, 'woocommerce_order_refunded', $e->getMessage() ); + } + } + + /** + * Whether a commission row exists for the order. + * + * @param int $wc_order_id WooCommerce order ID. + * @return bool + */ + private function has_commission( int $wc_order_id ): bool { + global $wpdb; + $table = TMDO_DB::table( 'wpdo_wc_commissions' ); + // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery,WordPress.DB.DirectDatabaseQuery.NoCaching,WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- own table, name from TMDO_DB::table(). + return (bool) $wpdb->get_var( $wpdb->prepare( "SELECT 1 FROM `{$table}` WHERE wc_order_id = %d LIMIT 1", $wc_order_id ) ); + } + /** * INSERT/UPDATE wp_wpdo_wc_commissions for the given order. * @@ -123,8 +166,8 @@ class TMDO_WC_Orders_Interceptor extends TMDO_Interceptor_Base { // v2.1.2 race-condition fix: atomic upsert via UNIQUE KEY ui_order_vendor. // Replaces SELECT-then-INSERT/UPDATE which could race two concurrent // status-change events (e.g. gateway IPN + admin click) and trigger - // duplicate-key DB errors. Single round-trip; on conflict only updates - // the mutable columns (status / updated_at), preserves financials. + // duplicate-key DB errors. Single round-trip; on conflict the amounts are + // replaced too, so a later event reflects refunds (2.3 PR-092). TMDO_DB::upsert( $table, array( @@ -140,7 +183,7 @@ class TMDO_WC_Orders_Interceptor extends TMDO_Interceptor_Base { 'created_at' => $now, 'updated_at' => $now, ), - array( 'status', 'updated_at' ), // columns to update on conflict. + array( 'subtotal', 'commission', 'vendor_payout', 'commission_rate', 'status', 'updated_at' ), // columns to update on conflict. array( 'wc_order_id', 'vendor_id' ), // composite unique key. array( '%d', '%d', '%d', '%f', '%f', '%f', '%f', '%s', '%d', '%s', '%s' ) ); diff --git a/tests/integration/WCOrdersInterceptorTest.php b/tests/integration/WCOrdersInterceptorTest.php index 09230ea..e624a47 100644 --- a/tests/integration/WCOrdersInterceptorTest.php +++ b/tests/integration/WCOrdersInterceptorTest.php @@ -25,9 +25,14 @@ if ( ! class_exists( 'TMDO_Test_WC_Order' ) ) { private float $total, private float $tax, private float $refunded, - private int $product_id + private int $product_id, + private string $status = 'processing' ) {} + public function get_status(): string { + return $this->status; + } + public function get_meta( string $key ) { return $this->meta[ $key ] ?? ''; } @@ -106,6 +111,11 @@ class WCOrdersInterceptorTest extends TestCase { ( new TMDO_WC_Orders_Interceptor() )->action_order_status_changed( self::ORDER_ID, 'pending', 'processing' ); } + /** The same order after a refund (and, for a full refund, a new status). */ + private function refund( float $refunded, string $status ): void { + $GLOBALS['_tmdo_test_orders'][ self::ORDER_ID ] = new TMDO_Test_WC_Order( $this->marketplace_meta(), 110.0, 10.0, $refunded, self::PRODUCT_ID, $status ); + } + private function commission_row(): ?array { global $wpdb; return $wpdb->get_row( 'SELECT * FROM `wp_itest_wpdo_wc_commissions` WHERE wc_order_id = ' . self::ORDER_ID, ARRAY_A ); @@ -157,4 +167,55 @@ class WCOrdersInterceptorTest extends TestCase { $this->assertEqualsWithDelta( 0.0, (float) $row['vendor_payout'], 0.0001 ); $this->assertEqualsWithDelta( 3.0, (float) $row['commission'], 0.0001 ); } + + public function test_partial_refund_then_completion_records_the_refunded_amounts(): void { + $this->place_order( $this->marketplace_meta(), 110.0, 10.0 ); + $this->refund( 50.0, 'processing' ); + $interceptor = new TMDO_WC_Orders_Interceptor(); + $interceptor->action_order_status_changed( self::ORDER_ID, 'processing', 'completed' ); + $interceptor->action_order_status_changed( self::ORDER_ID, 'processing', 'completed' ); // Duplicate event. + + global $wpdb; + $this->assertSame( '1', (string) $wpdb->get_var( 'SELECT COUNT(*) FROM `wp_itest_wpdo_wc_commissions` WHERE wc_order_id = ' . self::ORDER_ID ) ); + $row = $this->commission_row(); + $this->assertSame( 'completed', $row['status'] ); + $this->assertEqualsWithDelta( 50.0, (float) $row['subtotal'], 0.0001, '110 - 50 refunded - 10 tax' ); + $this->assertEqualsWithDelta( 40.0, (float) $row['vendor_payout'], 0.0001 ); + $this->assertEqualsWithDelta( 10.0, (float) $row['commission'], 0.0001 ); + } + + public function test_a_refund_recomputes_the_commission_without_a_status_change(): void { + $this->place_order( $this->marketplace_meta(), 110.0, 10.0 ); + $this->refund( 50.0, 'processing' ); + + ( new TMDO_WC_Orders_Interceptor() )->action_order_refunded( self::ORDER_ID, 1 ); + + $this->assertEqualsWithDelta( 50.0, (float) $this->commission_row()['subtotal'], 0.0001 ); + } + + public function test_a_full_refund_zeroes_a_recorded_commission(): void { + $this->place_order( $this->marketplace_meta(), 110.0, 10.0 ); + $this->refund( 110.0, 'refunded' ); + + ( new TMDO_WC_Orders_Interceptor() )->action_order_refunded( self::ORDER_ID, 1 ); + + $row = $this->commission_row(); + $this->assertSame( 'refunded', $row['status'] ); + $this->assertEqualsWithDelta( 0.0, (float) $row['vendor_payout'], 0.0001 ); + } + + public function test_a_refund_of_an_untracked_order_records_nothing(): void { + $this->refund( 50.0, 'pending' ); + + ( new TMDO_WC_Orders_Interceptor() )->action_order_refunded( self::ORDER_ID, 1 ); + + $this->assertNull( $this->commission_row() ); + } + + public function test_refunds_are_hooked(): void { + $interceptor = new TMDO_WC_Orders_Interceptor(); + $interceptor->register_hooks(); + + $this->assertContains( array( $interceptor, 'action_order_refunded' ), $GLOBALS['_wp_filter_callbacks']['woocommerce_order_refunded'] ?? array() ); + } }