Caution: This is an advanced tutorial and is not supported by Shopify. This tutorial has been verified to work with Debut theme only. You can try to implement this on your theme, but keep in mind that it may not function. Knowledge of technologies such as HTML, CSS, JavaScript, and Shopify Liquid is required. We suggest hiring a Shopify Expert if you are not comfortable proceeding with the following tutorial.
Once you have completed the steps in this tutorial, a custom section will appear on the storefront:
Check out this demo product to see the tutorial result in action.
Step 1. Create a new Shopify Liquid snippet called related-products.liquid
From your Shopify admin, go to Online Store > Themes.
Find your current theme (or the theme you want to edit) and then click Actions > Edit code.
On the left side, click the Snippets heading to access your Snippets. Once you get the list of snippets, click the Add a new snippet link.
Name your snippet ‘related-products’. Click Create snippet:
Copy and paste the code listed below into your new ‘related-products.liquid’ snippet.
<div class="related-products-wrapper"> {%assign related_products = product.metafields.related_products.product_list.value %} {% if related_products %} <h3>You might also like:</h3> <div class = "related-product-list"> {% for related_product in related_products %} <a href = "{{shop.url | append: "/products/" | append: related_product.handle }}"> <div class = "related-product"> <div class = "related-product-image"> <img src = "{{related_product.featured_image | img_url: 'medium' }}" /> </div> <div class = "related-product-name">{{related_product.title}}</div> <div class = "related-product-price">{{related_product.price | money}}</div> </div> </a> {% endfor %} </div> {%endif%} </div> <div class = "related-products-dummy"></div>
Save the file.
Step 2. Include related-products.liquid in your product-template.liquid
On the left side of the screen (same section we used to find the Snippets), click the Sections heading to access your Sections.
Under the Sections heading, locate and click product-template.liquid to open your product template in Shopify built-in online code editor.
Find the perfect spot to place your related products block within the product page. In this tutorial we’ll place it right under the product description. Find the following block of your product-template.liquid file:
copy and paste the code listed below right under the product description section:
{% render "related-products" %}
Proudly hit Save button:
Step 3. Add CSS styles.
Now you may want to add some CSS rules to make the whole thing look pretty. Go to Assets section and find your theme.scss.liquid file
Open this file, copy and paste the code listed below and save changes.
// **************************************************** Related products styles start here **************************************************** // .related-products-wrapper { } .related-products-dummy { padding: 10 px; clear: both; } .related-product-list { margin-top: 15px; display: flex; gap: 15px; flex-wrap: wrap; } .related-product { display: flex; flex-direction: column; align-items: center; text-align: center; gap: 3px; max-width: 165px; } .related-product-image { height: 165px; width: 165px; display: flex; justify-content: center; align-items: center; } .related-product-image img { max-width: 100%; max-height: 100%; } .related-product-price { color: $color-body-text; } // **************************************************** Related products styles end here **************************************************** //
Step 4. Populate the metafields.
The custom functionality we implemented earlier is pretty easy to use and maintain. All you need to do to add a new related product to your product is to create create a new metafield within the namespace specified in your related-products.liquid Snippet file (the default namespace used in this tutorial is “related-products”). The key of the metafield may be any valid and unique key. The value of the metafield should contain a valid hande of the product you want to create relation with. For example, this is what the metafields used to add the custom relations for our demo product look like “under the hood”:
Was this article helpful?
That’s Great!
Thank you for your feedback
Sorry! We couldn't be helpful
Thank you for your feedback
Feedback sent
We appreciate your effort and will try to fix the article