Skip to content

Instantly share code, notes, and snippets.

@zakhardage
Last active January 30, 2016 13:56
Show Gist options
  • Save zakhardage/6168331 to your computer and use it in GitHub Desktop.
Save zakhardage/6168331 to your computer and use it in GitHub Desktop.
Shopify collection template with a quick add-to-cart button for each product for wholesale customers.
<table>
{% tablerow product in collection.products cols: 3 %}
{% assign parentCount = tablerowloop.index %}
<a href="{{ product.url | within: collection }}">
<img id="img{{ tablerowloop.index }}" src="{{ product.featured_image | product_img_url: 'large' }}" alt="{{ product.title | escape }}" />
{{ product.title }}
{% assign wholesalePriceCheck = 'unset' %}
{% for variant in product.variants %}
{% assign opt1 = variant.option1 | handle %}
{% if opt1 contains 'wholesale' %}
{% if wholesalePriceCheck == 'unset' %}
{% assign wholesale_price = variant.price %}
{% assign wholesalePriceCheck = 'set' %}
{% endif %}
{% endif %}
{% endfor %}
{% if customer.tags contains 'wholesale' or customer.tags contains 'Wholesale' %}
Retail: {{ product.variants.first.price | money }}{% if wholesalePriceCheck == 'set' %}<br />Wholesale: <span id="price{{ parentCount }}">{{ wholesale_price | money }}</span>{% endif %}
{% else %}
{% if wholesalePriceCheck == 'set' %}
{% assign product_price = product.price_max %}
{% for variant in product.variants %}
{% assign opt1 = variant.option1 | handle %}
{% unless opt1 contains 'wholesale' %}
{% if variant.price < product_price %}{% assign product_price = variant.price %}{% endif %}
{% endunless %}
{% endfor %}
{{ product_price | money }}
{% else %}
{% if product.compare_at_price %}<del>{{ product.compare_at_price | money }}</del> <span class="red">{{ product.price | money }}</span>{% else %}{{ product.price | money }}{% endif %}
{% endif %}
{% endif %}
</a>
{% comment %} WHOLESALE SECTION {% endcomment %}
{% if customer.tags contains 'wholesale' or customer.tags contains 'Wholesale' %}
{% assign wholesaleCheck = false %}
{% assign marker = product.variants.size %}
{% for variant in product.variants %}
{% assign opt1 = variant.option1 | handle %}
{% if opt1 contains 'wholesale' %}
{% assign wholesaleCheck = true %} {% comment %} checks whether to display the dropdown {% endcomment %}
{% unless forloop.index > marker %}
{% assign first_wholesale_id = variant.id %} {% comment %} set the first, wholesale, variant ID {% endcomment %}
{% assign marker = forloop.index %}
{% endunless %}
{% endif %}
{% endfor %}
{% if product.available %}
{% for variant in product.variants %}
{% assign opt1 = variant.option1 | handle %}
{% if opt1 contains 'wholesale' %}
{% assign variantCount = variantCount | plus:1 %}
{% endif %}
{% endfor %}
<form id="form{{ parentCount }}" action="/cart/add" method="post">
<input type="hidden" name="return_to" value="back" />
{% if variantCount > 1 and wholesaleCheck %}
<div class="select">
<select name="id" id="wholesalePicker{{ parentCount }}" onchange="wholesalePicker('{{ parentCount }}')">
{% for variant in product.variants %}
{% assign opt1 = variant.option1 | handle %}
{% if opt1 contains 'wholesale' %}
<option value="{{ variant.id }}">{{ variant.option1 | remove:'-wholesale' }}</option>
{% endif %}
{% endfor %}
</select>
</div> <!-- end .select -->
{% else %}
<input type="hidden" value="{{ first_wholesale_id }}" name="id" /><br />
{% endif %}
<label>QTY:</label>
<input type="text" value="1" name="quantity" onclick="this.value=''" />&nbsp; &nbsp;
{% assign cartCheck = false %}
{% for item in cart.items %}{% capture items_list %}{{ items_list }} {{ item.id }}{% endcapture %}{% endfor %}
{% capture compareVariant %}{{ product.variants.first.id }}{% endcapture %}
{% if items_list contains compareVariant %}
{% for item in cart.items %}
{% if item.id == product.variants.first.id %}
{% assign available = product.variants.first.inventory_quantity | minus:item.quantity %}
{% endif %}
{% endfor %}
{% else %}
{% assign available = product.variants.first.inventory_quantity %}
{% endif %}
<label>({{ available }} in stock)</label><br />
<button type="submit">add to cart</button>
</form>
{% else %}
This item is currently unavailable
{% endif %}
{% endif %}
{% comment %} END WHOLESALE SECTION {% endcomment %}
{% endtablerow %}
</table>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment