Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save schaeken/f8233b653c64c8515461 to your computer and use it in GitHub Desktop.
Save schaeken/f8233b653c64c8515461 to your computer and use it in GitHub Desktop.
Add "add to cart" button to collection page. If there's more than one variant, it adds a text link to the product page that says 'Select Options'. Otherwise it adds the first variant to the cart. If product is not available, show 'Sold Out' link instead.
{% if product.available %}
{% if product.variants.size > 1 %}
<a href="{{ product.url | within: collection }}" class="btn" title="{{ product.title | escape }}">Select Options</a>
{% else %}
<form action="/cart/add" method="post">
<input type="hidden" name="id" value="{{ product.variants.first.id }}" />
<input type="submit" class="btn" value="Add to Cart" />
</form>
{% endif %}
{% else %}
<a id="SoldOut" href="{{ product.url | within: collection }}" class="btn" title="{{ product.title | escape }}">Sold Out</a>
{% endif %}
<script>
$( document ).ready(function() {
$('#SoldOut').addClass('disabled').attr('disabled', 'disabled').fadeTo(200,0.5);
$('#SoldOut').css({ cursor: "pointer" });
});
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment