Quantity add to cart html

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];

  1. value - The quantity required of this Item. Usually set to event.target.value if the event was triggered on a target input element.

  2. cart_id - Set to {{this.cart_data.cart_id}} to get the ID referring to the current Item in the item.liquid file.

  3. token - Set to '{{context.authenticity_token}}'

  4. 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.

  1. 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.

  2. 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];

  1. reload - Boolean - true will refresh the Page after updating; false will not.

  2. 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

instead. Make sure to adapt your stylesheets if you do this. We'd recommend against any solution which stops the default submit behaviour on the Cart buttons that should press on focus and enter keypress- as this will negatively impact Accessibility.

Conclusion

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.

How do you add items to cart in HTML?

Allow the user to empty the cart by one click..
Step 1: Creating a card. HTML: ... .
Step 2: Adding card header. HTML: ... .
Step 3: Adding a product details. HTML: ... .
Step 4: Creating a counter. HTML: ... .
Step 5: Adding a price section. HTML: ... .
Step 6: Duplicate cart item. ... .
Step 7: Creating a checkout section..

How do I add a shopping cart to my HTML website?

Navigate to Store settings → Embed options. Click the Buy Now button in the Choose Embed Type section. Select a product from the drop-down menu. Scroll down and copy the generated code from the Embed shopping cart window [the code will be the same for all products]

Chủ Đề