PHP Classes

File: resources/js/store/modules/Cart.js

Recommend this page to a friend!
  Classes of Edward Paul   Flexible PHP Coupon System   resources/js/store/modules/Cart.js   Download  
File: resources/js/store/modules/Cart.js
Role: Auxiliary data
Content type: text/plain
Description: Auxiliary data
Class: Flexible PHP Coupon System
E-commerce system that supports discount coupons
Author: By
Last change:
Date: 3 years ago
Size: 2,418 bytes
 

Contents

Class file image Download
import api from '../api/all' import coupon from './Coupon' import flash from './FlashMessage'; const state = { cart: [] }; const getters = { allCart : state => { return state.cart; }, cartItemCount : state => { return state.cart.length }, cartTotal: state => { return (state.cart.reduce((a, b) => { return a + b.product.price * b.quantity }, 0) - coupon.state.coupon.value).toFixed(2) } }; const actions = { getAllCarts({commit}) { api.getAllCart().then((response)=>{ commit('setCarts', response.data) }) }, addProductToCart({commit, dispatch}, { product, quantity }){ api.addProductToCart({product, quantity}).then(()=>{ commit('clearCoupon'); commit('appendToCart', { product, quantity }); dispatch('flashMessage', 'Item added to cart') }).catch(()=>{ dispatch('flashMessage', 'Error, Item Not Added To Cart') }); }, removeProductFromCart({ commit }, productId) { commit('clearCoupon'); commit('removeFromCart', productId); api.removeProductFromCart(productId) }, removeAllProductsFromCart({commit, dispatch}){ commit('clearCart'); commit('clearCoupon'); api.removeAllProductsFromCart(); dispatch('flashMessage', 'All items removed') } }; const mutations = { setCarts(state, cart){ state.cart = cart }, appendToCart(state, {product, quantity}){ const existing = state.cart.find((item) => { return item.product.id === product.id }); if (existing) { existing.quantity++ } else { state.cart.push({ product, quantity: quantity }) } }, removeFromCart(state, productId){ const existing = state.cart.find((item) => { return item.product.id === productId }); if (existing.quantity > 1) { existing.quantity-- } else { state.cart = state.cart.filter((item) => { return item.product.id !== productId }) } }, clearCart(state){ state.cart = [] } }; const modules ={ coupon:coupon, flash: flash }; export default { state, getters, mutations, actions, modules }