Fix: Ensure cart is completely cleared after payment success

ClearCartOnMount now explicitly deletes the IndexedDB storage in addition
to clearing the in-memory state. This prevents the cart from being
restored from IndexedDB on subsequent page loads after payment completes.

Fixes issue where cart items remained in bag after successful checkout.
This commit is contained in:
Andymick
2026-07-18 20:23:45 +01:00
parent 06cfe52a95
commit 43da81bd66
2 changed files with 21 additions and 7 deletions
+2 -1
View File
@@ -72,7 +72,8 @@
"Bash(git commit -m 'Fix: Safely parse designJson in admin order detail page *)", "Bash(git commit -m 'Fix: Safely parse designJson in admin order detail page *)",
"Bash(xargs grep -l \"design-spec\")", "Bash(xargs grep -l \"design-spec\")",
"Bash(git commit -m 'Improve: Design specification sheet now shows previews and info *)", "Bash(git commit -m 'Improve: Design specification sheet now shows previews and info *)",
"Bash(git commit -m 'Fix: Handle undefined designElements in design spec SVG rendering *)" "Bash(git commit -m 'Fix: Handle undefined designElements in design spec SVG rendering *)",
"Bash(git commit -m 'Fix: Ensure cart is completely cleared after payment success *)"
] ]
} }
} }
+19 -6
View File
@@ -2,17 +2,30 @@
import { useEffect } from 'react'; import { useEffect } from 'react';
import { useCart } from '@/lib/cartStore'; import { useCart } from '@/lib/cartStore';
import { del as idbDel } from 'idb-keyval';
export default function ClearCartOnMount() { export default function ClearCartOnMount() {
const clear = useCart((s) => s.clear); const clear = useCart((s) => s.clear);
useEffect(() => { useEffect(() => {
console.log('🧹 ClearCartOnMount: Clearing cart on success page'); const clearCart = async () => {
clear(); console.log('🧹 ClearCartOnMount: Clearing cart on success page');
// Also clear the isCheckingOut flag if it was set // Clear the in-memory state
if (typeof window !== 'undefined') { clear();
sessionStorage.removeItem('isCheckingOut'); // Also clear the IndexedDB storage to ensure it doesn't get restored
} try {
await idbDel('personalize-studio-cart');
console.log('✓ Cart cleared from IndexedDB');
} catch (err) {
console.error('Error clearing cart from IndexedDB:', err);
}
// Clear the isCheckingOut flag
if (typeof window !== 'undefined') {
sessionStorage.removeItem('isCheckingOut');
}
};
clearCart();
}, []); // Empty dependency array - run once on mount }, []); // Empty dependency array - run once on mount
return null; return null;