In this Article we'll take an in-depth look at the JavaScript needed to update the quantity of items in the Cart Layout.
Introduction
In this Article we'll take an in depth look at the JavaScript needed to update the Cart when the customer makes changes in the Cart itself. There are two main flows you can implement:
A second choice is between whether you want to reload the Page to get new values or have our JavaScript function live update them for you. We'll take a look at the key functions involved and the different ways they can be used within your Layout.
The s_e_cart_update_quantity[] function
What it does
The function will queue a change to the Cart in your local storage, but will not yet confirm the change- unless you explicitly ask it to.
Where it's commonly used
This function is designed to work as a callback function to Events triggered on the element which controls the quantity of items in the currently viewed Cart e.g. it could be added to this element in the Cart Layout item.liquid file.
Like so:
Arguments and Usage
You must pass the arguments 1-3. Argument 4 is optional.
s_e_cart_update_quantity[value, cart_id, token, update_entire_cart];
value - The quantity required of this Item. Usually set to event.target.value if the event was triggered on a target input element.
cart_id - Set to {{this.cart_data.cart_id}} to get the ID referring to the current Item in the item.liquid file.
token - Set to '{{context.authenticity_token}}'
update_entire_cart - Boolean - defaults to false. If you pass true, we'll automatically run the s_e_cart_update[] function [see below] as a further callback, confirming the Cart update immediately.
Your choice of which event is watched will affect the User Experience e.g. onkeyup as opposed to onchange. In conclusion, this function prepares an update to the Cart quantities. In the next section, we'll look at how to apply those changes.
The s_e_cart_update[] function
What it does
This function will apply the changes in the Cart quantities that you have already queued using the previously discussed s_e_cart_update_quantity[];
Where it's commonly used
You can either use this function directly to confirm Cart changes when the User is ready, or indirectly.
If true is passed for the 4th argument of the previously discussed s_e_cart_update_quantity[] function, we'll run this function immediately. In this case, you won't need to directly call this function yourself.
Normally this function will be used as a callback to an event triggered on a button on your Cart wrapper.liquid file. E.g.
Arguments and Usage
You must pass the arguments 1-2.
s_e_cart_update[reload, token];
reload - Boolean - true will refresh the Page after updating; false will not.
token - Set to '{{context.authenticity_token}}'
s_e_cart_update[false, '{{context.authenticity_token}}' ];
Live Updating Cart Values when Reload is set to
false
When passing in a false reload argument, you will need to follow additional steps to update the following:
The total quantity
The total price
The price of the line item that was just updated.
-optional- the currency symbol
Live Updating Total Quantity
You can use our Live Cart Update feature to get the new total quantity of items in the Cart. The s_e_cart_update[] function will automatically call the s_e_live_cart_update[] function for you- but you'll need to wrap your displayed total in a class with the following data-attribute: {{items_in_cart}}
Live Updating Total Price
In the wrapper.liquid file, wrap the following element and data-attribute around the Liquid value for the total Cart Price:
e.g.
Live Updating Item Price
In the item.liquid file for your Cart Layout, wrap the following element and data-attribute around the Liquid value for the item's price:
e.g.
It is necessary to add this item's Cart ID as the attribute's value, in order to locate the correct record within the Cart. This will continue to use the Liquid value on initial Page load, but will now replace it with the live value when needed.
Live Updating Currency - Optional
Depending on your HTML structure, you may already be displaying the currency symbol. If the JavaScript is overwriting it, you can add the following HTML to have the JavaScript return it when the function runs:
Troubleshooting Enter Button Presses
If you want to allow Users to press the enter button in the quantity input to trigger the event, you may need to modify your
wrapper.liquid file so that the Cart is no longer wrapped in a
element but a
If you want Users to be able to instantly update the Cart by changing values in the quantity input, you can run just the
s_e_cart_update_quantity[]; function with the 4th argument set to true. You may wish to
use the onkeyup event for even more instant feedback. If you want Users to have the option to confirm Cart Quantity updates before they are applied- you'll need to first run
s_e_cart_update_quantity[] when each input is changed and afterwards run
s_e_cart_update[] when the User is ready to confirm.Conclusion
How do you add items to cart in HTML?
How do I add a shopping cart to my HTML website?
Chủ Đề