Skip to content

Instantly share code, notes, and snippets.

@kyleaparker
kyleaparker / gist:1a18794a075dbbc0c3e4
Created July 9, 2014 15:24
[Shopify] Recently viewed, show image based on image name
{% raw %}
<script id="recently-viewed-product-template" type="text/x-jquery-tmpl">
{{each(prop, val) images}}
{{if val.toLowerCase().indexOf(".imagename") >= 0 }}
<div id="product-${handle}" class="product">
<div class="image">
<a href="${url}" class="cy">
<img src="${Shopify.Products.resizeImage(val, "medium")}" />
</a>
</div>
@kyleaparker
kyleaparker / 0_reuse_code.js
Last active August 29, 2015 14:08
Here are some things you can do with Gists in GistBox.
// Use Gists to store code you would like to remember later on
console.log(window); // log the "window" object to the console
@kyleaparker
kyleaparker / gist:9a3c51915953e98738d8
Created May 3, 2013 17:00
List products by product type
{% for product_type in shop.types %}
<h3>{{ product_type | link_to_type }}</h3>
{% for product in collections.all.products %}
{% if product.type == product_type %}
{{ product.title }}
{% endif %}
{% endfor %}
{% endfor %}
@kyleaparker
kyleaparker / gist:6122816
Created July 31, 2013 15:09
Shopify - Add percentage saved to the price in the Select callback.
---Find the select callback:
var selectCallback = function(variant, selector) {
if (variant && variant.available) {
---Add this with the select callback:
var per_saved = ((variant.compare_at_price - variant.price)/variant.compare_at_price)* 100.0;
$('.options .price').html('<strong>'+Shopify.formatMoney(variant.price, "{{shop.money_with_currency_format }}").toString().replace(',', '.') + '</strong><span class="compare_at_price"><del>' + Shopify.formatMoney(variant.compare_at_price, "{{shop.money_with_currency_format}}").toString().replace(',', '.') + '</del><br>You save ' + per_saved + '%</span>');
@kyleaparker
kyleaparker / gist:042f05a943c03ef11c67
Created October 8, 2013 00:18
Get product’s SEO information in Liquid
{% assign seo_data = product.metafields.global %}
{{ seo_data.description_tag }}
{{ seo_data.title_tag }}
{{ seo_data.metafields | to_json }}
@kyleaparker
kyleaparker / gist:7356064
Last active December 27, 2015 16:39
Inventory Quantity Table for up to two options
<div id="inventory">
<h3>Inventory Available</h3>
{% assign options1 = product.variants | map: "option1" %}
<table>
<tr>
{% if product.options.size > 1 %}<th>&nbsp;</th>{% endif %}
{% assign options1Filtered = '' %}
{% for option1 in options1 %}
{% assign option1Space = option1 | prepend: ', ' %}
{% unless options1Filtered contains option1Space %}
@kyleaparker
kyleaparker / gist:7360715
Last active December 27, 2015 17:18
Multi variant add to cart form
<div id="inventory">
<h3>Inventory Available</h3>
{% assign options1 = product.variants | map: "option1" %}
<table>
<tr>
{% if product.options.size > 1 %}<th>&nbsp;</th>{% endif %}
{% assign options1Filtered = '' %}
{% for option1 in options1 %}
{% assign option1Space = option1 | prepend: ', ' %}
{% unless options1Filtered contains option1Space %}
@kyleaparker
kyleaparker / gist:7360751
Last active December 27, 2015 17:19
Multi add to cart
{{ 'api.jquery.js' | shopify_asset_url | script_tag }}
<script type="text/javascript">
/* Enable multi add to cart function */
jQuery("#product-variants").hide();
Shopify.queue=[];$("#add").click(function(e){e.preventDefault();$(".quantity-add").each(function(){var a=$(this),id=a.attr('id'),qty=a.val();if(qty>0){Shopify.queue.push({variant_id:id,quantity_id:parseInt(qty,10)})}});if(Shopify.queue.length){Shopify.addItems()}else{alert("You have not selected anything to purchase")}});Shopify.addItems=function(){var request=Shopify.queue.shift();if(typeof request=='object'){Shopify.addItem(request.variant_id,request.quantity_id,Shopify.addItems)}else{document.location.href="/cart"}}
/* End multi add to cart function */
</script>
@kyleaparker
kyleaparker / gist:8362253
Created January 10, 2014 20:47
Shopify: Show the second lowest variant price
{% assign lowestPrice = product.variants.first.price %}
{% assign secondPrice = 0 %}
{% for variant in product.variants %}
{% if variant.price < lowestPrice %}
{% assign secondPrice = lowestPrice %}
{% assign lowestPrice = variant.price %}
{% endif %}
{% endfor %}
@kyleaparker
kyleaparker / gist:928c5925dc07ffe487ba
Created June 11, 2014 20:50
[Shopify] Show option as plain text if there is only one option in the dropdown
$('.single-option-selector').each(function() {
var numOptions = $(this).children().length;
if(numOptions == 1) {
$(this).hide();
$(this).before( "<p>" + $(this).val() + "</p>");
}
});