How to Use Shopify Liquid Snippets for Dynamic Product Filtering

How to Use Shopify Liquid Snippets for Dynamic Product Filtering

Why Use Shopify Liquid for Dynamic Product Pages

In e-commerce, making your product pages interactive and responsive is key to improving user experience and boosting conversions. This is where Shopify Liquid comes in—it’s the powerful templating language that powers your Shopify store.

With Shopify Liquid snippets, you can create dynamic product pages that adapt to customer actions in real-time, like selecting different colors, sizes, or personalized features.

Why does this matter? Well, Shopify’s own data shows that stores offering personalized experiences and custom options can see up to a 10% boost in sales.

By using dynamic pages, you're not just providing a better shopping experience, but also reducing bounce rates and encouraging more conversions.

What Are Shopify Liquid Snippets?

Now, what exactly are Liquid snippets? Think of them as reusable blocks of code that can be added to your theme to make product pages more flexible. Instead of creating static, one-size-fits-all pages, snippets allow you to change content based on user interaction—without needing to reload the page.

Imagine your store selling t-shirts. With Shopify Liquid snippets, when a customer selects a different color or size, the page updates to reflect the new price, stock availability, or even related products—all without leaving the product page.

This is what we mean by dynamic product pages. And the best part? You can implement this without heavy, slow third-party apps, which means faster load times and better SEO.

Setting Up Dynamic Product Options with Liquid Snippets

Now that you know why Shopify Liquid is essential for creating dynamic product pages, let's dive into the practical side—how to actually implement dynamic product options using Liquid snippets.

Whether you want to display different product variants, update prices dynamically, or show custom messages, Liquid snippets make it all possible.

Step 1: Create a Liquid Snippet for Product Options

To get started, you’ll need to create a new Liquid snippet that will control the dynamic elements of your product page, such as size, color, or other custom options.

  1. Go to your Shopify AdminOnline StoreThemesEdit Code.
  2. Under the "Snippets" folder, click "Add a new snippet". Name it something like dynamic-product-options.liquid.
  3. In this snippet, you’ll write code that controls the display of dynamic options. Here's a basic example:
{% assign product_options = product.options_with_values %}
<ul>
   {% for option in product_options %}
      <li>{{ option.name }}:
           <select>
               {% for value in option.values %}
                   <option value="{{ value }}">{{ value }}</option>
               {% endfor %}
           </select>
        </li>
   {% endfor %}
</ul>

This snippet will generate a dropdown menu for product options like size or color. When the customer selects an option, the page will dynamically update without a full page reload.

Step 2: Add the Snippet to the Product Page Template

Once your Liquid snippet is ready, you need to insert it into your product.liquid template so that it appears on each product page.

  1. In your Theme Editor, open the product.liquid file (found in the "Templates" folder).
  2. Insert the snippet where you want the product options to appear:
{% include 'dynamic-product-options' %}

This code will call the snippet you just created and display it on the product page. Now, when customers view a product, they’ll see a dropdown list for selecting options like size and color, and the page will update in real time based on their selection.

Step 3: Update Pricing Dynamically Based on Product Options

You can also update the product price dynamically when a customer selects a different option. To do this, we’ll use a combination of JavaScript and Liquid.

Here’s a simple example of updating the price based on the variant chosen:

<p class="price" id="product-price">{{ product.price | money }}</p>
<script>
     document.querySelectorAll('select').forEach(function(select) {
        select.addEventListener('change', function() {
          var selectedVariant = {{ product.variants | json }};
          var newPrice = selectedVariant.find(v => v.title === this.value).price;
          document.getElementById('product-price').textContent = newPrice / 100;
      });
  });
</script>
This will update the displayed product price based on the customer’s selection of size or color. When a new option is selected, the price element (product-price) will change in real time without refreshing the page.

Pro Tip: Adding Custom Fields for Personalized Products

Want to take things further? If you sell personalized products (like engraved items), you can add custom fields to collect input from customers.

For example, to add a custom text field for engraving:

<p>Enter your custom text:</p> <input type="text" name="engraving" id="engraving-input">

With this simple snippet, customers can input text that you can use to personalize their products. You can process this information at checkout using Shopify’s line item properties feature.

By using these Liquid snippets, you’re enabling dynamic interaction on your product pages, making the shopping experience smoother and more engaging.

Whether it’s changing options, updating prices, or collecting personalized information, Shopify Liquid gives you full control without the need for heavy apps that could slow down your store.

Adding Conditional Logic for Dynamic Display of Product Variations

Once you’ve set up your basic Shopify Liquid snippets for dynamic product options, you can enhance the experience even further by using conditional logic.

Conditional logic allows you to display certain product details, badges, or offers based on specific conditions—like inventory levels, user selections, or special sales periods.

This is a powerful tool that lets your product page adapt to real-time conditions, making the shopping experience more personalized.

Why Use Conditional Logic?

Conditional logic helps to:

  • Show real-time stock alerts (e.g., “Only 3 left in stock!”).
  • Display different prices or messages based on the variant selected.
  • Highlight limited-time offers or discounts when a certain product option is chosen.

This dynamic display keeps your customers engaged by providing them with relevant and timely information without overwhelming them with details that may not apply to their choices.

How to Implement Conditional Logic in Shopify Liquid

Here’s a simple example of how you can use Liquid’s conditional statements (if/else) to make your product page more dynamic.

In this case, let’s say you want to display a low stock warning when inventory for a selected variant is below a certain threshold.

Step 1: Set Up a Conditional Low Stock Warning

You can use Shopify Liquid’s built-in inventory tracking and combine it with an if statement to display this warning dynamically.

This code checks if the selected variant’s inventory is below 5. If it is, it displays a low stock warning to create urgency. Otherwise, it simply shows the number of items in stock.

Step 2: Display Different Prices Based on Variant Selection

Another use of conditional logic is to display different prices or discounts based on the variant chosen. For example, if you want to show a discounted price when a customer selects a specific color or size, you can implement this as follows:

{% if current_variant.title == 'Large' %}
   <p class="price">Discounted Price: ${{ current_variant.price | money }}</p>
{% else %}
   <p class="price">Price: ${{ current_variant.price | money }}</p>
{% endif %}

In this example, when a customer selects the Large variant, the page displays a discounted price. For other variants, it shows the regular price. You can adapt this for any condition, such as variant color or material.

Step 3: Show Special Product Badges for Limited Editions

You can also use conditional logic to display custom badges for products, such as "Limited Edition", "Bestseller", or "New Arrival". This can be useful for drawing attention to specific variants.

{% if product.tags contains 'Limited Edition' %}
    <div class="badge">Limited Edition</div>
{% endif %}

If a product has a "Limited Edition" tag, this snippet will display a custom badge on the product page, letting customers know it’s special or rare.

Updating Low Stock Alerts Dynamically

Let’s say you run a clothing store and want to use Shopify’s conditional logic to show customers real-time information about product stock. For example, if you’re selling jackets and certain sizes are selling out fast, you can create urgency by showing a “Low Stock” message.

Example snippet:

{% if current_variant.inventory_quantity <= 2 %}
    <p class="urgent-alert">Only {{ current_variant.inventory_quantity }} left—order now before it's gone!</p>
{% endif %}

This snippet will trigger the warning when inventory for any product variant drops to 2 or fewer items. This strategy can increase conversion rates, as customers feel a sense of urgency.

Why It’s Important to Test Conditional Logic

While using conditional logic is powerful, it’s important to test these features thoroughly before pushing them live. Test different variants, stock levels, and product conditions to ensure everything works smoothly across different scenarios.

A/B testing of conditional logic on product pages has shown a 30% improvement in conversions for stores that display relevant information dynamically, according to Shopify's UX report.

By leveraging Shopify Liquid snippets combined with conditional logic, you can provide a tailored, dynamic experience that enhances customer satisfaction and drives sales.

Using Shopify Liquid for Personalized Product Recommendations

One of the best ways to enhance the shopping experience and increase conversions is by providing personalized product recommendations.

With Shopify Liquid, you can create dynamic, custom recommendations based on what the customer is currently viewing, past purchase behavior, or even the items in their cart.

This not only improves user engagement but also increases the chances of upselling and cross-selling products.

Why Personalized Product Recommendations Work

Personalized recommendations give customers relevant options, helping them find complementary products or alternatives that match their preferences.

According to a report from Accenture, 91% of consumers are more likely to shop with brands that provide relevant offers and recommendations. Leveraging Shopify Liquid snippets to dynamically generate these recommendations helps build that personalized experience.

How to Set Up Dynamic Recommendations Using Shopify Liquid

Step 1: Display Related Products Based on Product Tags

The easiest way to suggest relevant products is to use Shopify Liquid to pull in items with similar tags or collections.

For example, let’s say you want to recommend products with the same tag as the product currently being viewed. You can do this by using the following Liquid snippet:

