Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(headless): fix cart implementation when adding same item twice #4670

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,14 @@ export async function getCart(): Promise<CartItem[]> {

export async function addItemToCart(newItem: CartItem): Promise<CartItem[]> {
const cart = getCartFromCookies();
cart.push(newItem);
const existingItem = cart.find(
(item) => item.productId === newItem.productId
);
if (existingItem) {
existingItem.quantity += 1;
} else {
cart.push(newItem);
}
setCartInCookies(cart);
return cart;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export default function InstantProducts() {
const router = useRouter();

const {state, methods} = useInstantProducts();
const {methods: cartMethods} = useCart();
const {state: cartState, methods: cartMethods} = useCart();

const clickProduct = (product: Product) => {
methods?.interactiveProduct({options: {product}}).select();
Expand All @@ -24,7 +24,7 @@ export default function InstantProducts() {
<button onClick={() => clickProduct(product)}>
{product.ec_name} ({product.ec_product_id})
</button>
<button onClick={() => addToCart(cartMethods!, product)}>
<button onClick={() => addToCart(cartMethods!, cartState, product)}>
Add to cart
</button>
</li>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {useRouter} from 'next/navigation';

export default function ProductList() {
const {state, methods} = useProductList();
const {methods: cartMethods} = useCart();
const {state: cartState, methods: cartMethods} = useCart();

const router = useRouter();

Expand All @@ -32,10 +32,7 @@ export default function ProductList() {
height={50}
/>
</button>
<button onClick={() => addToCart(cartMethods!, product)}>
Add to cart
</button>
<button onClick={() => addToCart(cartMethods!, product)}>
<button onClick={() => addToCart(cartMethods!, cartState, product)}>
Add to cart
</button>
</li>
Expand Down
8 changes: 7 additions & 1 deletion packages/samples/headless-ssr-commerce/utils/cart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Cart as HeadlessCart,
CartItem as HeadlessCartItem,
Product as HeadlessProduct,
CartState as HeadlessCartState,
} from '@coveo/headless-react/ssr-commerce';

type HeadlessSSRCart = Omit<HeadlessCart, 'state' | 'subscribe'>;
Expand All @@ -24,13 +25,18 @@ export async function adjustQuantity(

export async function addToCart(
headlessCart: HeadlessSSRCart,
headlessCartState: HeadlessCartState,
product: HeadlessProduct
) {
const existingItem = headlessCartState.items.find(
(item) => item.productId === product.ec_product_id
);
const quantity = existingItem ? existingItem.quantity + 1 : 1;
const item = {
name: product.ec_name!,
price: product.ec_price!,
productId: product.ec_product_id!,
quantity: 1,
quantity: quantity,
};

headlessCart.updateItemQuantity(item);
Expand Down
Loading