skip to Main Content

I have some variants I’m looping over and then assigning the whole thing to a variable

   {% if settings.limit_quantity or settings.display_inventory_left %}
      {% assign variantInventory = data-variant-inventory:'[
       {% for v in product.variants %}
         {
           "id":{{ v.id }},
           "inventory_quantity":{{v.inventory_quantity}},
           "inventory_management":"{{- v.inventory_management -}}",
           "inventory_policy":"{{ v.inventory_policy }}"
         }
         {% if forloop.last == false %},{% endif %}
       {% endfor %}
    ]'%}
  {% endif %}

This is then put onto the form as an attribute

   {% form 'product', product, id: "product-form-{{ product.id }}", class: "clearfix product_form init {{cssClass}}", variantInventory, data-product-id:"{{ product.id }}"%}

But I’m getting error Liquid syntax error (line 21): ‘endfor’ is not a valid delimiter for if tags. use endif

2

Answers


  1. The problem with your approach is that you are thinking about it like it’s JavaScript.

    First things first:

    1. You can’t run an assign and a forloop at the same time. You can do things like:
      assign myVar = 'hello world' | replace: 'world', '' But that’s it. You can read more about pipelines here:
      https://shopify.dev/docs/api/liquid/filters
    2. You can’t create arrays in liquid that way. They can be created but it’s tricky:
      assign myArray = 'apple,orange,strawberry' | split: ','. Now you will be able to iterate over your "Array".

    So to answer your question:

    {% if settings.limit_quantity or settings.display_inventory_left %}
      {% assign variantInventory = "" %}
      {% for v in product.variants %}
        {% capture variant %}
          {
            "id": {{ v.id }},
            "inventory_quantity": {{ v.inventory_quantity }},
            "inventory_management": "{{ v.inventory_management }}",
            "inventory_policy": "{{ v.inventory_policy }}"
          }{% unless forloop.last %},{% endunless %}
        {% endcapture %}
        {% assign variantInventory = variantInventory | append: variant %}
      {% endfor %}
    {% endif %}
    

    Once you have the variantInventory variable assigned, you can proceed to use it in your form attribute or any other required context.

    Login or Signup to reply.
  2. This should do the trick (not tested):

       {%- if settings.limit_quantity or settings.display_inventory_left -%}
          {%- capture variantInventory -%}
           data-variant-inventory:'[
           {%- for v in product.variants -%}
             {
               "id":{{ v.id }},
               "inventory_quantity":{{v.inventory_quantity}},
               "inventory_management":"{{- v.inventory_management -}}",
               "inventory_policy":"{{ v.inventory_policy }}"
             }
             {%- unless forloop.last -%},{%- endunless -%}
           {%- endfor -%}
           ]'
         {%- endcapture -%}
      {%- endif -%}
    

    Then you may use it in your form:

    <form data-variant-inventory="{{ variantInventory }}"></form>
    

    HTH

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search