[Vuejs]-How to add product into the cart according to the stock in vue

0👍

It looks like the mutation’s product argument contains the stock count, so you could perform the check in the mutation:

export const ADD_TO_CART = (state, {product, variation, amount}) => {
    if (amount > product.attributes.stock) {
      return
    }
    ⋮
    // add to cart
}

Or you could do it in the Product component before dispatching:

export default {
    methods: {
        addToCart: function () {
            let amount = this.itemsCount !== "" ? this.itemsCount : 1;
            if (amount > this.product.attributes.stock) {
                return;
            }
            ⋮
            // dispatch action
        },
    }
}

Leave a comment