fix(affiliate): persist referral code through SPA checkout and process after save

This commit is contained in:
Dwindi Ramadhana
2026-06-01 00:58:10 +07:00
parent 6d2b1fb9ca
commit 5b8882e595
3 changed files with 75 additions and 1 deletions

View File

@@ -329,6 +329,11 @@ class CheckoutController
return ['error' => $order->get_error_message()];
}
if (!empty($payload['referral_code'])) {
$order->update_meta_data('_woonoow_referral_code', $payload['referral_code']);
$_COOKIE['woonoow_ref'] = $payload['referral_code'];
}
// Track if user was logged in during this request (for frontend page reload)
$user_logged_in = false;
@@ -516,6 +521,11 @@ class CheckoutController
$order->save();
// wc_create_order() fires woocommerce_new_order before SPA checkout line
// items exist. Fire the processed hook after saving so integrations that
// calculate from order items, like affiliate tracking, see a complete order.
do_action('woocommerce_checkout_order_processed', $order->get_id(), $payload, $order);
if (!headers_sent()) {
header('Server-Timing: app;dur=' . round((microtime(true) - $__t0) * 1000, 1));
}
@@ -842,6 +852,7 @@ class CheckoutController
'shipping_title' => isset($json['shipping_title']) ? sanitize_text_field($json['shipping_title']) : null,
'custom_fields' => $custom_fields,
'customer_note' => isset($json['customer_note']) ? sanitize_textarea_field($json['customer_note']) : '',
'referral_code' => isset($json['referral_code']) ? sanitize_text_field($json['referral_code']) : '',
];
}