Clear cart before checkout
Reset cart on plan purchase and enhance sync - Clear cart before starting checkout in PlansSection - Expand CART_QUERY to fetch full cart lines - Improve cart synchronization between local state and Shopify - Ensure checkout URL remains in sync after changes X-Lovable-Edit-ID: edt-2727f8c1-61ce-4ea7-8aa7-ce7473a0eb48
This commit is contained in:
@@ -53,7 +53,7 @@ const PlansSection = () => {
|
|||||||
const [checkoutLoading, setCheckoutLoading] = useState(false);
|
const [checkoutLoading, setCheckoutLoading] = useState(false);
|
||||||
|
|
||||||
const { products, isLoading: productsLoading } = useShopifyProducts("product_type:Abonnemang");
|
const { products, isLoading: productsLoading } = useShopifyProducts("product_type:Abonnemang");
|
||||||
const { addItem, getCheckoutUrl, isLoading: cartLoading } = useCartStore();
|
const { addItem, getCheckoutUrl, clearCart, isLoading: cartLoading } = useCartStore();
|
||||||
|
|
||||||
const handleSelectPlan = async (plan: typeof planDetails[0]) => {
|
const handleSelectPlan = async (plan: typeof planDetails[0]) => {
|
||||||
// Find the matching Shopify product
|
// Find the matching Shopify product
|
||||||
@@ -74,6 +74,9 @@ const PlansSection = () => {
|
|||||||
setSelectedPlan(plan.name);
|
setSelectedPlan(plan.name);
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
// Clear any existing cart items first for a clean checkout
|
||||||
|
clearCart();
|
||||||
|
|
||||||
await addItem({
|
await addItem({
|
||||||
product: shopifyProduct,
|
product: shopifyProduct,
|
||||||
variantId: variant.id,
|
variantId: variant.id,
|
||||||
|
|||||||
+23
-1
@@ -138,7 +138,29 @@ export const STOREFRONT_PRODUCTS_QUERY = `
|
|||||||
// Cart mutations
|
// Cart mutations
|
||||||
export const CART_QUERY = `
|
export const CART_QUERY = `
|
||||||
query cart($id: ID!) {
|
query cart($id: ID!) {
|
||||||
cart(id: $id) { id totalQuantity }
|
cart(id: $id) {
|
||||||
|
id
|
||||||
|
totalQuantity
|
||||||
|
checkoutUrl
|
||||||
|
lines(first: 100) {
|
||||||
|
edges {
|
||||||
|
node {
|
||||||
|
id
|
||||||
|
quantity
|
||||||
|
merchandise {
|
||||||
|
... on ProductVariant {
|
||||||
|
id
|
||||||
|
title
|
||||||
|
price {
|
||||||
|
amount
|
||||||
|
currencyCode
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
`;
|
`;
|
||||||
|
|
||||||
|
|||||||
+46
-3
@@ -208,15 +208,58 @@ export const useCartStore = create<CartStore>()(
|
|||||||
getCheckoutUrl: () => get().checkoutUrl,
|
getCheckoutUrl: () => get().checkoutUrl,
|
||||||
|
|
||||||
syncCart: async () => {
|
syncCart: async () => {
|
||||||
const { cartId, isSyncing, clearCart } = get();
|
const { cartId, isSyncing, clearCart, items } = get();
|
||||||
if (!cartId || isSyncing) return;
|
if (!cartId || isSyncing) return;
|
||||||
|
|
||||||
set({ isSyncing: true });
|
set({ isSyncing: true });
|
||||||
try {
|
try {
|
||||||
const data = await storefrontApiRequest(CART_QUERY, { id: cartId });
|
const data = await storefrontApiRequest(CART_QUERY, { id: cartId });
|
||||||
if (!data) return;
|
if (!data) {
|
||||||
|
set({ isSyncing: false });
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
const cart = data?.data?.cart;
|
const cart = data?.data?.cart;
|
||||||
if (!cart || cart.totalQuantity === 0) clearCart();
|
|
||||||
|
// Cart doesn't exist or is empty - clear local state
|
||||||
|
if (!cart || cart.totalQuantity === 0) {
|
||||||
|
clearCart();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Sync local items with Shopify cart
|
||||||
|
const shopifyLines = cart.lines?.edges || [];
|
||||||
|
|
||||||
|
// Build a map of Shopify line items by variant ID
|
||||||
|
const shopifyItemsMap = new Map<string, { lineId: string; quantity: number }>();
|
||||||
|
for (const edge of shopifyLines) {
|
||||||
|
const variantId = edge.node.merchandise.id;
|
||||||
|
shopifyItemsMap.set(variantId, {
|
||||||
|
lineId: edge.node.id,
|
||||||
|
quantity: edge.node.quantity
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update local items to match Shopify state
|
||||||
|
const syncedItems = items
|
||||||
|
.filter(item => shopifyItemsMap.has(item.variantId))
|
||||||
|
.map(item => {
|
||||||
|
const shopifyItem = shopifyItemsMap.get(item.variantId)!;
|
||||||
|
return {
|
||||||
|
...item,
|
||||||
|
lineId: shopifyItem.lineId,
|
||||||
|
quantity: shopifyItem.quantity
|
||||||
|
};
|
||||||
|
});
|
||||||
|
|
||||||
|
// Update checkout URL if available
|
||||||
|
const checkoutUrl = cart.checkoutUrl ? formatCheckoutUrl(cart.checkoutUrl) : get().checkoutUrl;
|
||||||
|
|
||||||
|
if (syncedItems.length === 0) {
|
||||||
|
clearCart();
|
||||||
|
} else {
|
||||||
|
set({ items: syncedItems, checkoutUrl });
|
||||||
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error('Failed to sync cart with Shopify:', error);
|
console.error('Failed to sync cart with Shopify:', error);
|
||||||
} finally {
|
} finally {
|
||||||
|
|||||||
Reference in New Issue
Block a user