Skip to content

Instantly share code, notes, and snippets.

@kbxian
Created April 16, 2018 07:03
Show Gist options
  • Save kbxian/b47481f7c5bb15687ee7d7d9117f27a4 to your computer and use it in GitHub Desktop.
Save kbxian/b47481f7c5bb15687ee7d7d9117f27a4 to your computer and use it in GitHub Desktop.
Add filter to Shopify colections page
<script>
jQuery('select[name=custom-filter-by]').on('change', function() {
var thisfilter = jQuery('select[name=custom-filter-by]').val();
jQuery('select[name=custom-filter-by] option[value="'+thisfilter+'"]').attr('selected','selected');
});
jQuery('select[name=custom-sort-by]').on('change', function() {
var thissort = jQuery('select[name=custom-sort-by]').val();
jQuery('select[name=custom-sort-by] option[value="'+thissort+'"]').attr('selected','selected');
});
/* Product Tag Filters - Good for any number of filters on any type of collection pages */
jQuery('button#apply-sort-filter').click(function(e) {
e.preventDefault();
var filterby = jQuery('select[name=custom-filter-by]').val();
var sortby = jQuery('select#custom-sort-by').val();
var query = '';
if (filterby != '') {
query = filterby + '+' + filterby;
}
if (sortby != '') {
query = query + '?sort-by=' + sortby;
}
var newurl = "/collections/all/" + query;
//console.log(newurl);
window.location.href = newurl;
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment