Our projects share similarities, so please complete the section below.
Develop a collection template collection.price-filter.liquid
Utilize ajax to retrieve the products json. Keep in mind that there is a limit of 250 products. If you have more than 250 products, consider using an app to filter the results first or make multiple requests.
<script>
var allProducts;
var filteredProducts = [];
var activeFilter = [];
$(document).ready(function(){
var url = '{{collection.url}}/products.json?limit=250';
const res = getProducts(url);
});
function getProducts(url){
$.ajax({
type: 'GET',
url: url,
success: function(res){
allProducts = res.products;
filterProducts(allProducts);
},
error: function(status){
alert(status);
}
});
}
</script>
Implement filters to populate the filteredProducts
array with product objects
//iterate through products, creating categories ($20-$40, $40-$60, etc.)
function filterProducts(products){
var cat1 = '20-40', cat2 = '40-60';
var currentCat;
products.forEach(function (i, j){
if (i.price > 20 && i.price <= 40){
currentCat = cat1;
else if (i.price > 40 && i.price <= 60) {
currentCat = cat2;
}
if(filteredProducts[currentCat]){
filteredProducts[currentCat].push(i);
}
else {
filteredProducts[currentCat] = [i]
}
};
});
}
Once filteredProducts
is generated, monitor User's checkbox selections and add them to an array named activeFilter
function getActiveFilter(){
$('#myFilter').each(function(){
if($(this).is(':checked')){
//add to activeFilter
}
});
}
Create resultFilter
from filteredProducts
and activeFilter
, eliminating common results;
function resultFilter(){
var result = [];
activeFilter.forEach(function (i, value){
Object.keys(value).forEach(function(j, product){
//add to result
};
});
}
Display product results using handlebars.js
<script id="ProductTemplate" type="text/x-handlebars-template">
{% raw %}
{{#product}}
<div class="col">
<a href="/products/{{productHandle}}" class="grid__image">
<div class="product__image-wrapper" style="background-color: white;">
<img class="no-js lazyload" width="316" height="237"
data-src="{{ featuredImg }}"
data-widths="[108, 360, 375, 414, 568, 684, 720, 732, 736, 768, 1024, 1200, 1296, 1512, 1728, 1944, 2048]"
data-aspectratio="1.33"
data-sizes="auto"
data-parent-fit="width"
alt="{{ title }}">
</div>
</a>
<p class="h5"><a href="/products/{{productHandle}}">{{ title }}</a></p>
</div>
{{/product}}
{% endraw %}
</script>
Insert data into #container-products
, where you want the filtered Products to show.
function buildFilteredProducts(filteredProds) {
var $productContainer = $('#container-product');
$productContainer.empty();
if(filteredProds.length <= 0){
$productContainer.append('empty result');
return;
}
var products = [];
var product = {};
var data = {};
var source = $("#ProductTemplate").html();
var template = Handlebars.compile(source);
products = filteredProds.map(function(productItem) {
return product = {
id: productItem.id,
title: decodeHtml(productItem.title),
featuredImg: responsiveImage(productItem.images[0].src),
productHandle: productItem.handle
}
});
data = {
product: products
}
$productContainer.append(template(data));
}