加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

ruby – 如果产品标签包含 – 在shopify中

发布时间:2020-12-17 03:05:57 所属栏目:百科 来源:网络整理
导读:所以我基本上试图使用shopify的逻辑来显示图像,如果产品的标签包含“相关”字样 (有一个json查询可以获取包含’related-x’的标签,其中x是产品的名称,并使用它来显示相关产品.) 在Json查询之前是一个基本上表示“相关产品”的图像.我想做的是只在存在“相关
所以我基本上试图使用shopify的逻辑来显示图像,如果产品的标签包含“相关”字样

(有一个json查询可以获取包含’related-x’的标签,其中x是产品的名称,并使用它来显示相关产品.)

在Json查询之前是一个基本上表示“相关产品”的图像.我想做的是只在存在“相关”标签时显示.

我试过这个:

{% if product.tags contains 'related' %}              
          <img src="{{ 'complete-this-look.gif' | asset_url }}" alt="" align="left;" style="vertical-align:top; margin-right:8px; padding-top:0px;" width="130" height="175"/>
{% endif %}

哪个没有显示任何东西.我也尝试过:

{% for t in product.tags %}
{% if t contains 'related-' %}
<img src="{{ 'complete-this-look.gif' | asset_url }}" alt="" align="left;" style="vertical-align:top; margin-right:8px; padding-top:0px;" width="130" height="175"/>
{% endif %}
{% endfor %}

但是,每次查询返回相关产品时,这将显示图像.

它后面是什么(图像)(查询结果) – 如果没有查询结果,那么它什么都不显示.

有任何想法吗?

解决方法

你的第一段代码无效的原因是因为contains正在寻找一个名为’related’的标签,而不是一个包含子字符串’related’的标签.

请参阅其声明的Shopify Wiki for contains:

It can check for the presence of a string in another string,or it can check for the presence of a string in an array of simple strings.

在您的实例中,contains正在检查简单字符串数组中的字符串(并且正在查找整个字符串,而不是包含指定字符串作为子字符串的字符串).

另见Shopify wiki for product.tags:

Returns a list of the product’s tags (represented by simple strings).

You can use the contains keyword with an array of simple strings,so
you can use this with a product’s tags:

{% if product.tags contains 'custom to order' %}
<!-- Output custom to order form HTML here -->
{% endif %}

所以,Gerard Westerhof建议在上面的评论中使用Join是一个很好的建议.如果您首先加入product.tags数组,那么contains将搜索join返回的标记字符串中的“相关”字符串.

试试这个:

{% if product.tags | join ' ' contains 'related' %}
    <img src="{{ 'complete-this-look.gif' | asset_url }}" alt="" align="left;" style="vertical-align:top; margin-right:8px; padding-top:0px;" width="130" height="175"/>
{% endif %}

编辑:

试试这个:

{% assign product_tags_string = product.tags | join ' ' %}
{% if product_tags_string contains 'related' %}
    <img src="{{ 'complete-this-look.gif' | asset_url }}" alt="" align="left;" style="vertical-align:top; margin-right:8px; padding-top:0px;" width="130" height="175"/>
{% endif %}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读