delete( "{$wpdb->prefix}vv_system_orders", [ 'order_id' => $order_id ], [ '%d' ] ); foreach ( $order->get_items() as $item ) { $product_id = $item->get_product_id(); $vendor_id = (int) get_post_field( 'post_author', $product_id ); $wpdb->insert( "{$wpdb->prefix}vv_system_orders", [ 'product_id' => $product_id, 'order_id' => $order_id, 'product_owner_user_id' => $vendor_id, 'order_owner_user_id' => (int) $order->get_user_id(), 'ip_address' => sanitize_text_field( $_SERVER['REMOTE_ADDR'] ?? '' ), ] ); } // Fire webhook do_action( 'vv_webhook_new_order', $order_id ); } public function remove_order( $order_id ) { global $wpdb; $wpdb->delete( "{$wpdb->prefix}vv_system_orders", [ 'order_id' => $order_id ], [ '%d' ] ); } public function process_payout( $order_id ) { global $wpdb; $order = wc_get_order( $order_id ); if ( ! $order ) return; $order_data = $order->get_data(); $buyer_id = (int) $order->get_user_id(); foreach ( $order->get_items() as $item ) { $product_id = $item->get_product_id(); $vendor_id = (int) get_post_field( 'post_author', $product_id ); $total = (float) $item->get_total(); $product_name= $item->get_name(); $commission_rate = VV_Helpers::get_commission_rate( $vendor_id, $product_id ); $commission_fee = round( ( $commission_rate / 100 ) * $total, 2 ); $vendor_payout = round( $total - $commission_fee, 2 ); $desc = sprintf( __( 'Order #%d — %s', 'vendorverse' ), $order_id, $product_name ); // Credit vendor VV_Helpers::record_transaction( $vendor_id, $vendor_payout, 'credit', $desc, $product_id, $order_id ); // Debit admin commission from vendor $commission_desc = sprintf( __( 'Platform commission (%.0f%%) for order #%d', 'vendorverse' ), $commission_rate, $order_id ); VV_Helpers::record_transaction( $vendor_id, $commission_fee, 'debit', $commission_desc, $product_id, $order_id ); // Credit admin account (user ID 1) VV_Helpers::record_transaction( 1, $commission_fee, 'credit', $commission_desc, $product_id, $order_id ); VV_Helpers::flush_vendor_cache( $vendor_id ); // Notify vendor do_action( 'vv_vendor_order_completed', $vendor_id, $order_id, $vendor_payout, $order ); do_action( 'vv_webhook_order_completed', $order_id ); } } public function handle_refund( $order_id ) { // Reverse the payout transactions global $wpdb; $order = wc_get_order( $order_id ); if ( ! $order ) return; foreach ( $order->get_items() as $item ) { $product_id = $item->get_product_id(); $vendor_id = (int) get_post_field( 'post_author', $product_id ); $total = (float) $item->get_total(); $desc = sprintf( __( 'Refund for order #%d', 'vendorverse' ), $order_id ); VV_Helpers::record_transaction( $vendor_id, $total, 'debit', $desc, $product_id, $order_id ); VV_Helpers::flush_vendor_cache( $vendor_id ); } $this->remove_order( $order_id ); } public function track_cart( $cart_item_key, $product_id, $quantity, $variation_id, $variation, $cart_item_data ) { global $wpdb; $wpdb->insert( "{$wpdb->prefix}vv_cart_report", [ 'product_id' => (int) $product_id, 'user_id' => (int) get_current_user_id(), 'ip_address' => sanitize_text_field( $_SERVER['REMOTE_ADDR'] ?? '' ), ] ); } } new VV_Commission();