Skip to content

Commit

Permalink
docs: useAddToCart composable (SWF-194)
Browse files Browse the repository at this point in the history
  • Loading branch information
mkucmus committed Dec 22, 2022
1 parent 71b7cf9 commit 4fc1cd8
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 7 deletions.
6 changes: 6 additions & 0 deletions .changeset/rare-bags-remain.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"docs": patch
"@shopware-pwa/composables-next": patch
---

Explains an usage of useAddToCart composable
22 changes: 16 additions & 6 deletions apps/docs/src/packages/composables/useAddToCart.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,26 @@
category: CMS
---

<script setup>
import StackBlitzLiveExample from '../../components/StackBlitzLiveExample.vue'
</script>

# useAddToCart

Add product to cart.

[[toc]]

## Usage

```ts
const { isInCart, quantity, addToCart } = useAddToCart({ product });
if (!isInCart.value) {
quantity.value = 5;
await addToCart();
}
Provided `product` object in the argument should be in a `Ref<Product>` type.

```vue
<script setup lang="ts">
const { isInCart, quantity, addToCart, getStock } = useAddToCart({ product });
</script>
```

## Live example

<StackBlitzLiveExample projectId="mkucmus/frontends-examples" example="UseAddToCart" />
3 changes: 2 additions & 1 deletion packages/composables/src/useAddToCart.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ export type UseAddToCartReturn = {
export function useAddToCart(product: Ref<Product>): UseAddToCartReturn {
const _product = computed(() => unref(product));

const { addProduct, cartItems } = useCart();
const { addProduct, cartItems, refreshCart } = useCart();
const quantity: Ref<number> = ref(1);

async function addToCart(): Promise<Cart> {
Expand All @@ -38,6 +38,7 @@ export function useAddToCart(product: Ref<Product>): UseAddToCartReturn {
quantity: quantity.value,
});
quantity.value = 1;
refreshCart();
return addToCartResponse;
}

Expand Down

0 comments on commit 4fc1cd8

Please sign in to comment.