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

@@ -599,6 +599,18 @@ export default function Checkout() {
setIsProcessing(true);
try {
const referralCode = (() => {
try {
const stored = localStorage.getItem('woonoow_ref')?.trim();
if (stored) return stored;
} catch {
// Ignore storage access errors.
}
const match = document.cookie.match(/(?:^|;\s*)woonoow_ref=([^;]+)/);
return match ? decodeURIComponent(match[1]).trim() : '';
})();
// Prepare order data
const orderData = {
items: cart.items.map(item => ({
@@ -652,6 +664,7 @@ export default function Checkout() {
custom_fields: customFieldData,
// CAPTCHA token for security validation
captcha_token: captchaToken,
referral_code: referralCode || undefined,
};
// Submit order
@@ -670,8 +683,16 @@ export default function Checkout() {
// If user was logged in during this request (guest auto-register),
// we need a full page reload to recognize the auth cookie
if (data.user_logged_in) {
// Get basePath from window
const basePath = (window as any).woonoowCustomer?.basePath ?? '/store';
// Ensure thankYouUrl starts with / if basePath doesn't end with /
const sep = basePath.endsWith('/') || thankYouUrl.startsWith('/') ? '' : '/';
const prefix = thankYouUrl.startsWith('/') && basePath.endsWith('/') ? basePath.slice(0, -1) : basePath;
// Full page reload so browser recognizes the new auth cookie
window.location.href = thankYouUrl;
const path = thankYouUrl.startsWith('/') ? thankYouUrl.slice(1) : thankYouUrl;
window.location.href = `${prefix}${sep}${path}`;
} else {
// Already logged in or no login happened - SPA navigate is fine
navigate(thankYouUrl, { replace: true });