Cart Store is designed to handle all actions related the shopping cart.
state: {
itemsAfterPlatformTotals: {},
platformTotals: null,
platformTotalSegments: null,
cartIsLoaded: false,
cartServerPullAt: 0,
cartServerTotalsAt: 0,
cartServerCreatedAt: 0,
cartSavedAt: new Date(),
bypassToAnon: false,
cartServerToken: '', // server side ID to synchronize with Backend (for example Magento)
shipping: [],
payment: [],
cartItems: [] // TODO: check if it's properly namespaced
},
Cart state is automatically loaded from localForage
collection after page has been loaded whenever core/components/blocks/Microcart.vue
is included. The cart state is loaded by dispatching cart/load
action and stored automatically by any change to the cart state.
The cart state data:
itemsAfterPlatformTotals
- helper collection, dictionary where the key is Magento's cart itemitem_id
that stores the totals information per item - received from Magento; it's automatically populated whenconfig.cart.synchronize_totals
is enabledplatformTotals
- similiary to above item, here we have the full totals from Magento for the current shopping cart. These collections are populated bycart/serverTotals
and the event handler forservercart-after-totals
cartIsLoaded
(bool) - true after dispatchingcart/load
cartServerPullAt
(int) - timestap for the last server cart synchronization set byservercart-after-pulled
- enabled whencart/synchronize
is set to true in the config,cartServerTotalsAt
- (int) timestamp for the latest server totals synchronziation set by 'servercart-after-totals`cartServerCreatedAt
- (int) timestamp for the last server cart id sync set byservercart-after-created
cartSavedAt
- (int) timestamp for the latest cart - localForage (local browser) state sync,bypassToAnon
- (bool) whenever there is a client's quote lock on the cart (for example the cart is currently in sync with Magento etc.) we're using the guest cart for current order; in that case this value is set to true,shipping
- (objeyct) currently selected shipping method - only when NOT usingcart.synchronize_totals
(if so, the shipping and payment's data comes from Magento2),payment
- (objeyct) currently selected shipping method - only when NOT usingcart.synchronize_totals
(if so, the shipping and payment's data comes from Magento2),cartItems
- collection of the cart items; the item format is the same as described in ElasticSearch Data formats - theproduct
class; the only difference is that the (int)qty
field is added
The following events are published from cart
store:
EventBus.$emit('cart-after-itemchanged', { item: cartItem })
- executed afterservercart-after-itemupdated
- after server cart sync, that sygnalize the specific shopping cart item has been changed;Microcart/Product.vue
component is subscribed to this event to refresh the shopping cart UIEventBus.$emit('cart-before-add', { product: item })
- fired after product has been added to the cart,EventBus.$emit('cart-before-save', { items: state.cartItems })
- fired after the product cart has been saved,EventBus.$emit('cart-before-delete', { items: state.cartItems })
- the event fired before the cart item is going to be deleted with the current cart state (before item is deleted)EventBus.$emit('cart-after-delete', { items: state.cartItems })
- the event fired before the cart item has been deleted with the current cart state (after item is deleted)EventBus.$emit('cart-before-itemchanged', { item: record })
- item called before the specific item properites are going to be changed; for example called whenservercart-after-itemupdated
is going to change theserver_item_id
propertyEventBus.$emit('cart-after-itemchanged', { item: record })
- item called after the specific item properites has been changed; for example called whenservercart-after-itemupdated
is going to change theserver_item_id
propertyEventBus.$emit('application-after-loaded')
- event called aftercart/load
action has been dispatched to notify that cart is being available,EventBus.$emit('cart-after-updatetotals', { platformTotals: totals, platformTotalSegments: platformTotalSegments })
- event called after the totals from Magento has been synchronized with current state; it's going to be emited only whencart.synchronize_totals
option is enabled.
The cart store provides following public actions:
Helper method used to clear the current server cart id (used for cart synchronization)
This method is called after order has been placed to empty the cartItems
collection and create the new server cart when the cart.synchronize_totals
is set to true
Method used to save the cart to the localForage
browser collection
This method is used to synchronize the current state of the cart items back and forth between server and current client state. When the forceClientState
is set to false the communication is one-way only (client->server). This aciton is called automatically on any shopping cart change when the cart.synchronize
is set to true.
Method is called whenever the cart totals should have been synchronized with the server (after serverPull
). This method overrides local shopping cart grand totals and specific item values (for example prices after discount).
Action is dispatched to create the server cart and store the cart id (for further synchronization)
Actions called whenever the shopping cart item should be synchronized with server side (pushes changes to the server). Basically this method is called within servercart-after-pulled
This method loads the cart items from localForage
browser state management.
This action is used for search the particular item in the shopping cart (by SKU)
This action is used to add the productToAdd
to the cart, if config.cart.synchronize
is set to true the next action subsequently called will be serverPull
to synchronize the cart. The event cart-before-add
is called whenever new product lands in the shopping cart. The option forceServerSilence
is used to bypass the server synchronization and it's used for example then the item is added during the ... sync process to avoid circullar synchronization cycles.
As you may imagine :) This action simply removes the product from the shopping cart and synchronizes the server cart when set. You must at least specify the product.sku
.
This method is called whenever user changes the qunatity of product in the cart (called from Microcart.vue
). The parameter qty
is the new quantity of product and by using forceServerSilence
you may controll if the server cart synchronization is being executed or not.
Gets a list of payment methods from the backend and saves them to cart.payment
store state.
Gets a list of shipping methods from the backed and saves them to cart.shipping
store state. Country ID is passed to this method in a mandatory address
parameter.
This method sends request to the backend to collect cart totals. It calls different backend endpoints depending on if payment and shipping methods information is available or not.
All state members should have been accessed only by getters. Please take a look at the state reference for data formats
const getters = {
current: (state) => state.current,
list: (state) => state.list
}