So far in ourShopify tutorial serieswe've looked at a lot of concepts relating to how Liquid works in Shopify themes. In this article, I'd like to take a more in-depth look at one particular template —product.liquid
.
If you are new to Shopify themes,product.liquid
is the template that is rendered by default whenever a customer views a product detail page. As discussed in a previous tutorial, it is also possible to havealternate product templates. You can also build a customizablerelated productssection. However in this post we'll stick to the basic template, which resides in thetemplates
folder within a Shopify theme.
By way of an example, I am going to use theproduct.liquid
template from my own starter theme “Birthday Suit”. Here it is in its entirety:
{{ product.title }}
{{ product.description }}
As you will see, there's very little HTML in this template. This is on purpose as it's intended as a starting block for your own theme. If you download a theme from the Shopify theme store, you'll notice that theproduct.liquid
template will be more involved but may not actually contain much more Liquid code.
Let's examine what's happening in detail. We begin by using Liquid output to display the product's title and description:
{{ product.title }}
{{ product.description }}
As the description is entered via the Shopify admin, we don't need to wrap this output with further HTML. Of course, if you need to add in a wrapper element of some sort then you can.
Moving down the template, we come to theform
and openingelement:
Theaction
attribute is important – it must be set to/cart/add
in order for products to be added to the cart. We also need to ensure that theelement has its
name
attribute set toid
.
Next comes the main output of the template:
{% for variant in product.variants %}
{% if variant.available == true %}
{% else %}
{% endif %}
{% endfor %}
A few things are at work here:
- We create a
for loop
to iterate over all the current products variants - We check to see if the current product in the loop has inventory using
{% if variant.available == true %}
- If the product has inventory, we output the title in an
element and set the value of the
to the variants
id
. As well as outputting the variant title, we output the price and use themoney_with_currency
filter. - If the product has no inventory, we output a disabled
element and output the title followed by
sold out!
- Finally, we close off our
如果
statement andfor
loop
Next we add in athat when clicked will add an available product to the cart:
We complete the template by closing out theelement.
This template makes use of both theproductandvariantobjects. They have a large range of properties that you can display in this template and are worth investigating as you develop your Shopify theme skills.
You might also like:10 Top Questions About Developing Shopify Themes Answered
Extending the template
Of course this example is relatively simplistic and is intended as a starting point for your own development. There's a lot more you could include in this template including:
- Adding in Liquid code to display product andvariant images
- Use the Shopify JavaScript snippet
option_selection.js
to allow better display of variant options - Use the
| t
filter for retrieving translated strings from your theme'slocale file
Learning Liquid: Getting Started with Shopify Theming
Get this free guide and learn practical tips, tricks, and techniques to start modifying, developing, and building Shopify themes.
By entering your email - we’ll also send you marketing emails related to Shopify. You can unsubscribe anytime. Note: the guide won't be delivered to role-based emails, like info@, developer@, etc.