Skip to content
This repository has been archived by the owner on Oct 31, 2024. It is now read-only.

Commit

Permalink
feat(theme): change product cart quantity
Browse files Browse the repository at this point in the history
  • Loading branch information
patzick committed Nov 29, 2019
1 parent b8a8bbd commit e03704f
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 5 deletions.
8 changes: 7 additions & 1 deletion packages/composables/src/hooks/useCart/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { ref, Ref, computed } from "@vue/composition-api";
import { getCart, addProductToCart, removeCartItem } from "@shopware-pwa/shopware-6-client";
import { getCart, addProductToCart, removeCartItem, changeCartItemQuantity } from "@shopware-pwa/shopware-6-client";
import { getStore } from "../..";

export const useCart = (): any => {
Expand Down Expand Up @@ -29,6 +29,11 @@ export const useCart = (): any => {
const result = await removeCartItem(id);
vuexStore.commit("SET_CART", result);
}

async function changeProductQuantity({ id, quantity }: any) {
const result = await changeCartItemQuantity(id, quantity);
vuexStore.commit("SET_CART", result);
}

const cart = computed(() => {
return vuexStore.getters.getCart;
Expand Down Expand Up @@ -58,6 +63,7 @@ export const useCart = (): any => {
cartItems,
addProduct,
removeProduct,
changeProductQuantity,
loading,
refreshCart,
error
Expand Down
20 changes: 16 additions & 4 deletions packages/default-theme/components/SwCartProduct.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
:title="product.label"
:regular-price="product.price.unitPrice | price"
:stock="product.deliveryInformation.stock"
v-model="product.quantity"
v-model="quantity"
@click:remove="removeProduct(product)"
class="collected-product"
>
Expand Down Expand Up @@ -39,6 +39,7 @@ import {
SfCollectedProduct
} from "@storefront-ui/vue";
import { useCart } from "@shopware-pwa/composables";
import {ref, watch} from "@vue/composition-api"
export default {
components: {
Expand All @@ -54,10 +55,21 @@ export default {
defult: () => ({})
}
},
setup () {
const { removeProduct } = useCart()
setup (props) {
const { removeProduct, changeProductQuantity } = useCart()
const quantity = ref(props.product.quantity)
watch(quantity, async qty => { // in future we may want to have debounce here
if (qty == props.product.quantity) return
await changeProductQuantity({id: props.product.id, quantity: qty})
})
watch(() => props.product.quantity, qty => {
quantity.value = qty
})
return {
removeProduct
removeProduct,
quantity
}
},
filters: {
Expand Down

0 comments on commit e03704f

Please sign in to comment.