Created
December 19, 2018 01:29
-
-
Save atikju/c66d484a26a75df42edf3754a664a42e to your computer and use it in GitHub Desktop.
Shopify Swatch - find out of stock variants on click swatches
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
| //find the select which has name="id" and replace with the following | |
| <select id="product-select-{{ product.id }}{{ product-form }}{{ section.id }}{{ block.id }}" name="id" class="multi_select"> | |
| {% for v in product.variants %} | |
| <option {% if v.inventory_quantity < 1 %}class="soldout"{% endif %} {% if v == variant %}selected="selected"{% endif %} value="{{ v.id }}" data-sku="{{ v.sku }}">{{ v.title }}</option> | |
| {% endfor %} | |
| </select> | |
| //JS Part | |
| <script> | |
| $(document).ready(function(){ | |
| $('.swatch[data-option-index="0"] .swatch-element').on('click', function(){ | |
| //console.log('Clicked ya!'); | |
| var thisOption = $(this).attr('data-value'); | |
| console.log(thisOption); | |
| $('.swatch-element').find('.crossed-out').hide(); | |
| $('select.multi_select[name="id"] option').each(function(){ | |
| var ttr = $(this).text(); | |
| //console.log(ttr); | |
| if(ttr.includes(thisOption)){ | |
| if($(this).hasClass('soldout')){ | |
| //show crossed out image | |
| //console.log(ttr); | |
| var splitItNow = ttr.split(' / '); | |
| if(splitItNow[0] === thisOption){ | |
| console.log(splitItNow[1]); | |
| //grab the swatch | |
| $('.swatch-element[data-value="'+splitItNow[1]+'"]').find('.crossed-out').show(); | |
| } | |
| }else{ | |
| //wait.. | |
| } | |
| } | |
| }); | |
| }); | |
| }); | |
| </script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment