cart.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. const state = {
  2. cart: [],
  3. total: 0,
  4. productToDiscount: null
  5. }
  6. const getters = {
  7. cartItems(state) {
  8. return state.cart
  9. },
  10. total(state) {
  11. return state.total
  12. },
  13. cartIsEmpty(state) {
  14. return state.cart.length !== 0
  15. },
  16. productToDiscount(state) {
  17. return state.productToDiscount
  18. }
  19. }
  20. const mutations = {
  21. addToCart(state, payload) {
  22. let finded = state.cart.find(item => item.id == payload.product.id)
  23. if (finded) {
  24. payload.product.qty = payload.product.qty + 1
  25. } else {
  26. state.cart = [payload.product, ...state.cart]
  27. }
  28. },
  29. subtractFromCart(state, payload) {
  30. let finded = state.cart.find(item => item.id == payload.product.id)
  31. finded.qty = finded.qty - 1
  32. },
  33. discountFromCart(state, payload) {
  34. state.productToDiscount = payload
  35. },
  36. applyDiscount(state, payload) {
  37. let finded = state.cart.find(item => item.id == state.productToDiscount.id)
  38. finded.price = state.productToDiscount.discount
  39. },
  40. removeFromCart(state, payload) {
  41. let findedIndex = state.cart.findIndex(item => item.id == payload.product.id)
  42. state.cart.splice(findedIndex, 1)
  43. },
  44. calculateTotal(state) {
  45. let sum = 0
  46. state.cart.forEach(item => {
  47. sum = sum + ((item.price || item.list_price) * (item.qty || 1))
  48. })
  49. state.total = sum
  50. }
  51. }
  52. const actions = {
  53. addToCart({ commit, dispatch }, payload) {
  54. commit('addToCart', {
  55. product: payload
  56. })
  57. commit('calculateTotal')
  58. // dispatch('setSelectedProduct', {
  59. // product: null
  60. // })
  61. },
  62. subtractFromCart({ commit, dispatch }, payload) {
  63. if (payload.qty > 1) {
  64. commit('subtractFromCart', {
  65. product: payload
  66. })
  67. } else {
  68. commit('removeFromCart', {
  69. product: payload
  70. })
  71. }
  72. commit('calculateTotal')
  73. },
  74. discountFromCart({ commit, dispatch }, payload) {
  75. if (payload && !payload.minimum_price && !payload.maximum_price) {
  76. dispatch('notify', 'No hay descuento para este producto')
  77. return
  78. }
  79. commit('discountFromCart', payload)
  80. },
  81. applyDiscount({ getters, commit, dispatch }, payload) {
  82. if(payload.apply) {
  83. let product = getters.productToDiscount
  84. if (product.priceApply < product.minimum_price || product.priceApply > product.maximum_price) {
  85. dispatch('notify', 'No se puede aplicar este descuento')
  86. return
  87. }
  88. commit('applyDiscount', product.id)
  89. commit('calculateTotal')
  90. }
  91. dispatch('discountFromCart', null)
  92. },
  93. removeFromCart({ commit, dispatch }, payload) {
  94. commit('removeFromCart', {
  95. product: payload
  96. })
  97. commit('calculateTotal')
  98. }
  99. }
  100. export default {
  101. state,
  102. getters,
  103. mutations,
  104. actions
  105. }