get_var($wpdb->prepare( "SELECT id FROM {$table} WHERE order_number = %s LIMIT 1", $order_number )); return !empty($found); } } if (!function_exists('aksrt_burn_order_coupon')) { /** * Burn coupon for a paid cash order (use_coupon + log_usage). * * @param array $meta Order note JSON (coupon_code / coupon / coupon_discount). * @return bool True when a burn was attempted (or already burned). */ function aksrt_burn_order_coupon(string $order_number, array $meta, int $buyer_id = 0): bool { if ($order_number === '' || !class_exists('\AKSRT\Payment\Coupon')) { return false; } if (aksrt_coupon_usage_exists($order_number)) { return true; } $coupon = is_array($meta['coupon'] ?? null) ? $meta['coupon'] : []; $coupon_code = sanitize_text_field((string) ($coupon['code'] ?? $meta['coupon_code'] ?? '')); $coupon_discount = (float) ($meta['coupon_discount'] ?? $meta['discount'] ?? ($coupon['discount'] ?? 0)); if ($coupon_code === '') { return false; } $coupon_api = \AKSRT\Payment\Coupon::get_instance(); $coupon_api->use_coupon($coupon_code); // Prefer snapshot id: post-increment validate may fail on usage_limit edge. $coupon_id = (int) ($coupon['id'] ?? 0); if ($coupon_id <= 0) { $validated = $coupon_api->validate($coupon_code, max(0.01, $coupon_discount), [ 'user_id' => max(0, $buyer_id), ]); $coupon_id = (int) ($validated['coupon']['id'] ?? 0); } if ($coupon_id > 0) { $coupon_api->log_usage($coupon_id, max(0, $buyer_id), $order_number, max(0.0, $coupon_discount)); } return true; } }