【发布时间】:2021-08-12 21:41:46
【问题描述】:
我正在为 shopify 网站编写一些流动代码,以便根据使用产品句柄的标签将产品添加到任何博客文章中。我已经尝试过一种产品,它工作得很好,所以我试图通过一个循环对其进行迭代,但我无法从我创建的数组中获取任何信息。这是我到目前为止写的代码。你能帮我理解我做错了什么吗?谢谢!
{% comment %}declare variables{% endcomment %}
{% assign related_prod_index = 0 %}
{% assign related_prod_array = "" | split: ',' %}
{% comment %}check for tags that contains products handles{% endcomment %}
{% for tag in article.tags %}
{% if tag contains "product_"%}
{% assign prod_handle = tag | split:"_" %}
{% assign blog_prod = all_products[prod_handle[1]] %}
{% assign related_prod_array = related_prod_array | push:blog_prod %}
{% assign related_prod_index = related_prod_index | plus:1 %}
{% endif %}
{% endfor%}
{% comment %}check how many product tags I found{% endcomment %}
<h1>{{ related_prod_index }} tags found</h1>
{% comment %}loop that create small preview for each product{% endcomment %}
{% if related_prod_array %}
{% for rel_pr in related_prod_array %}
<img src="{{ rel_pr.featured_image | img_url:'original' }}">
<p><a href="{{ rel_pr.url}}">{{rel_pr.title}}</a></p>
<p><a href="{{ rel_pr.url}}">{{rel_pr.price | money}}</a></p>
{% endfor %}
{% endif %}
【问题讨论】: