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,7 +1,7 @@
<script lang="ts"> <script lang="ts">
import { capitalizeFirstLetter } from '$utils/functions'
import { cartId, cartData } from '$utils/stores/shop' import { cartId, cartData } from '$utils/stores/shop'
import { addNotification } from '$utils/notifications' import { addToCart } from '$utils/functions/shop'
import { capitalizeFirstLetter } from '$utils/functions'
// Components // Components
import Button from '$components/atoms/Button.svelte' import Button from '$components/atoms/Button.svelte'
import Image from '$components/atoms/Image.svelte' import Image from '$components/atoms/Image.svelte'
@@ -55,33 +55,6 @@
ratio: 0.68, ratio: 0.68,
}, },
] ]
/**
* Handling add to cart
*/
const addToCart = async () => {
const updatedCart = await fetch('/api/swell', {
method: 'POST',
body: JSON.stringify({
action: 'addToCart',
cartId: $cartId,
productId: product.product_id,
quantity: 1,
})
})
if (updatedCart.ok) {
const newCart = await updatedCart.json()
$cartData = newCart
// Show notification
addNotification({
title: 'Added to cart',
name: `${productShop.name} - x1`,
image: productShop.images[0].file.url,
})
}
}
</script> </script>
<section class="poster-layout grid"> <section class="poster-layout grid">
@@ -98,7 +71,7 @@
<Button <Button
text="Add to cart" text="Add to cart"
color="pinklight" color="pinklight"
on:click={addToCart} on:click={() => addToCart($cartId, shopProduct)}
/> />
</div> </div>

View File

@@ -1,7 +1,11 @@
<script lang="ts"> <script lang="ts">
import { cartId } from '$utils/stores/shop'
import { addToCart } from '$utils/functions/shop'
// Components
import Button from '$components/atoms/Button.svelte' import Button from '$components/atoms/Button.svelte'
import Image from '$components/atoms/Image.svelte' import Image from '$components/atoms/Image.svelte'
export let product: any
export let location: { name: string, slug: string } export let location: { name: string, slug: string }
export let image: any export let image: any
</script> </script>
@@ -33,6 +37,7 @@
text="Add to cart" text="Add to cart"
color="pinklight" color="pinklight"
size="xsmall" size="xsmall"
on:click={() => addToCart($cartId, product)}
/> />
</div> </div>
</div> </div>

View File

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