Shopify and Liquid: Get product in an array for loop of product variant SKUs

879 Views Asked by At

I'm trying to create a custom collection template on our Shopify store. Our business, every month, does a promotional update to our existing collections that ends up becoming a laborious chore with the manual collection curation of selecting individual SKUs. On our Wordpress/Woocommerce site, it's all easily handled with a comma separated list of of SKUs and the [products] shortcode.

I am trying to write a for loop with Liquid to loop through an array of SKUs, and display the product tile per SKU. The only thing is, I cannot for the life of me figure out how to target a specific product by product.variant.sku.

How I envision it from a simplistic view:

{% assign product_skus = "1234,5678,9101,1121" | split: ',' %}
{% for product_sku in product_skus %}
  {% if product.variant.sku == product_sku %}
    {% include "product-tile" %}
  {% endif %}
{% endfor %}

I know the above will not work, but I cannot find the proper direction in the documentation (or I'm just not understanding it correctly) to make this sort of thing work. I feel like it should be possible. Any help would be greatly apprecaited!

1

There are 1 best solutions below

1
Onkar On

I think you need to utilize the default input types to create a backend editor settings panel using input type product_list documentation link.

{
  "type": "product_list",
  "id": "product_list",
  "label": "Products",
  "limit": 12
}

and loop them using liquid one front end easily. add code settings_schema.json

enter image description here

it appears in the editor like this

enter image description here

Choose products enter image description here

get the same into using liquid code like this sample

{% for product in settings.product_list %}
   {{ product.title }}
{% endfor %}

enter image description here enter image description here

Read more about product object in Shopify here