Skip to content

Instantly share code, notes, and snippets.

@carolineschnapp
Created May 26, 2011 18:04
Show Gist options
  • Save carolineschnapp/993663 to your computer and use it in GitHub Desktop.
Save carolineschnapp/993663 to your computer and use it in GitHub Desktop.
Holy Grail for buy section in product.liquid
{% if product.available %}
<form method="post" action="/cart/add">
{% if product.variants.size > 1 %}
<!-- START LISTING VARIANTS -->
<select name="id" id="product-select">
{% for variant in product.variants %}
{% if variant.available %}
<option value="{{ variant.id }}">{{ variant.title }} ({{ variant.price | money }})</option>
{% else %}
<option disabled="disabled">{{ variant.title }} ({{ variant.price | money }}) - SOLD OUT</option>
{% endif %}
{% endfor %}
</select>
<!-- END LISTING VARIANTS -->
{% else %}
{% assign variant = product.variants.first %}
<input type="hidden" name="id" value="{{ variant.id }}" />
{% endif %}
<!-- START BUY -->
{% assign variant = product.variants.first %}
<span id="price">{{ variant.price | money }}</span>
{% if product.compare_at_price_max > product.price %}
<del id="compare-at-price">{% if variant.compare_at_price > 0 %}{{ variant.compare_at_price | money }}</del>
{% endif %}
<input type="submit" name="purchase" value="Purchase" id="purchase" />
<!-- END BUY -->
</form>
{% else %}
<span class="sold-out">SOLD OUT</span>
{% endif %}
{% if product.availbable and product.variants.size > 1 %}
<script type="text/javascript">
// <![CDATA[
var selectCallback = function(variant, selector) {
if (variant && variant.available) {
jQuery("#purchase").removeClass("disabled").removeAttr("disabled");
jQuery("#price").html(Shopify.formatMoney(variant.price, "{{ shop.money_format }}"));
if (variant.compare_at_price) {
jQuery("#compare-at-price").html(Shopify.formatMoney(variant.compare_at_price, "{{ shop.money_format }}"));
}
else {
jQuery("#compare-at-price").empty();
}
}
else {
jQuery("#purchase").addClass("disabled").attr("disabled", "disabled");
var message = variant ? "Sold Out" : "Unavailable";
jQuery("#price").text(message);
jQuery("#compare-at-price").empty();
}
};
jQuery(function() {
new Shopify.OptionSelectors("product-select", { product: {{ product | json }}, onVariantSelected: selectCallback });
{% assign found_one_in_stock = false %}
{% for variant in product.variants %}
{% if variant.available and found_one_in_stock == false %}
{% assign found_one_in_stock = true %}
{% for option in product.options %}
jQuery('#product-select-option-' + {{ forloop.index0 }}).val({{ variant.options[forloop.index0] | json }}).trigger('change');
{% endfor %}
{% endif %}
{% endfor %}
{% if product.options.size == 1 %}
{% for variant in product.variants %}
{% unless variant.available %}
jQuery('option:contains(' + {{ variant.title | json }} + ')').attr('disabled', 'disabled');
{% endunless %}
{% endfor %}
{% endif %}
});
// ]]>
</script>
{% endif %}
@justinmetros
Copy link

typo line 38 availbable. also, thanks! ~j

@ErickMirandadotcom
Copy link

Liquid syntax error: 'if' tag was never closed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment