how to work on variant in shopify
Edit Product-Template.liquid
{% comment %} Inventory tracking on product page {% endcomment %}
<div id="variant-inventory" class="{% unless current_variant.available %} hide {% endunless %}">
{% if current_variant.inventory_management == "shopify" and current_variant.inventory_policy != "continue" %}
We have {{ current_variant.inventory_quantity }} in stock.
{% else %}
This product is available.
{% endif %}
</div>
Step 3: Edit Product-Template.liquid
In case you have multiple variants you will need to add one additional line in the product-template.liquid.
Paste the following code right above the code you just pasted in the product-template.liquid and click on Save.
{% for variant in product.variants %}
<div InventoryManagment data-id="{{variant.id}}" data-inventory="{{variant.inventory_quantity}}" style="display:none;"></div>
{% endfor %}
Step 4: Edit Theme.js
let inventoryHash = document.querySelectorAll('[inventorymanagment]');
debugger;
Array.from(inventoryHash).forEach(
(Selectedvariant)=>{
if(Selectedvariant.dataset.id == variant.id){
if(Selectedvariant.dataset.inventory > 0)
{
$("#variant-inventory").html("We have "+ Selectedvariant.dataset.inventory +" in stock");
}
else{
$("#variant-inventory").html("Sold out!");
}
}
}
);