tetchi blog

Tetchi's blog about life and stuff

Shopify Tutorial: Adding a Barchart for Inventory Using jQuery and JSON Objects


I was looking at t-shirts at an online store one day, and it had a nice little bar chart that displayed how much of each size it had left. I thought to myself, “I wonder if this is doable in Shopify?”. The answer is yes, thanks to jQuery and JSON Objects. In this tutorial, I’ll be showing you how to add a bar chart in your product page that shows how much of each variant you have left in a bar chart. A big thank you to Javascript wizard John Tajima for introducing me to JSON objects and helping me out with the Javascript portion of this tutorial.

Screen-shot-2009-11-22-at-6.13.52-PM

The bar chart integrated into the Moderno theme.

Implementing this feature can be done in three easy steps: adding the HTML/Liquid, adding the Javascript, and then adding the CSS. You can also fully customize the look of the bar chart with CSS. This bar chart will work for any theme, as long as you insert the HTML, Liquid, Javascript and CSS properly.

Keep in mind that this code will only work if you’re tracking the inventory levels of your product’s variant. In order to enable inventory tracking, you must navigate to your Product details page, and scroll down to where it lists the variants. Click the ‘edit’ link next to the variants, and in the dropdown menu, select “Shopify tracks this variant’s stock level”. Finally, enter the inventory level for this variant.

Screen shot 2009-12-10 at 12.09.34 AM

The way it works:

The algorithm is best explained with an example. You have a t-shirt with 3 variants: small, medium, large. Let’s say you have 2 smalls, 4 mediums, and 4 larges.

The Javascript code will first calculate the total inventory level for ALL variants – in this example, the total will be 10 (2 smalls + 4 mediums + 4 larges = 10). Then, for each variant, it will calculate the percentage of a single variant’s inventory level versus. the total inventory level. This percentage is then used to determine the width of each bar. The percentages in our t-shirt example would be 20% for small, 40% for medium, and 40% for large, and the CSS will fill in the black bars accordingly.

1. Download jQuery and put it in the theme.

If you are unsure of how to do this, please see Steps 1 to 3 from my last tutorial.

2. Insert the HTML/Liquid code inside of Product.liquid.

This is the code that is used to display the bar chart. Paste this code inside product.liquid where you want to output the bar chart.


{% if product.variants.size > 1 %}
  
{% for variant in product.variants %}
{{ variant.title }}
 
{% endfor %}
{% endif %}

The code above first checks if the product has more than one variant. The bar chart will only show if there is more than one variant, because there is not much point in making a bar chart if there is only one bar to show.

Next, the for-loop creates a div with the class “inventory-bar” for each variant. The “inventory-bar” div has a gray background has a width of 100% (which means that it will be the width of the “inventory” div. The div within “inventory-bar” is the black ‘filler’ bar that will expand its width depending on how much inventory for the variant is available. The filler bar is assigned a width in percentage by the Javascript in Step 3.

The next function then takes the inventory level of each variant, and assigns the CSS property ‘width’ in percentage. The percentage is calculated by dividing the variant’s inventory level by the total inventory, and then multiplying the result by 100. Using jQuery, the class variant_i (where ‘i’ is the index number of the loop) is assigned an inline style with the calculated width.

3. Insert the Javascript/jQuery code.

Below the code from Step 2, insert the following Javascript/jQuery code.




The code above first creates a JSON object of the product that you are currently viewing, using Liquid’s “json” filter. We want to convert the product into a JSON object because it will allow us to use Javascript for calculations (unfortunately, Liquid alone is not sufficient, as it does not have many math functions).

Next, it loops through each of the variants of the product, and calculates the total of all variant inventories and stores them in a variable called total_inventory.

The last function takes the inventory level of each variant, and assigns the CSS property ‘width’ in percentage. The percentage is calculated by dividing the variant’s inventory level by the total inventory, and then multiplying the result by 100. Using jQuery, the class variant_i (where ‘i’ is the index number of the loop) is assigned an inline style with the calculated width. The CSS class variant_i is matched up with the variant_{{forloop.index0}} in the for-loop from the HTML code in Step 2, which ensures that the right width is assigned to the right variant.

However, if the variant’s inventory level is 0, the bar for the variant will not be given a width, and instead it will display a message that says “Sold Out”.

4. Insert the CSS code.

Inside your CSS file, insert the following selectors for the bar chart.



#inventory{
        /*Give this div a width if you'd like to set the barcharts to a certain width. Otherwise, it will take on the width of its parent div. */
}

.filler{
	background:#000;
}

.inventory-item{
	margin-bottom:15px;
}

.inventory-bar{
	background:#e1e1e1;
	height:20px;
	width:100%;
}

The simple CSS above styles the bar chart. By using your own CSS, you can customize the color, margin, width, etc. in any way that you like.

Here are some examples of the bar charts in action, in different themes:

dropify2

The bar chart in the Dropify theme.

minimify

The bar chart in the Minimify theme.

Conclusion:

Originally, I had wanted to use just Liquid to accomplish this, but in the end I had to use a combination of Liquid and Javascript because Liquid cannot do many math functions. With that said I’m pretty happy that I used Javascript because I learned a great deal about JSON objects. (thanks again John!). I hope you find this technique useful, and I hope to see this in some themes in the future!

