Build the Salar Store storefront and connect products, blogs, and home specials to the Meshkee API.
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
co-authored by
Cursor
parent
2783305f07
commit
7d26eaa838
+39
@@ -0,0 +1,39 @@
|
||||
export type CartItem = {
|
||||
productId: string;
|
||||
variantId?: string;
|
||||
slug: string;
|
||||
name: string;
|
||||
image: string;
|
||||
price: number;
|
||||
qty: number;
|
||||
};
|
||||
|
||||
const KEY = "salar-cart";
|
||||
|
||||
export function getCart(): CartItem[] {
|
||||
if (typeof window === "undefined") return [];
|
||||
try {
|
||||
const raw = window.localStorage.getItem(KEY);
|
||||
return raw ? (JSON.parse(raw) as CartItem[]) : [];
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export function cartCount() {
|
||||
return getCart().reduce((sum, item) => sum + item.qty, 0);
|
||||
}
|
||||
|
||||
export function addToCart(item: CartItem) {
|
||||
const cart = getCart();
|
||||
const index = cart.findIndex(
|
||||
(entry) => entry.productId === item.productId && entry.variantId === item.variantId,
|
||||
);
|
||||
if (index >= 0) {
|
||||
cart[index] = { ...cart[index], qty: cart[index].qty + item.qty };
|
||||
} else {
|
||||
cart.push(item);
|
||||
}
|
||||
window.localStorage.setItem(KEY, JSON.stringify(cart));
|
||||
window.dispatchEvent(new Event("cart-updated"));
|
||||
}
|
||||
Reference in New Issue
Block a user