0 wins). if ($author_id > 0 && function_exists('aksrt_get_user_vip_level')) { $level = (int) aksrt_get_user_vip_level($author_id); if ($level === 1 && (float) ($settings['ratio_vip1'] ?? 0) > 0) { $ratio = (float) $settings['ratio_vip1']; } elseif ($level >= 2 && (float) ($settings['ratio_vip2'] ?? 0) > 0) { $ratio = (float) $settings['ratio_vip2']; } } return max(0.0, min(100.0, $ratio)); } /** * Guard: has this order already been settled into revenue? */ private function already_settled(string $order_number): bool { if ($order_number === '' || !class_exists('AKSRT_DB_Migrator')) { return false; } global $wpdb; $table = AKSRT_DB_Migrator::table('revenue'); if ($table === '') { return false; } $count = (int) $wpdb->get_var($wpdb->prepare( "SELECT COUNT(*) FROM {$table} WHERE order_number = %s AND type = %s", $order_number, 'order' )); return $count > 0; } /** * aksrt_order_paid listener: credit creators their share. * * @param string $order_number Paid order number. * @param array $order Order row (status=paid). */ public function handle_order_paid(string $order_number, array $order): void { if (!$this->is_enabled() || $order_number === '') { return; } if ($this->already_settled($order_number)) { return; } $buyer_id = (int) ($order['user_id'] ?? 0); $items = []; if (class_exists('AKSRT_Shop')) { $shop = AKSRT_Shop::get_instance(); if (method_exists($shop, 'get_order_items')) { $items = (array) $shop->get_order_items($order_number); } } if (empty($items)) { return; // Only structured shop-product orders settle creator revenue here. } $revenue = AKSRT_Revenue::get_instance(); foreach ($items as $item) { $product_id = (int) ($item['product_id'] ?? 0); $pay_price = (float) ($item['pay_price'] ?? $item['total_price'] ?? 0); if ($product_id <= 0 || $pay_price <= 0) { continue; } $author_id = (int) get_post_field('post_author', $product_id); if ($author_id <= 0 || $author_id === $buyer_id) { continue; // No author, or buyer is the author. } $ratio = $this->resolve_share_ratio($author_id); if ($ratio <= 0) { continue; } $amount = round($pay_price * $ratio / 100, 2); if ($amount <= 0) { continue; } $revenue->add($author_id, $amount, $order_number, 'order'); } } } }