mirror of
https://github.com/vercel/commerce.git
synced 2025-05-19 16:07:01 +00:00
feat(poc): improve addToCart, remove warn
This commit is contained in:
parent
0bd4c18958
commit
66343b5e73
@ -104,7 +104,11 @@ export default async function ProductPage({ params }: { params: { handle: string
|
||||
<Prose className="mb-6 text-sm leading-tight" html={product.descriptionHtml} />
|
||||
) : null}
|
||||
|
||||
<AddToCart product={product} variants={product.variants} availableForSale={true} />
|
||||
<AddToCart
|
||||
product={product}
|
||||
variants={product.variants}
|
||||
availableForSale={product.availableForSale}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
<Suspense>
|
||||
|
@ -2,7 +2,7 @@
|
||||
|
||||
import { ApiClientError } from '@shopware/api-client';
|
||||
import { getApiClient } from 'lib/shopware/api';
|
||||
import { ExtendedCart } from 'lib/shopware/api-extended';
|
||||
import { ExtendedCart, ExtendedLineItem } from 'lib/shopware/api-extended';
|
||||
import { cookies } from 'next/headers';
|
||||
|
||||
export const fetchCart = async function (cartId?: string): Promise<ExtendedCart | undefined> {
|
||||
@ -29,12 +29,23 @@ export const addItem = async (variantId: string | undefined): Promise<Error | un
|
||||
}
|
||||
|
||||
try {
|
||||
let quantity = 1;
|
||||
const apiClient = getApiClient(cartId);
|
||||
|
||||
// this part allows us to click multiple times on addToCart and increase the qty with that
|
||||
const cart = await fetchCart(cartId);
|
||||
const itemInCart = cart?.lineItems?.filter((item) => item.id === variantId) as
|
||||
| ExtendedLineItem
|
||||
| undefined;
|
||||
if (itemInCart && itemInCart.quantity) {
|
||||
quantity = itemInCart.quantity + 1;
|
||||
}
|
||||
|
||||
apiClient.invoke('addLineItem post /checkout/cart/line-item', {
|
||||
items: [
|
||||
{
|
||||
id: variantId,
|
||||
quantity: 1,
|
||||
quantity: quantity,
|
||||
referencedId: variantId,
|
||||
type: 'product'
|
||||
}
|
||||
|
@ -39,7 +39,6 @@ export default function CartModal({ cart, cartIdUpdated }: { cart: Cart; cartIdU
|
||||
}, [setCookie, cartIdUpdated, cart.id]);
|
||||
|
||||
useEffect(() => {
|
||||
console.warn('cart modal', cart);
|
||||
// Open cart modal when when quantity changes.
|
||||
if (cart.totalQuantity !== quantityRef.current) {
|
||||
// But only if it's not already open (quantity also changes when editing items in cart).
|
||||
|
Loading…
x
Reference in New Issue
Block a user