Forked from dylanjhunt/Filter by size in stock shopify. Step 1 full guide at dylanjh.com
Created
June 27, 2020 21:53
-
-
Save williambsb/186943599f3fa8f21339f105837c429f to your computer and use it in GitHub Desktop.
Filter by size in stock shopify. Step 1 full guide at https://dylanjh.com
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
{% assign sizes = '' %} | |
{% for product in products limit: limit %} | |
{% for variant in product.variants %} | |
{% if variant.available %} | |
{% assign sizes = sizes | append: variant.options[1] | append: '_' %} | |
{% endif %} | |
{% endfor %} | |
{% endfor %} | |
{% assign sizesArr = sizes | split: '_' | uniq %} | |
<ul class="sortme"> | |
<li class="clearfix filter"> | |
<h4>Filter by Size in Stock</h4> | |
<select class="styled-select coll-filter"> | |
<option value="">All</option> | |
{% for size in sizesArr %} | |
<option value="{{ size }}"{% if current_tags contains size %} selected{% endif %}>{{ size }}</option> | |
{% endfor %} | |
</select> | |
</li> | |
</ul> | |
<script> | |
Shopify.queryParams = {}; | |
if (location.search.length) { | |
for (var aKeyValue, i = 0, aCouples = location.search.substr(1).split('&'); i < aCouples.length; i++) { | |
aKeyValue = aCouples[i].split('='); | |
if (aKeyValue.length > 1) { | |
Shopify.queryParams[decodeURIComponent(aKeyValue[0])] = decodeURIComponent(aKeyValue[1]); | |
} | |
} | |
} | |
jQuery('.coll-picker').change(function() { | |
if (jQuery(this).val()) { | |
location.href = '/collections/' + jQuery(this).val(); | |
} | |
else { | |
location.href = '/collections/all'; | |
} | |
}); | |
var collFilters = jQuery('.coll-filter'); | |
collFilters.change(function() { | |
delete Shopify.queryParams.page; | |
var newTags = []; | |
collFilters.each(function() { | |
if (jQuery(this).val()) { | |
newTags.push(jQuery(this).val()); | |
} | |
}); | |
{% if collection.handle %} | |
var newURL = '/collections/{{ collection.handle }}'; | |
if (newTags.length) { | |
newURL += '/' + newTags.join('+'); | |
} | |
var search = jQuery.param(Shopify.queryParams); | |
if (search.length) { | |
newURL += '?' + search; | |
} | |
location.href = newURL; | |
{% else %} | |
if (newTags.length) { | |
Shopify.queryParams.constraint = newTags.join('+'); | |
} | |
else { | |
delete Shopify.queryParams.constraint; | |
} | |
location.search = jQuery.param(Shopify.queryParams); | |
{% endif %} | |
}); | |
$(document).on('shopify:section:load', function(event) { | |
Shopify.queryParams = {}; | |
if (location.search.length) { | |
for (var aKeyValue, i = 0, aCouples = location.search.substr(1).split('&'); i < aCouples.length; i++) { | |
aKeyValue = aCouples[i].split('='); | |
if (aKeyValue.length > 1) { | |
Shopify.queryParams[decodeURIComponent(aKeyValue[0])] = decodeURIComponent(aKeyValue[1]); | |
} | |
} | |
} | |
jQuery('.coll-picker').change(function() { | |
if (jQuery(this).val()) { | |
location.href = '/collections/' + jQuery(this).val(); | |
} | |
else { | |
location.href = '/collections/all'; | |
} | |
}); | |
var collFilters = jQuery('.coll-filter'); | |
collFilters.change(function() { | |
delete Shopify.queryParams.page; | |
var newTags = []; | |
collFilters.each(function() { | |
if (jQuery(this).val()) { | |
newTags.push(jQuery(this).val()); | |
} | |
}); | |
{% if collection.handle %} | |
var newURL = '/collections/{{ collection.handle }}'; | |
if (newTags.length) { | |
newURL += '/' + newTags.join('+'); | |
} | |
var search = jQuery.param(Shopify.queryParams); | |
if (search.length) { | |
newURL += '?' + search; | |
} | |
location.href = newURL; | |
{% else %} | |
if (newTags.length) { | |
Shopify.queryParams.constraint = newTags.join('+'); | |
} | |
else { | |
delete Shopify.queryParams.constraint; | |
} | |
location.search = jQuery.param(Shopify.queryParams); | |
{% endif %} | |
}); | |
}); | |
</script> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment