"only 1 left" feature

Nikos1
Shopify Partner
17 0 5

Hello community,

The following snippet allows you to show when there is only one stock item left from your products. Note that the snippet considers all different variants of the product. I thought to share it with you in case someone looks for something similar. I have added the snippet into the "product-grid-item.liquid" file so that it can appear when someone browses through my collections. 

Cheers!

          	  {% assign stock = 0 %}              
              {% for variant in product.variants %}
              {%capture stock %}
              {{ stock | plus:variant.inventory_quantity }}
              {% endcapture %}
              {% endfor %}
              
              {%assign stock = stock | times:1%}

			  {% if stock ==1 %}
             <span class="label inventory"> Only {{stock}} left! </span>
			  {% endif %}

 

Dress your attitude
Cyndi_Roberts
New Member
2 0 0

Thank you so much for sharing this! Worked perfectly for me.

0 Likes
Eric_Seastrand
Shopify Partner
42 3 36

Thanks for this! Super useful.

 

Tip for others: If you're looking for an "only 9 left" feature (same thing, but kicks in at a higher inventory count), you can change this line:

{% if stock ==1 %}

to this (10 can be any other number)

{% if stock < 10 %}

 

0 Likes
AliceE
New Member
3 0 0

Hello, I used the code but for some reason it isnt working, is it up to date, where in the section should it be placed? Thank you

0 Likes
Eric_Seastrand
Shopify Partner
42 3 36

For us, it works. Here's what the code looks like in our product-template.liquid (highlighted section)

This is in the "stock" Debut theme with minimal modifications, but should work in other themes too.

 

image.png

0 Likes
AliceE
New Member
3 0 0

Thank you, I am using Brooklyn, not quite sure why it isn't working but thank you, will see if I need to do anything else on inventory tracking. 

0 Likes
fatima2020
New Member
1 0 0
i use Debut theme so I put the following code in your theme.js file at the very bottom and watch the magic!!!

@AliceE wrote:

Thank you, I am using Brooklyn, not quite sure why it isn't working but thank you, will see if I need to do anything else on inventory tracking. 



jQuery(function() {    $('.single-option-selector').on('change', function(){        setTimeout(function(){ 
          if($('[name="id"]').val() != "{{ product.selected_or_first_available_variant.id }}"){            location.reload();
          }
        }, 1);
    });
  });

 

0 Likes
anq123
New Member
1 0 0
Hi, 

Thank you so much for the code. Where in the product-template liquid should you add the below code? 

Thanks, 
Alexa



 

0 Likes
mvvecommerce1
Tourist
4 0 1

Hi, I tried to add the code in my Debut theme.js file in product.liquid at the bottom and I can't see any chance - can you advise?

0 Likes
mvvecommerce1
Tourist
4 0 1

Hi there,

Thanks for this it's amazing! Question though.. what if I have the site in more than one language? how can I make sure the "only 1 left" changes according to the language selected?

Thank you

0 Likes