Home > Enterprise >  How to append number after liquid block object in Shopify?
How to append number after liquid block object in Shopify?

Time:08-10

I want to use block.settings.product_1 up to number 6 without repeating my code. Below is my code with schema. I use for loop for that but its not working.

{% for block in section.blocks   %}
  {% for i in (1..4) %}   
    {% assign pro = block.settings.product_| append: i %} 
       {% assign pro1 = pro | remove:'"'  %}
       {{ pro1 }}
       {% assign product1 = all_products[pro1] %}
       { product1.title | strip_html}} 
    {%endfor%}
{%endfor%}

{% schema %}
"blocks": [
{
"type": "WholeSale",
"name": "WholeSale",
"settings": [
{
"type": "product",
"label": "product1",
"id": "product_1"
},
{
"type": "product",
"label": "product2",
"id": "product_2"
},
{
"type": "product",
"label": "product3",
"id": "product_3"
},
{
"type": "product",
"label": "product4",
"id": "product_4"
}
]
{% endschema %}

CodePudding user response:

Instead of adding pipe line after the block.settings I use square bracket. Now it's working.

{% for block in section.blocks   %}
  {% for i in (1..4) %}   
    {%  assign produ_name = 'product_' | append: i %}
    {% assign var_pro = block.settings[produ_name]  %}
    {% assign product1 = all_products[var_pro] %}
      {{ product1.title }}
    {%endfor%}
{%endfor%}

  • Related