Add product to cart from anywhere

Make a global function that adds a product to the current cart
This commit is contained in:
2021-11-08 13:00:31 +01:00
parent 571f5d0a6d
commit 378a64f2b0
3 changed files with 13 additions and 34 deletions

View File

@@ -1,17 +1,18 @@
import { addNotification } from '$utils/functions/notifications'
import type { Writable } from 'svelte/store'
import { cartData } from '$utils/stores/shop'
/**
* Add a product to a cart
*/
export const addToCart = async (cart: string, cartData: Writable<any>, product: any, quantity: number = 1) => {
if (cart && cart !== 'null') {
export const addToCart = async (cartId: string, product: any, quantity: number = 1) => {
if (cartId && cartId !== 'null') {
const updatedCart = await fetch('/api/swell', {
method: 'POST',
body: JSON.stringify({
action: 'addToCart',
cartId: cart,
cartId,
productId: product.id,
quantity,
})