Files
payloadcms/test/plugin-ecommerce/app/components/Cart.tsx
Paul ef4874b9a0 feat: ecommerce plugin and template (#8297)
This PR adds an ecommerce plugin package with both a Payload plugin and
React UI utilities for the frontend. It also adds a new ecommerce
template and new ecommerce test suite.

It also makes a change to the `cpa` package to accept a `--version` flag
to install a specific version of Payload defaulting to the latest.
2025-09-29 20:05:16 -04:00

59 lines
1.6 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
'use client'
import { useCart, useCurrency } from '@payloadcms/plugin-ecommerce/react'
export const Cart = () => {
const { cart, incrementItem, decrementItem, removeItem, subTotal, clearCart } = useCart()
const { formatCurrency } = useCurrency()
return (
<div>
<h1>Cart Component</h1>
<p>This is a placeholder for the Cart component.</p>
<p>subTotal: {formatCurrency(subTotal)}</p>
{cart && cart.size > 0 ? (
<ul>
{Array.from(cart.values()).map((item, index) => {
const id = item.variantID || item.productID
const options =
item.variant?.options && item.variant.options.length > 0
? item.variant.options
.filter((option) => typeof option !== 'string')
.map((option) => {
return option.label
})
: []
return (
<li key={id}>
<h2>
{item.product.name} {options.length > 0 ? `(${options.join(' ')})` : ''}
</h2>
<p>Quantity: {item.quantity}</p>
<button onClick={() => incrementItem(id)}>+</button>
<button onClick={() => decrementItem(id)}>-</button>
<button onClick={() => removeItem(id)}>Remove</button>
</li>
)
})}
</ul>
) : (
<p>Your cart is empty.</p>
)}
<button
onClick={() => {
clearCart()
}}
>
Clear all
</button>
{/* <pre>{JSON.stringify(Array.from(cart.entries()), null, 2)}</pre> */}
</div>
)
}