How to Add Variant Selector in Shopify on Collection Pages

Shopify is a popular e-commerce platform that provides a flexible and intuitive interface for online store owners.

One of the key features of Shopify is the ability to add variant selectors to the product pages, which allow customers to choose different variations of the same product.

In this tutorial, we will show you how to add variant selectors to the collection pages in Shopify.

This will enable customers to easily select the desired variant without having to navigate to the product page, making the purchasing process smoother and more efficient.

Before we start, it’s important to note that this guide assumes that you have already set up your variants and options in the Shopify admin panel.

If you have not yet done this, you can follow Shopify’s guide on how to add product variants.


Step 1: Add Liquid Code to Collection Page

The first step to adding variant selectors to your collection pages is to add the necessary Liquid code to your collection template.

Liquid is the template language used by Shopify to display your store’s content.

To add the code, go to your Shopify admin panel and click on the “Online Store” button.

Then, select “Themes” and click on the “Actions” button, followed by “Edit code”.

In the templates directory, find the file named “collection.liquid” and click on it to open it in the editor.

Next, paste the following code into the file, wherever you would like the variant selector to appear:

{% for product in collection.products %}
  <h2>{{ product.title }}</h2>
  {% if product.options.size == 1 %}
    {% assign option = product.options.first %}
    {% assign option_name = option.name | downcase %}
    {% assign option_values = '' %}
    {% for value in option.values %}
      {% assign option_values = option_values | append: value | append: ',' %}
    {% endfor %}
    <select class="{{ option_name }}">
      {% for variant in product.variants %}
        <option value="{{ variant.id }}">{{ variant.title }} - {{ variant.price | money }}</option>
      {% endfor %}
    </select>
  {% endif %}
{% endfor %}

Step 2: Style the Variant Selector

Now that you have added the code to your collection template, the next step is to style the variant selector to match the look and feel of your store.

You can do this by adding CSS to your theme’s stylesheet.

To add the styles, go to the “Assets” directory in your theme files and click on the “Add a new asset” button. Choose “CSS file” and name it “variant-selector.css”.

Next, paste the following code into the file:

select {
background-color: #fff;
border: 1px solid #d3d3d3;
border-radius: 5px;
padding: 10px;
width: 100%;
box-sizing: border-box;
font-size: 16px;
}

Step 3: Link the Stylesheet

Finally, link the stylesheet in your theme’s head.liquid file to apply the styles to your variant selector.

To do this, go to the “Layout” directory in your theme files and open the “head.liquid” file. Then, paste the following code into the file:

<link rel="stylesheet" href="{{ 'variant-selector.css' | asset_url }}">

And that’s it! Your variant selectors should now be displayed on your collection pages and styled to match your store’s design.


Conclusion

Adding variant selectors to your collection pages in Shopify can greatly improve the customer experience and make it easier for them to purchase the product they want.

By following the steps outlined in this post, you can easily add variant selectors to your collection pages and style them to match your store’s design.

If you have any questions or issues, don’t hesitate to reach out to the Shopify support team for assistance.