mirror of
https://github.com/vercel/commerce.git
synced 2025-09-07 16:30:17 +00:00
feat(cart): cart is now based on store configuration
This commit is contained in:
@@ -2,36 +2,42 @@
|
||||
|
||||
import { TAGS } from 'lib/constants';
|
||||
import { addToCart, createCart, getCart, removeFromCart, updateCart } from 'lib/shopify';
|
||||
import { Store } from 'lib/shopify/types';
|
||||
import { revalidateTag } from 'next/cache';
|
||||
import { cookies } from 'next/headers';
|
||||
|
||||
export async function addItem(prevState: any, selectedVariantId: string | undefined) {
|
||||
export async function addItem(
|
||||
prevState: any,
|
||||
payload: { selectedVariantId: string | undefined; store: Store }
|
||||
) {
|
||||
let cartId = cookies().get('cartId')?.value;
|
||||
let cart;
|
||||
|
||||
if (cartId) {
|
||||
cart = await getCart(cartId);
|
||||
cart = await getCart(payload.store, cartId);
|
||||
}
|
||||
|
||||
if (!cartId || !cart) {
|
||||
cart = await createCart();
|
||||
cart = await createCart(payload.store);
|
||||
cartId = cart.id;
|
||||
cookies().set('cartId', cartId);
|
||||
}
|
||||
|
||||
if (!selectedVariantId) {
|
||||
if (!payload.selectedVariantId) {
|
||||
return 'Missing product variant ID';
|
||||
}
|
||||
|
||||
try {
|
||||
await addToCart(cartId, [{ merchandiseId: selectedVariantId, quantity: 1 }]);
|
||||
await addToCart(payload.store, cartId, [
|
||||
{ merchandiseId: payload.selectedVariantId, quantity: 1 }
|
||||
]);
|
||||
revalidateTag(TAGS.cart);
|
||||
} catch (e) {
|
||||
return 'Error adding item to cart';
|
||||
}
|
||||
}
|
||||
|
||||
export async function removeItem(prevState: any, lineId: string) {
|
||||
export async function removeItem(prevState: any, payload: { lineId: string; store: Store }) {
|
||||
const cartId = cookies().get('cartId')?.value;
|
||||
|
||||
if (!cartId) {
|
||||
@@ -39,7 +45,7 @@ export async function removeItem(prevState: any, lineId: string) {
|
||||
}
|
||||
|
||||
try {
|
||||
await removeFromCart(cartId, [lineId]);
|
||||
await removeFromCart(payload.store, cartId, [payload.lineId]);
|
||||
revalidateTag(TAGS.cart);
|
||||
} catch (e) {
|
||||
return 'Error removing item from cart';
|
||||
@@ -52,6 +58,7 @@ export async function updateItemQuantity(
|
||||
lineId: string;
|
||||
variantId: string;
|
||||
quantity: number;
|
||||
store: Store;
|
||||
}
|
||||
) {
|
||||
const cartId = cookies().get('cartId')?.value;
|
||||
@@ -60,16 +67,16 @@ export async function updateItemQuantity(
|
||||
return 'Missing cart ID';
|
||||
}
|
||||
|
||||
const { lineId, variantId, quantity } = payload;
|
||||
const { lineId, variantId, quantity, store } = payload;
|
||||
|
||||
try {
|
||||
if (quantity === 0) {
|
||||
await removeFromCart(cartId, [lineId]);
|
||||
await removeFromCart(store, cartId, [lineId]);
|
||||
revalidateTag(TAGS.cart);
|
||||
return;
|
||||
}
|
||||
|
||||
await updateCart(cartId, [
|
||||
await updateCart(store, cartId, [
|
||||
{
|
||||
id: lineId,
|
||||
merchandiseId: variantId,
|
||||
|
Reference in New Issue
Block a user