Last active
March 4, 2022 09:08
-
-
Save enamhasan/339c54475a0495eee9ddf0d9cb32b17f to your computer and use it in GitHub Desktop.
Display variant inventory quantiy on product page Shopify
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* check inventory of another product in product page */ | |
{% assign qtylist = '' %} | |
{% assign variant_sku = '' %} | |
{% for variant in product.variants %} | |
{% assign temp = variant.sku %} | |
{% assign variant_sku = variant.sku | split:'-set-' %} | |
{% assign topid =variant_sku[0] | plus: 0 %} | |
{% assign bottomid =variant_sku[1] | plus: 0 %} | |
{% assign vid = variant.id %} | |
{% assign bottomQty = 0 %} | |
{% assign topQty = 0 %} | |
{% assign qtysum = 0 %} | |
{% assign vstr = '' %} | |
{% comment %} | |
{{ variant_sku[0] }}- {{ variant.sku }}<br> | |
{% endcomment %} | |
{% for product in collections.tops.products %} | |
{% for var in product.variants %} | |
{% assign topsvarID =var.id | plus: 0 %} | |
{% if topsvarID == topid %} | |
{% assign topQty = var.inventory_quantity | plus: 0 %} | |
{% break %} | |
{%endif%} | |
{% endfor %} | |
{% endfor %} | |
{% for product in collections.bottoms.products %} | |
{% for var in product.variants %} | |
{% assign bottomsvarID =var.id | plus: 0 %} | |
{% if bottomsvarID == bottomid %} | |
{% assign bottomQty = var.inventory_quantity | plus: 0 %} | |
{% break %} | |
{%endif%} | |
{% endfor %} | |
{% endfor %} | |
{% assign qtysum = topQty | plus: bottomQty %} | |
{% if topQty > bottomQty %} | |
{% assign vstr = vid | append:'-' | append:bottomQty | append:'||' %} | |
{% else %} | |
{% assign vstr = vid | append:'-' | append:topQty | append:'||' %} | |
{% endif %} | |
{% assign qtylist = qtylist | append:vstr %} | |
{% assign variant_sku = '' %} | |
{% comment %}{% endcomment %} | |
{% endfor %} | |
{% assign qtylist = qtylist | split:'||'%} | |
<script> | |
var inv_qty = {}; | |
{% for item in qtylist %} | |
{% assign spvarid = item | split:'-' %} | |
inv_qty[{{ spvarid[0] }}] = {{ spvarid[1] }}; | |
{% endfor %} | |
</script> | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# add this script in product liquid (Just before addtocart form) | |
<!-- Code added for Inventory in Pipeline --> | |
<script> | |
var inv_qty = {}; | |
{% for var in product.variants %} | |
inv_qty[{{- var.id -}}] = {{ var.inventory_quantity | default: 0 }}; | |
{% endfor %} | |
</script> | |
{% if current_variant.inventory_management == "shopify" %} | |
<p class="variant-inventory text-center uppercase" style="margin-top:1em"> | |
{{ current_variant.inventory_quantity }} left in stock | |
</p> | |
{% endif %} | |
<!-- end inventory --> | |
# Add bellow lines to theme.js (just after if variant() line ) | |
/* -- code added for Inventory & SKU -- */ | |
var selectors = { | |
SKU: '.variant-sku', | |
variantInventory: '.variant-inventory' | |
}; | |
$(selectors.SKU, this.$container).html(variant.sku); | |
var inventory_level = (inv_qty[ variant.id ]); | |
if (inventory_level == 0){ | |
$(selectors.variantInventory, this.$container).html('Out of stock').show(); | |
} | |
else if (inventory_level > 10) { | |
$(selectors.variantInventory, this.$container).html("In stock").show(); | |
} | |
else { | |
$(selectors.variantInventory, this.$container).html(inventory_level + ' left in stock').show(); | |
} | |
/* - end - */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment