Skip to content

Instantly share code, notes, and snippets.

@cpv123
Last active October 5, 2021 04:57
Show Gist options
  • Save cpv123/38d52c477098597a0752fb062f1f782d to your computer and use it in GitHub Desktop.
Save cpv123/38d52c477098597a0752fb062f1f782d to your computer and use it in GitHub Desktop.
import { usePlausible } from "next-plausible"
import { commerce } from "libs/commercejs"
import { useCart } from "context/cart"
function AddToCartButton({ product, quantity }) {
const { setCart } = useCart() // <-- a custom hook to manage cart state
const plausible = usePlausible() // <-- access the plausible API
const handleAddToCart = async () => {
const { cart } = await commerce.cart.add(product.id, quantity)
setCart(cart)
plausible("addProductToCart", {
props: {
productName: product.name,
}
})
}
return (
<Button onClick={handleAddToCart}>Add to cart</Button>
)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment