Ad

Shopify - Using Product Type In Static Section Block, Unable To Display Value

- 1 answer

I have a static section with blocks that allow the merchant to choose from a list of products. I have the blocks working in the dashboard as expected, but I can't figure out how to display the values of each of the products.

This is what my schema looks like:

{
  "name": "New Arrivals",
  "max_blocks": 4,
  "blocks": [
    {
      "type": "new-arrival",
      "name": "New Arrival",
      "settings": [
        {
          "type": "product",
          "id": "product",
          "label": "Product"
        }
      ]
    }
  ]
}

Here's a simplified version of my for loop:

{% if section.blocks.size > 0 %}
        {% for block in section.blocks %}
            <!-- Individual product values here -->
        {% endfor %}
    {% endif %}

Normally, in order to retrieve block content I'd use something like this:

{{ block.settings.product.title }} 

But there must be a different way of getting the product information when using the product type like this.

Ad

Answer

You can use: {%- assign product = all_products[block.settings.product] -%}

That will grab your product from the block. Then use product as needed for other properties like title and price. Eg: {{ product.title }}

Ad
source: stackoverflow.com
Ad