Skip to content

Instantly share code, notes, and snippets.

@cole007
Last active June 6, 2017 05:38
Show Gist options
  • Save cole007/54a7422aa55859fabfdd to your computer and use it in GitHub Desktop.
Save cole007/54a7422aa55859fabfdd to your computer and use it in GitHub Desktop.
Shopify code samples
$(function($){
function doProducts(collection, limit) {
collection = typeof collection !== 'undefined' ? collection : 'books';
limit = typeof limit !== 'undefined' ? limit : 250;
var url = 'http://XXXX.myshopify.com/collections/' + collection + '/products.json?limit=' + limit;
return url;
}
var doFilter = function(data,min,max) {
min = typeof min !== 'undefined' ? min : 0;
max = typeof max !== 'undefined' ? max : 250;
var output = new Array();
for (var i = 0; i < data.products.length; i++) {
if (data.products[i].variants[0].price >= min && data.products[i].variants[0].price <= max) {
output.push(data.products[i]);
}
}
return output;
}
$('form').on('submit',function(e) {
e.preventDefault();
var min = parseInt($(this).find('[name=min]').val());
var max = parseInt($(this).find('[name=max]').val());
var collection = $(this).find('[name=collection]').val();
var json = doProducts(collection);
var imgsrc = 'https://cdn.shopify.com/s/assets/admin/no-image-large-d7c282f81cbf208c9ee0f0f27cb214c7.gif';
$('.products').find('li').remove();
$.ajax({
type: 'GET',
url: json,
dataType: 'jsonp',
success: function (data) {
var output = doFilter(data,min,max);
for (var i = 0; i < output.length; i++) {
var item = output[i];
if (item.images.length > 0) {
var tempSrc = item.images[0].src;
} else {
var tempSrc = imgsrc;
}
$('.products').append('<li><a href="/collections/' + collection + '/products/' + item.handle + '"><img src="' + tempSrc + '" alt=""/> ' + item.title + '</a></li>');
}
}
});
});
});
{% capture saved_amount %}{{ compare_price | money }}{% endcapture %}
{% comment %}
Unless this store uses multiple currencies,
we will wrap the cents in a sup (superscript) element,
to hide them with CSS.
{% endcomment %}
{% unless shop.money_format contains 'money' %}
{% if shop.money_format contains '{{amount}}' %}
{% unless shop.money_format contains '.' %}
{% capture saved_amount %}{{ saved_amount | replace: '.','<sup>' }}</sup>{% endcapture %}
{% endunless %}
{% elsif shop.money_format contains '{{amount_with_comma_separator}}' %}
{% capture saved_amount %}{{ saved_amount | replace: ',','<sup>' }}</sup>{% endcapture %}
{% endif %}
{% endunless %}
{% assign saved_amount = saved_amount | strip %}
<del>{{ saved_amount }}</del>
{% comment %}
{{ 'products.general.save_html' | t: saved_amount: saved_amount }}
{% if compare_price > product_price %}
Save {{ compare_price | minus: product_price | times: 100 | divided_by: compare_price | round }}%
{% endif %}
{% endcomment %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment