我一直在尝试在Shopify上为我的所有产品实现一个自动标签。它的工作原理是,创建的标签从产品设计中提取信息。如果说明书上写着“预定前2-3周内装运”。或“预购船2021年1月”它将拉动在“船舶”或“船只”一词之后出现的任何东西。因此,标签显示为:
weeks
**2-3
标签用于在整个站点插入估计的运送时间。
现在的问题是,有些标签在"preorder**“之后有一个空格,我们需要确保没有空格。对如何修复有什么想法吗?它所基于的主体HTML是如果它包含“预购。装运”。以下是我们的当前代码:
{% assign body = product.body_html | strip_html | newline_to_br %}
{% assign keywords = "Pre-order. Ships" %}
b{% assign tags = '' %}
{% if body contains keywords %}
{% assign lines = body | split: "<br />" %}
{% for line in lines %}
{% if line contains keywords %}
{% assign tokens = line | split: keywords %}
{% if tokens.size == 2 %}
{% assign inner_tokens = tokens[1] | split: "." %}
{% if inner_tokens.size >= 1 %}
{% assign value = inner_tokens[0] | replace: "in", "" | escape_once | replace: " ", "" %}
{% assign tag = "preorder**" | append:value | replace_first: " ", "" %}
{% assign tags = tags | append: tag | append: ',' %}
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{{ tags }}发布于 2021-01-13 01:45:05
问题
这个问题之所以发生,是因为“船”比“船”多一个空间。
解决方案
您需要删除"in“之后的空格。
您可以通过修改replace操作符来轻松地完成这一任务。
代码
replace: "in", ""变成replace: "in ", "" (注意空格)。
您的代码变成:
{% assign body = product.body_html | strip_html | newline_to_br %}
{% assign keywords = "Pre-order. Ships" %}
b{% assign tags = '' %}
{% if body contains keywords %}
{% assign lines = body | split: "<br />" %}
{% for line in lines %}
{% if line contains keywords %}
{% assign tokens = line | split: keywords %}
{% if tokens.size == 2 %}
{% assign inner_tokens = tokens[1] | split: "." %}
{% if inner_tokens.size >= 1 %}
{% assign value = inner_tokens[0] | replace: "in ", "" | escape_once | replace: " ", "" %}
{% assign tag = "preorder**" | append:value | replace_first: " ", "" %}
{% assign tags = tags | append: tag | append: ',' %}
{% endif %}
{% endif %}
{% endif %}
{% endfor %}
{% endif %}
{{ tags }}https://stackoverflow.com/questions/65694400
复制相似问题