// resources/js/store/index.js
import { createStore } from 'vuex';
const store = createStore({
state: {
cart: [],
},
mutations: {
addToCart(state, product) {
const item = state.cart.find((item) => item.id === product.id);
if (item) {
item.quantity += 1;
} else {
state.cart.push({ ...product, quantity: 1 });
}
},
},
actions: {
addToCart({ commit }, product) {
commit('addToCart', product);
},
},
getters: {
cartItems(state) {
return state.cart;
},
cartTotal(state) {
return state.cart.reduce((total, item) => total + item.price * item.quantity, 0);
},
},
});
export default store;
This store manages the shopping cart state. It includes: