Add "Stock available: #" on product page (Empire Theme)

Highlighted
New Member
3 0 0

Hello, I've been trying to add a "stock available" on the product page of Empire theme with no success.

 

I had the Porto theme and I could do it with this part on the product page

 

<script>
 var inv_qty = {};
 {% for var in product.variants %}
   inv_qty[{{- var.id -}}] = {{ var.inventory_quantity | default: 0 }};
 {% endfor %}
</script>


And in the text itself:

$(".product-inventory span.in-stock").text(inv_qty[ variant.id ] +' '+ window.inventory_text.in_stock);

But it doesn't work for the Empire theme, ant combination i try with "variant.inventory_quantity" returns "translation missing"

 

Anyone else have this theme and added a stock available text on the product page?

 

0 Likes
Highlighted
Shopify Partner
111 10 28

I don't think you need javascript for that, a couple of liquid lines of code will do it.

 

{% for variant in product.variants %}
{% if current_variant.inventory_quantity > 0 %} 
"stock available"
{% else %}
"stock unavailable"
{% endif %} {% endfor %}

This simply checks if the variant selected has inventory quantities bigger than 0, and if so, then displays the message. 

 

If the product does not contain variants you can also use this: 

 

{% if product.available %}

"stock available"

{% else %}
"stock not available"
{% endif %}

 

Want to customize or make changes to a theme? Hire me.
If my answer was helpful please Like and Accept Solution.
Email: oscar@sobefy.com
Website: Sobefy.com
0 Likes
Highlighted
New Member
3 0 0

Thanks for the info, though I tried the code you gave me but it works halfway, i get: ""stock available" "stock available" "stock unavailable" "stock available""  when I preview it.

 

sssssssasa.PNG

 

 

 

But this is because the product has 4 variants, if I try it on a single variant product I get it right.

 

Also this would show only if a product is available or not and I would prefer if it specified the amount too like "stock available: 5" or something.

0 Likes
Highlighted
Shopify Partner
111 10 28

Try this  ;)

 

{% for variant in product.variants %}
{% if current_variant.inventory_quantity > 0 %} 

stock available: {{ current_variant.inventory_quantity }}

{% else %} 

"stock unavailable"

{% endif %}
{% endfor %}
Want to customize or make changes to a theme? Hire me.
If my answer was helpful please Like and Accept Solution.
Email: oscar@sobefy.com
Website: Sobefy.com
0 Likes
Highlighted
New Member
3 0 0

Still got the same problem, I used the exact code you provided:

image.png

 

 

 

 

 

And now get all 4 variants as unavailable and the {{ current_variant.inventory_quantity }} is not showing any number.image.png

 

 

I also tried using the current_variant.inventory_quantity alone but it won't show any number or text at all

0 Likes
Highlighted
Shopify Partner
111 10 28

My apologies, this code works, but you are right in needing a bit of javascript as you need to be able to see real time inventory changes when users select different variants.

 

Empire theme is a commercial theme which makes it difficult to support by the community since we do not have access to the theme's code or knowledge of its customization settings.

 

I'll do some digging and get back to you shortly. This code works on my testing theme, but it needs a bit of js to display real time data changes. 

 

{% if current_variant.inventory_quantity > 0 %}
<p>Stock Available: {{ current_variant.inventory_quantity }}</p>
 {% else %}
<p>Stock Unavailable</p>
{% endif %}

 

 

 

Want to customize or make changes to a theme? Hire me.
If my answer was helpful please Like and Accept Solution.
Email: oscar@sobefy.com
Website: Sobefy.com
0 Likes
Highlighted
Tourist
6 0 0

Hello my friend, i have same issue here.

Did you advance on the development of the solution?

i want a quote same fix here.

 

0 Likes