123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293 |
- export const state = () => ({
- cart: [],
- cartLength: 0,
- price: 0,
- shippingPrice:0,
- estimatedDelivery: ''
- })
- export const actions = {
- addProductToCart ({ state, commit }, product) {
- const cartProduct = state.cart.find(prod => prod._id === product._id)
- if (!cartProduct) {
- commit('pushProductToCart', product)
- } else {
- commit('incrementProductQty', cartProduct)
- }
- commit('incrementCartLength')
- }
- }
- export const mutations = {
- pushProductToCart (state, product) {
- product.quantity = 1
- state.cart.push(product)
- },
- incrementProductQty (state, product) {
- product.quantity++
- let indexOfProduct = state.cart.indexOf(product)
- state.cart.splice(indexOfProduct, 1, product)
- },
- incrementCartLength (state) {
- state.cartLength = 0
- if (state.cart.length > 0) {
- state.cart.map(product => {
- state.cartLength += product.quantity
- })
- }
- },
- changeQty (state, { product, qty }) {
- let cartProduct = state.cart.find(prod => prod._id === product._id )
- cartProduct.quantity = qty
- state.cartLength = 0
- if (state.cart.length > 0) {
- state.cart.map(product => {
- state.cartLength += product.quantity
- })
- }
- let indexOfProduct = state.cart.indexOf(cartProduct)
- state.cart.splice(indexOfProduct, 1, cartProduct)
- },
- removeProduct (state, product) {
- state.cartLength -= product.quantity
- let indexOfProduct = state.cart.indexOf(product)
- state.cart.splice(indexOfProduct, 1)
- },
- setShipping (state, { price, estimatedDelivery }) {
- state.price = price
- state.estimatedDelivery = estimatedDelivery
- }
- }
- export const getters = {
- getCartLength (state) {
- return state.cartLength
- },
- getCart (state) {
- return state.cart
- },
- getCartTotalPrice (state) {
- let total = 0
- state.cart.map(product => {
- total += product.price * product.quantity
- })
- return total
- },
- getCartTotalPriceWithShipping (state) {
- let total = 0
- state.cart.map(product => {
- total += product.price * product.quantity
- })
- return total + state.shippingPrice
- }
- }
|