Last active
October 5, 2021 04:57
-
-
Save cpv123/38d52c477098597a0752fb062f1f782d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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