Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
898 views
in Technique[技术] by (71.8m points)

liquid - Shopify' s all_products[ ] does only accept title

For a client I'm building a way to choose what products are shown in the recommended products section on the product page. To achieve this I thought about inserting the handles from the products I'd like to display into the tag part of the product.

So I'm able to loop over all the tags from the product and then for every tag I'd take the product object and display it. The problem is this code doesn't seem to work:

{% for tag in tags %}
  {% for single_product in all_products[tag] %}
      {{ single_product }}
  {% endfor %}
{% endfor %}

This code does work:

{% for tag in tags %}
  {% for single_product in all_products[tag].title %} <---- Added .title
      {{ single_product }}
  {% endfor %}
{% endfor %}

Sadly, I need the entire product object to display it instead of only the product title. How do I achieve this in shopify?

Side info: This code is placed inside of the framework--product-recommendations.liquid file

question from:https://stackoverflow.com/questions/65904711/shopify-s-all-products-does-only-accept-title

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The solution was using the collection instead of the all_product. I create a collection containing all product and was perfectly able to loop over it.

My code looks like this now:

          {% for tag in tags %}
            {% for single_product in all_the_products %}
                {% if tag == single_product.handle %}
                    <div class="product-recommendations--item">
                    {%
                    render 'framework--product--item',
                    product: single_product,
                    view: 'grid'
                    %}  
                    </div>
                {% endif %}
            {% endfor %}
          {% endfor %}

all_the_products is assigned to a collection containing all the products.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...