25 Comments

  • Jamie
    Jamie on December 10th, 2009

    Nice one Tetsuro!

  • Tetsuro
    Tetsuro on December 10th, 2009

    Thanks Jamie! :)

  • falcadia
    falcadia on January 27th, 2010

    Awesome. Simple and easy. thanks for the tutorial.

  • Tetsuro
    Tetsuro on April 26th, 2010

    Thank you Falcadia!

  • Alejandro
    Alejandro on April 29th, 2010

    sweet! I´m new to shopify and very much appreciate your work!

  • Tetsuro
    Tetsuro on May 12th, 2010

    Thanks Alejandro!

  • Ss
    Ss on January 6th, 2011

    Good shit, thank you sir!

  • Nope
    Nope on March 7th, 2011

    hmm, I’m trying something similar, but want to show the current inventory level compared to the original (say 5 remaining out of 50) because we are selling limited editions. I haven’t found support in Shopify for getting the original stock level, any ideas?

  • Victor
    Victor on April 8th, 2011

    Oh yes!I have a question Tetsuro! I really like your stock bars because it is something that visually looks very good for customers, but I would like to display a single bar showing the total of all the variants that are in stock, because I like visually only bar instead of a bar per variant. Any suggestions to help me? Thank you very much! I love your blog!

  • Tetsuro
    Tetsuro on April 8th, 2011

    Nope > Sorry for the late reply – been a little busy finishing school :P Is the 50 the total inventory for that one product? If so, you can use the ‘total_inventory’ variable that I created in the Javascript code above, and output it where you want it.

    Victor >Thanks for the kind words, Victor! Hmm, so you’re looking to do one bar that has ‘slices’ of each variant, correct? Or, could you perhaps show me an example of what you’re looking for?

  • Victor
    Victor on April 8th, 2011

    Hi Tetsuro, thanks for your answer.

    Lets say that for example I have 15 t´shirts on my store, 5 are red, 5 blue and 5 black.

    Right now I have one stock bar for each color product, so in my product page I see 3 bars, one for each product color.

    What I am trying to do is to have just one bar grouping the 3 product variants, so I only have one bar with the total of t-shirts available.

    Thanks!

  • Tetsuro
    Tetsuro on April 9th, 2011

    That should be doable very easily by modifying the CSS of the divs. You can float each ‘slice’ of the bar to the left so that they only fill up one bar. I’ll try something out on a test store, and get back to you.

  • Tetsuro
    Tetsuro on April 9th, 2011

    Sweet, check out my test store here. I’ve made one bar chart with divisions in it – is this what you were looking for? Let me know and I can help implement it to your store as well.

    There are 3 variants here: 2 small, 5 medium, and 3 large. The bar chart gets divided up into different colors proportionately.

  • Victor
    Victor on April 9th, 2011

    Nice Work Tetsuro!

    This is something I could use, but what I need it is a little different, I´m looking for how to make just one color bar with all the product inventory on it.

    The idea is to group the total of the inventory of a product (with different colors and sizes) in just one bar of one color.

    I don´t want to show the total of the product variants (color or sizes) with divisions on the bar, I need to show on the bar for the total of “shirts”.

    In your example, I would want to show the available stock of the total of 10 shirts. Not how many are available of each size.

    Is there any way to do this?

    Thank you!!

  • Tetsuro
    Tetsuro on April 9th, 2011

    Hmm, I thought that’s what I did in my example, I’m not sure if I get what you mean. Do you have an example of it somewhere that you can perhaps show me?

    Thanks,

    Tetsu

  • Victor
    Victor on April 11th, 2011

    Hello Tetsuro,

    It is something simpler than in your example, instead of showing a segmented bar for each product variant, should be a unique bar segment, showing the total of all remaining stock of the product without specify each variant inventory, you can see an example here: http://www.bonktown.com/

    You see, although there are several variants to choose sizes and colors, there is only one bar stock (Qty Remaining), just below the “Time Remaining”, with the total quantity available product. The bars of each variant are not needed, right below the video it would be like your first example.

    Thanks!

  • Tetsuro
    Tetsuro on April 11th, 2011

    Ah okay, now I see it. I’ll see what I can do when I get home tonight from school, but it’s def. possible!

  • Tetsuro
    Tetsuro on April 12th, 2011

    Actually, in order for this to work, you’ll need to define the total of each variant for each product, and this could be tricky if it’s different for all products. Notice that in BonkTown they have text indicating “Total Qty Offered: x”. You’ll have to define what this value is going to be before this can be implemented.

  • Gordon
    Gordon on February 9th, 2012

    Hi Tetsuro, nice work with the code.

    I prefer to use radio buttons to show the variants of my product. How can I move the bar chart for each variant to display next to the radio button for the variant?

    Thanks for your help

  • Marta
    Marta on April 24th, 2012

    Hi Tetsuro, thanks so much for the code.
    But of course I have a question too :)
    Do you by any chance know, what shall I add to this code so my barchart displays also quantity? Below or on top of the barchart?

  • tetchi
    tetchi on April 24th, 2012

    Sure Marta, shoot me over an email at tetsuro at shopify.com and I can take a look. It’s just a matter of outputting {{ variant.inventory_quantity }} in its appropriate location :P

  • Marcos
    Marcos on February 1st, 2014

    Hi,
    Awesome job, thats what I was searching.
    Could you give a small example of how to implement one bar with all the qantity stock?

  • Mahender
    Mahender on May 26th, 2015

    can i show this bar in %…

    Thanks

  • Keith
    Keith on July 13th, 2015

    I have a shopify metafield that holds a variants original inventory level. I want to track each variant independently of each other. If I want to return the starting inventory number of each variant, I use this: {{ original_count }}

    How would I insert this into the javascript to return a percentage for each variant, rather than variant level compared to whole product level?

  • MKSpdr
    MKSpdr on October 11th, 2015

    Mate you’re a star. Many thanks for this

Post a comment