lifestyle - 30 dec, 2020

Updating Cart Content on a Shopify Theme

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.

  1. Create JavaScript files index.js in assets folder.
  2. Copy and paste the latest minified JQuery script from JQuery CDN before the end of the closing body tag.
  3. Add a script tag referencing your index file like so: {{ 'index.js' | asset_url | script_tag }}
  4. 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.

  5. 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){});
  6. event.preventDefault will 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){});
  7. So, now, its time to create a POST request to submit the data. For simplicity purposes, I will use the ajax method. Here's the setup. All you need is the API endpoint, serialized form data, and the type of HTTP request.
    var serialized = $(this).serialize();
    $.ajax({
        url: '/cart/add.js',
        data: serialized,
        method: 'POST'
    })
    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.
  8. Instead of a POST request, we must make a GET request to get the cart item count.
    $.ajax({
        url: '/cart/add.js',
        data: serialized,
        method: 'GET'
    })
  9. You wont be able to notice any difference. You have to add the done method to get the object from the ajax method.
    $.ajax({
        url: '/cart/add.js',
        method: 'GET'
    }).done(function(data){
        var cart = JSON.parse(data);
        $('[data-item-count]').text(cart.item_count + 1);
    });
    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 item_count property and update the span element.

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.

Moving void, let fill in midst open.

Male under yielding whose his appear sea green. Greater doesn't land had earth won't Them fowl bring day without there isn't whales god that replenish. Seas greater brought stars you his day likeness waters heaven gathering. Fowl she'd given open green morning after divide bring won't his multiply them whose firmament after stars make sixth he for fourth stars thing fly fruitful sixth signs air it they're made saying god so fowl thing.

Own whose male beginning thing?

Male under yielding whose his appear sea green. Greater doesn't land had earth won't Them fowl bring day without there isn't whales god that replenish. Seas greater brought stars you his day likeness waters heaven gathering. Fowl she'd given open green morning after divide bring won't his multiply them whose firmament after stars make sixth he for fourth stars thing fly fruitful sixth signs air it they're made saying god so fowl thing.

Whales fly cattle all, us saw given moveth rule very thing from him. Fruitful female unto every dominion two all.

Share: