Learn to customize your Shopify theme with Liquid, from basic edits to advanced features like dynamic content, and more.
Shopify is an e-commerce platform that allows the user to build company webstores. With the many prebuilt theme varieties in Shopify, being able to customize your own theme will make a store unique and further personalize your brand. Such customizations have their key in Liquid-Shopify’s templating language. Liquid will enable shop owners and developers to create dynamic content, manipulate data, and control how it is to be shown in a Shopify theme.
In this blog, we will start with the basics of Shopify theme customization using Liquid by taking you through the main concepts supported by code snippets and real examples. Whether you’re a developer or a Shopify merchant looking to give an individual look to your online store, with the help of this guide, you’ll learn how you can customize your Shopify theme using Liquid.
Liquid is an open-source templating language created by Shopify. It’s the glue between the data in your store and the HTML that presents that data, making it possible to render dynamic content with ease. Syntax in Liquid is comprised of HTML and CSS with a little bit of logic; thus, it is pretty easy to learn if you know any front-end web development.
Liquid includes:
Before going into the discussion about customization, you need to be fitted with how setup of theme files in Shopify and code editor which you will use. The very basic Shopify theme structure is composed of
To edit a Shopify theme, you can either use the online Code Editor available in the Shopify admin or download theme files and use a local development environment with the Shopify CLI installation.
Ok. Let’s get moving with a couple of basic customizations, just to get your store customized with your personal touch.
You may want to customize your front page to show specific products, collections, or even promotion banners. For simplicity, now, let’s assume a basic example to show a featured product on your front page.
Open the index.liquid file of your theme and insert the next code so that you can have a featured product show up:
1 2 3 4 5 6 7 8 |
{% assign featured_product = all_products['your-product-handle'] %} <div class="featured-product"> <h2>Featured Product: {{ featured_product.title }}</h2> <img src="{{ featured_product.featured_image | img_url: 'large' }}" alt="{{ featured_product.title }}"> <p>{{ featured_product.description }}</p> <a href="{{ featured_product.url }}">Shop Now</a> </div> |
The code above will assign the variable featured_product with a particular product from your store and then return its title, image, description, and a link to the product page. You need to replace ‘your-product-handle’ with the actual product handle of the product in your store.
Liquid allows you to output a range of product information in your Shopify theme. By editing the product.liquidemplate you are able to change exactly how and where product information is shown.
Using the following code you can, for example, output the product information for title, price and variants:
1 2 3 4 5 6 7 |
<h1>{{ product.title }}</h1> <p>{{ product.description }}</p> <p>Price: {{ product.price | money }}</p> {% for variant in product.variants %} <p>{{ variant.title }}: {{ variant.price | money }}</p> {% endfor %} |
The above code will loop through all the available variants of the product and print its respective title and price.
Conditionals in Liquid is how you dynamically show – or not show – certain blocks of content based on certain conditions. Suppose you want to customize your product page to show some message when a product is out of stock:
1 2 3 4 5 |
{% if product.available %} <p>This product is in stock and ready to ship!</p> {% else %} <p>Sorry, this product is currently out of stock.</p> {% endif %} |
Similarly, you can apply conditionals on discounting or showing messages to only a certain group of customers; for example, wholesale customers.
With Liquid loops, you are able to show many products or collections dynamically within your Shopify store. Suppose you want to show some products from one particular collection; you can use the following code for it:
A single collection can be iterated with like so:
1 2 3 4 5 6 7 8 9 10 |
{% assign collection = collections['your-collection-handle'] %} <h2>{{ collection.title }}</h2> <ul> {% for product in collection.products %} <li> <a href="{{ product.url }}">{{ product.title }}</a> - {{ product.price | money }} </li> {% endfor %} </ul> |
This will output all products within the defined collection in a listed format. This would replace ‘your-collection-handle’with the handle of whatever collection you want.
The cart page is a very important part of the customer journey – and Liquid can refresh it. You may want to display your own messages, or upsell suggestions if there are certain items in the cart.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
{% if cart.item_count > 0 %} <p>You have {{ cart.item_count }} items in your cart.</p> <p>Subtotal: {{ cart.total_price | money }}</p> <h3>You might also like:</h3> <ul> {% for product in collections['related-products'].products %} <li> <a href="{{ product.url }}">{{ product.title }}</a> - {{ product.price | money }} </li> {% endfor %} </ul> {% else %} <p>Your cart is empty.</p> {% endif %} |
It would have shown the total number of items in the cart, the cart sub-total, and some upsell product suggestions from a certain collection named ‘related-products’.
Liquid eliminates duplicated code because you can declare code snippets which you can reuse. For example, you can declare a snippet that renders a product card, then include that snippet in several templates.Create a new file in the Snippets folder like product-card.liquid, and paste the following code in:
1 2 3 4 5 6 |
<div class="product-card"> <h3>{{ product.title }}</h3> <img src="{{ product.featured_image | img_url: 'medium' }}" alt="{{ product.title }}"> <p>{{ product.price | money }}</p> <a href="{{ product.url }}">View Product</a> </div> |
You can now include this snippet throughout multiple templates, like your collection or homepage template, with:
1 2 3 |
{% for product in collection.products %} {% include 'product-card' %} {% endfor %} |
This helps you to keep cleaner and more organized code and will save you a lot of hassle when the time to update your theme comes.
While working on Shopify theme customization, one should never forget about performance optimization of a theme. By using Liquid, caching, or image load optimizations, it’s possible to cut down the page load time.
For example, the img_url filter fetches the image of the right size for the device:
1 |
<img src="{{ product.featured_image | img_url: 'small' }}" alt="{{ product.title }}"> |
Also, you can use the {% include %} tag but you should do so rarely because it may slow down page rendering considerably.
When you work with Liquid, debugging is a must. Shopify has a few built-in utilities such as “theme preview” that allows you to test changes before going live. You can also use {{ debug }} for debugging Liquid variables.
Example
Let’s say you want to see what’s inside this variable. You’ll use
1 |
{{ product | json }} |
This would output the product object in JSON and can be used for debugging when customizing.
Liquid allows the Shopify store owner or developer to extend the functionality of a theme using dynamic and fully customized content. Learning to manipulate the Liquid objects, tags, and filters allows you to hone the Shopify store’s look and feel to your business’ specific needs.
It also allows Liquid to do everything from simple customizations-like changing the layout of the homepage-to more advanced techniques that show related products or customize the cart page. The more comfortable you get, the more advanced features you will know: show customer-specific content, filter products, and keep your store flexible and scalable.
This will be a good way to learn the Liquid language, which Shopify uses and, in turn, build that truly personalized ecommerce experience where functions and user experiences are better delivered.
To learn more about Shopify and its capabilities, check out their official website here.
For additional insightful articles and information, please reach out to us.
Web Development Services in the United States