Updating Cart Content on a Shopify Theme with the Shopify Cart API
During the development process, I hit a wall figuring out how to update cart related content after a cart transaction. For example, after building the product page for my theme. I was left with changing the default behavior of the product page form. Each and every project out of Theme Kit starts out with boilerplate code. This code provides the bare minimum requirements for a theme to be functional. Anything less, and your theme will break.
By default, the form on product.liquid directs you to the cart.liquid page after submitting the form. Now, one of my main goals at the time was to have an accurate number of items on a cart without needing to reload the browser.
Before, following me through this walkthrough, I recommd that readers are familiar with the following:
- HTML + CSS
- JavaScript
- Liquid
- JQuery
- AJAX
- HTTP
- Theme Kit
No need for advanced mastery for any of these items, but I recommend for anyone reading to understand the basics for the languages.
Now, before getting started. I need to inform you guys that this work is all from a blank theme from Theme Kit. Before getting started, create a development store, and create a new theme.
- Create JavaScript files
index.jsinassetsfolder. - Copy and paste the latest minified JQuery script from JQuery CDN before the end of the closing
bodytag. - Add a script tag referencing your index file like so:
{{ 'index.js' | asset_url | script_tag }} - Change the code in the Cart link page to the following:
Feel free to add your own styling to this element. I turned the text red to make it stand out.
- Add a submssion event listener for the product page form. The anonymous function in the parameter will take the event object from the DOM.
$('form[action="/cart/add"]').on('submit', function(event){}); event.preventDefaultwill stop the browser from going to cart after the customer adds an item to cart. Understand. By preventing the default behavior of the form, we block the transfer of data. So, we have to create use AJAX code to update the cart.$('form[action="/cart/add"]').on('submit', function(event){});- So, now, its time to create a POST request to submit the data. For simplicity purposes, I will use the
ajaxmethod. Here's the setup. All you need is the API endpoint, serialized form data, and the type of HTTP request.
This is all you need really to update the cart. Now, how do we see the results - we have to make another request upon submitting the product page form in order to update the cart item count in the header.var serialized = $(this).serialize(); $.ajax({ url: '/cart/add.js', data: serialized, method: 'POST' })
Instead of a POST request, we must make a GET request to get the cart item count.
-
You wont be able to notice any difference. You have to add the
donemethod to get the object from the ajax method.
This piece of code tells the browser to make a request for the data. If the request is successful, convert the data into an JSON object, to get the object properties. From there, get the$.ajax({ url: '/cart/add.js', method: 'GET' }).done(function(data){ var cart = JSON.parse(data); $('[data-item-count]').text(cart.item_count + 1); });item_countproperty and update thespanelement.
$.ajax({
url: '/cart/add.js',
data: serialized,
method: 'GET'
})
The Final Result.
$('form[action="/cart/add"]').on('submit', function(event){
event.preventDefault();
var serialized = $(this).serialize();
$.ajax({
url: '/cart/add.js',
data: serialized,
method: 'POST'
})
.done(function(data){
var item = JSON.parse(data);
console.log(item.product_title + 'successfully added.')
});
$.ajax({
url: '/cart.js',
method: 'GET'
})
.done(function(data){
var cart = JSON.parse(data);
$('[data-item-count]').text(cart.item_count + 1);
});
});
Hopefully, this is helpful to someone who wants to learn more about Shopify's APIs. Now, that you know how to interact with Shopify's Cart API, you can create your own custom cart experience. Just know, that you must be aware of the solution that you build, and make sure it covers all types of scenarios. I recommend that you build your theme on top of Shopify's build tools code.










