123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051 |
- const state = {
- cart: [],
- total: 0
- }
- const getters = {
- getCartItems(state) {
- return state.cart
- },
- getTotal(state) {
- return state.total
- },
- isEmpty(state) {
- return state.cart.length !== 0
- }
- }
- const mutations = {
- addToCart(state, payload) {
- let finded = state.cart.find(item => item.id == payload.product.id)
- if (finded) {
- payload.product.qty = payload.product.qty + 1
- } else {
- state.cart = [payload.product, ...state.cart]
- }
- },
- calculateTotal(state) {
- let sum = 0
- state.cart.forEach(item => {
- sum = sum + (item.list_price * (item.qty || 1))
- })
- state.total = sum
- }
- }
- const actions = {
- addToCart({ commit }, payload) {
- commit('addToCart', payload)
- commit('calculateTotal')
- }
- }
- export default {
- state,
- getters,
- mutations,
- actions
- }
|