D.K. Goutham Kishore
D.K. Goutham Kishore

Reputation: 29

How to load values from shop.metafields as options in a Shopify schema tag in liquid file?

I am working on a Shopify theme app that requires me to load configuration options from shop.metafields and use them as selectable options in a schema tag. I have tried to generate the options array inside the schema tag using a for loop, but it doesn't seem to be correct approach. Here's a simplified version of my code:

{% assign optionsArray = shop.metafields.global.convertcart_blockIds %};

{% schema %}
  {
    "name": "Recommended Products",
    "tag": "section",
    "target": "section",
    "settings": [
      {
        "type": "richtext",
        "id": "heading",
        "label": "Heading"
      }, {
        "type": "select",
        "id": "blockId",
        "label": "Block ID",
        "info": "Select an option",
        "options": [{% for option in optionsArray %}
            {
            "value": "{{ option }}",
            "label": "{{ option }}"
          }{% unless forloop.last %},
          {% endunless %}{% endfor %}]
      }
    ]
  }
{% endschema %}

I have tried constructing the options array in the script tag but still using that options array in schema was not possible. Can someone please help me figure out how to properly load the configuration options from shop.metafields and use them as selectable options in a schema tag? Any help or directions on this would be greatly appreciated. Thanks!

Upvotes: 1

Views: 1289

Answers (1)

David Lazar
David Lazar

Reputation: 11427

You ae probably not accessing the metafield correctly. First off, you want to ask for the namespace.key.value, and you skipped the value part, and secondly, you can store JSON natively in metafields, meaning you could skip the whole Liquid construct of the loop.

Upvotes: 0

Related Questions