fix(2meet-data-optimizer-woocommerce-addon): read real Marketplace commission keys
Tests / PHP Lint (pull_request) Successful in 11s
Tests / Integration Tests (pull_request) Successful in 59s
Tests / Unit Tests (pull_request) Successful in 59s

- Marketplace writes hp_vendor / hp_commission_rate / hp_commission_fee (no
  underscore); the _hp_* keys never existed, so no commission was recorded
- Compute subtotal, vendor payout and commission the way
  Marketplace::get_order_profit() does, honouring hp_vendor_include_taxes
- Store the vendor's user ID, the product's parent listing, and the platform
  commission percentage (Marketplace stores the vendor share, e.g. 0.9)

Verify: phpunit-integration --filter WCOrdersInterceptorTest (4 tests);
  WC unit 10 OK, integration 22 OK
PR: PR-012
This commit is contained in:
2026-09-25 22:47:35 +08:00
parent 421a8bc777
commit 094248b75d
2 changed files with 184 additions and 11 deletions
+24 -11
View File
@@ -6,8 +6,9 @@
* `wp_wpdo_wc_commissions` table. HPOS-aware (uses `wc_get_order()`).
*
* Trigger: `woocommerce_order_status_changed` → on `processing`/`completed`
* read order's vendor meta (`_hp_vendor`, `_hp_commission`, etc.)
* and INSERT/UPDATE `wp_wpdo_wc_commissions`.
* read hivepress-marketplace's order meta (`hp_vendor`,
* `hp_commission_rate`, `hp_commission_fee`) and INSERT/UPDATE
* `wp_wpdo_wc_commissions`.
*
* Reads from order via `wc_get_order()` — works for both HPOS and legacy.
*
@@ -76,7 +77,7 @@ class TMDO_WC_Orders_Interceptor extends TMDO_Interceptor_Base {
/**
* INSERT/UPDATE wp_wpdo_wc_commissions for the given order.
*
* Skips orders without a `_hp_vendor` meta — only marketplace orders count.
* Skips orders without an `hp_vendor` meta — only marketplace orders count.
*
* @param int $wc_order_id WooCommerce order ID.
* @param string $status Order status.
@@ -91,18 +92,30 @@ class TMDO_WC_Orders_Interceptor extends TMDO_Interceptor_Base {
return;
}
$vendor_id = (int) $order->get_meta( '_hp_vendor' );
if ( $vendor_id <= 0 ) {
$vendor_post_id = (int) $order->get_meta( 'hp_vendor' );
if ( $vendor_post_id <= 0 ) {
// Not a marketplace order — skip.
return;
}
$listing_id = (int) $order->get_meta( '_hp_listing' );
$subtotal = (float) ( $order->get_meta( '_hp_subtotal' ) ?: 0 );
$commission = (float) ( $order->get_meta( '_hp_commission' ) ?: 0 );
$vendor_payout = (float) ( $order->get_meta( '_hp_vendor_payout' ) ?: 0 );
$commission_rate = (float) ( $order->get_meta( '_hp_commission_rate' ) ?: 0 );
$hpos_enabled = class_exists( 'TMDO_WooCommerce' ) && TMDO_WooCommerce::is_hpos_enabled() ? 1 : 0;
// hivepress-marketplace stores the vendor's share (0.9 for a 10% commission) and
// a flat fee, and pays vendors net_total * share - fee (Marketplace::get_order_profit()).
$vendor_share = (float) $order->get_meta( 'hp_commission_rate' );
$fee = (float) $order->get_meta( 'hp_commission_fee' );
$subtotal = (float) $order->get_total() - (float) $order->get_total_refunded();
if ( ! get_option( 'hp_vendor_include_taxes' ) ) {
$subtotal -= (float) $order->get_total_tax();
}
$vendor_payout = max( 0.0, $subtotal * $vendor_share - $fee );
$commission = $subtotal - $vendor_payout;
$commission_rate = round( ( 1 - $vendor_share ) * 100, 2 ); // Platform percentage, as set in Marketplace.
// vendor_id holds the vendor's user ID (the admin screen resolves it with get_userdata());
// the listing is the product's parent, as in Marketplace::get_product_listing().
$vendor_id = (int) get_post_field( 'post_author', $vendor_post_id );
$item = current( $order->get_items() );
$listing_id = $item ? (int) get_post_field( 'post_parent', $item->get_product_id() ) : 0;
$hpos_enabled = class_exists( 'TMDO_WooCommerce' ) && TMDO_WooCommerce::is_hpos_enabled() ? 1 : 0;
$table = TMDO_DB::table( 'wpdo_wc_commissions' );
$now = TMDO_DB::now();