Created
February 1, 2019 02:09
-
-
Save atikju/b06b397312061b4fe141e03c5de6adf4 to your computer and use it in GitHub Desktop.
Shopify - Show variant inventory on swatch click
This file contains hidden or 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
| <div id="variant-inventory" class="devst-variant-inventory"> | |
| {% if product.variants.first.inventory_management == "shopify" %} | |
| {% if product.variants.first.inventory_quantity > 0 %} | |
| {% if product.variants.first.inventory_quantity > 10 %} | |
| {% assign hideValue = 'none' %} | |
| {% else %} | |
| {% assign hideValue = 'block' %} | |
| {% endif %} | |
| <span id="qtyLogic" style="display:{{ hideValue }}"> | |
| Only <span class="devStock">{{ product.variants.first.inventory_quantity }}</span> left! | |
| </span> | |
| {% else %} | |
| <span id="qtyLogic" style="display:none"> | |
| Only <span class="devStock">{{ product.variants.first.inventory_quantity }}</span> left! | |
| </span> | |
| <span id="outStockNotice">The product is out of stock</span> | |
| {% endif %} | |
| {% else %} | |
| This product is available | |
| {% endif %} | |
| </div> |
This file contains hidden or 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
| {{ 'api.jquery.js' | shopify_asset_url | script_tag }} | |
| {{ 'option_selection.js' | shopify_asset_url | script_tag }} | |
| <script> | |
| var selectCallback = function(variant, selector) { | |
| if (variant) { | |
| if (variant.available) { | |
| if(variant.inventory_quantity > 10){ | |
| //if is greater than 10 | |
| //hide | |
| $('#qtyLogic').hide(); | |
| $('#outStockNotice').show(); | |
| }else{ | |
| if(variant.inventory_quantity >= 1){ | |
| $('#qtyLogic').show(); | |
| $('#outStockNotice').hide(); | |
| } | |
| if(variant.inventory_quantity < 1){ | |
| $('#qtyLogic').hide(); | |
| $('#outStockNotice').show(); | |
| } | |
| } | |
| $('.devStock').text(variant.inventory_quantity); | |
| console.log('yo: '+variant.inventory_quantity); | |
| // Selected a valid variant that is available. | |
| //$('#add').removeClass('disabled').removeAttr('disabled').val('Add to Cart').fadeTo(200,1); | |
| } else { | |
| // Variant is sold out. | |
| $('.devStock').text(variant.inventory_quantity); | |
| } | |
| } else { | |
| // variant doesn't exist. | |
| $('#add').val('Unavailable').addClass('disabled').attr('disabled', 'disabled').fadeTo(200,0.5); | |
| } | |
| } | |
| // initialize multi selector for product | |
| jQuery(function($) { | |
| new Shopify.OptionSelectors("product-select", { product: {{ product | json }}, onVariantSelected: selectCallback }); | |
| $('.option-value-input').on('change', function(){ | |
| setTimeout(function(){ | |
| var currentVar = location.href; | |
| currentVar = currentVar.split('=')[1]; | |
| prodGetter(currentVar); | |
| }, 300); | |
| }); | |
| var product_handle = '{{ product.handle }}'; | |
| function prodGetter(currentVar){ | |
| var disID = currentVar; | |
| jQuery.getJSON('/products/'+product_handle+'.js', function(product) { | |
| var xx = product.variants; | |
| var i; | |
| for (i = 0; i < xx.length; i++) { | |
| var thisIDx = xx[i].id; | |
| if( disID == thisIDx){ | |
| console.log(xx[i].inventory_quantity); | |
| if(xx[i].inventory_quantity > 10){ | |
| //if is greater than 10 | |
| //hide | |
| $('#qtyLogic').hide(); | |
| $('#outStockNotice').hide(); | |
| }else{ | |
| if(xx[i].inventory_quantity >= 1){ | |
| $('#qtyLogic').show(); | |
| $('#outStockNotice').hide(); | |
| } | |
| if(xx[i].inventory_quantity < 1){ | |
| $('#qtyLogic').hide(); | |
| $('#outStockNotice').show(); | |
| } | |
| // $('#qtyLogic').show(); | |
| // $('#outStockNotice').hide(); | |
| } | |
| // if(xx[i].inventory_quantity < 1){ | |
| // //if is greater than 10 | |
| // //hide | |
| // $('#qtyLogic').hide(); | |
| // $('#outStockNotice').show(); | |
| // } | |
| $('.devStock').text(xx[i].inventory_quantity); | |
| } | |
| } | |
| }); | |
| } | |
| }); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment