Craft plugin to integrate with the Shopify API
- Move the
shopify
folder into yourcraft/plugins
directory - Install the plugin in the Control Panel
- Enter your Shopify Credentials in the Plugin settings
Retrieve all products from Shopify. You can pass in any parameters that are noted in the products endpoint. Example:
{% for product in craft.shopify.getProducts({ fields: 'title,variants', limit: 5 }) %}
<div class="product">
<h2>{{ product.title }}</h2>
<ul>
{% for variant in product.variants %}
<li>{{ variant.title }} - ${{ variant.price }}</li>
{% endfor %}
</ul>
</div>
{% endfor %}
This is just a slightly modified version of the getProducts method where the array keys are the product ID. So if you have a field named shopifyProduct
which stores the Shopify product ID, you can access all the products' variants with a single API call.
{% set shopifyProducts = craft.shopify.getProductsVariants() %}
{% for entry in craft.entries({ section: 'product' }) %}
<div class="product">
<h2><a href="{{ entry.url }}">{{ entry.title }}</a></h2>
{% if entry.shopifyProduct and shopifyProducts[entry.shopifyProduct] %}
<ul>
{% for variant in shopifyProducts[entry.shopifyProduct] %}
<li>{{ variant.title }} - ${{ variant.price }}</li>
{% endfor %}
</ul>
{% endif %}
</div>
{% endfor %}
Useful on product show pages to access Shopify product information. Useful to get a product price or create an add to cart form. This hits the single product endpoint.
{% set shopify = craft.shopify.getProductById({ id: entry.shopifyProduct, fields: 'variants' }) %}
<form action="http://your.shopify.url/cart/add" method="post">
<select name="id">
{% for variant in shopify.variants %}
<option value="{{ variant.id }}">{{ variant.title }} - ${{ variant.price }}</option>
{% endfor %}
</select>
<input type="hidden" name="return_to" value="back">
<button type="submit">Add to Cart</button>
</form>