{% assign current_tags = product.tags %}
{% for related_product in collections.all.products %}
    {% if related_product.tags contains current_tags %}
       <div class="related-product">
         <a href="{{ related_product.url }}">
             <img src="{{ related_product.featured_image | img_url: 'medium' }}" alt="{{ related_product.title }}">
          <p>{{ related_product.title }}</p>
         <p>{{ related_product.price | money }}</p>
      </a>
   </div>
 {% endif %}
{% endfor %}

This snippet identifies the tags of the current product being viewed and then looks for other products in your store with matching tags.

These related products are dynamically displayed on the product page, encouraging customers to browse and consider additional purchases.

Step 2: Recommend Products Based on Customer Behavior

For a more personalized approach, you can create dynamic recommendations based on a customer’s previous purchases or browsing history.

You can use Shopify's customer object in Liquid to track and display items the customer has previously bought or viewed.

For instance, to show products from the same collection as a customer's previous purchase:

{% if customer %}
   <h3>Recommended for you</h3>
   {% for order in customer.orders %}
      {% for line_item in order.line_items %}
         {% assign previous_collection = line_item.product.collections.first %}
         {% for product in previous_collection.products %}
            <div class="recommended-product">
                <a href="{{ product.url }}">
                     <img src="{{ product.featured_image | img_url: 'medium' }}" alt="{{ product.title }}">
                    <p>{{ product.title }}</p>
                    <p>{{ product.price | money }}</p>
                 </a>
             </div>
        {% endfor %}
     {% endfor %}
   {% endfor %}
{% endif %}

This snippet displays items from the same collection as products the customer previously purchased. It’s a subtle, smart way to re-engage returning customers with options they’re more likely to be interested in.

Step 3: Use "Frequently Bought Together" Recommendations

Another effective strategy is to display products that are often purchased together. This is especially useful for increasing average order value (AOV). You can manually set these recommendations using Shopify product tags or collections.

<h3>Frequently Bought Together</h3>
<div class="frequently-bought">
    {% for product in collections.frequently_bought.products %}
       <a href="{{ product.url }}">
           <img src="{{ product.featured_image | img_url: 'small' }}" alt="{{ product.title }}">
           <p>{{ product.title }}</p>
           <p>{{ product.price | money }}</p>
        </a>
     {% endfor %}
</div>

This example assumes that you've created a collection named "Frequently Bought Together" that contains products related to the one being viewed. This is a simple way to suggest bundles or related products.

Optimizing Product Recommendations for Performance

While it’s tempting to load up your product pages with many personalized options, remember that page speed is critical for both user experience and SEO. To make sure your personalized recommendations don’t slow down the page:

  • Lazy load images for recommended products to delay loading until they are about to come into the viewport.
  • Limit the number of recommended products displayed at once to avoid overwhelming the customer and slowing down the page.

Make the Most of Shopify Liquid

Personalized product recommendations can make a huge difference in increasing customer engagement and driving more sales. By using Shopify Liquid snippets, you can create dynamic, real-time product suggestions that enhance your store's user experience.

Whether you're displaying related items, products from previous orders, or frequently bought together products, this added customization can help boost conversions and average order value.

If you're looking to enhance your Shopify store with advanced features like these, EcomExperts can help you get the most out of your store by setting up custom Liquid snippets that take your product pages to the next level.

FAQs

1. What are Shopify Liquid snippets?
Shopify Liquid snippets are reusable blocks of code that help you dynamically display content across your store, like product options or pricing. They allow for flexible customization of your Shopify theme without duplicating code.

2. How do Shopify Liquid snippets create dynamic product pages?
Liquid snippets enable dynamic product pages by allowing the page to change based on user actions, such as selecting different sizes, colors, or personalized options—without reloading the page.

3. Can Shopify Liquid snippets improve conversion rates?
Yes, dynamic product pages created with Shopify Liquid can improve user experience, leading to better engagement and higher conversion rates, as they offer a smoother and more personalized shopping journey.

4. Are Shopify Liquid snippets difficult to implement?
For those familiar with Shopify's theme editor, Liquid snippets are relatively easy to add. You can insert them into your product templates and use them to create dynamic content without needing to install heavy third-party apps.

5. Can Shopify Liquid snippets be used for personalized product recommendations?
Absolutely! Shopify Liquid allows you to display personalized product recommendations based on customer behavior or product tags, which can enhance user experience and increase average order value.

Back to blog