如何根据客户标签显示自定义"已登录"欢迎消息?(购物/液体)



我有一个从互联网上收集的工作脚本(我不是专家(,但我不确定它是否有效。但它似乎运行良好。

{% if customer %}
{% assign customer == 'new'}
{% for tag in customer.tags %}
{% if tag contains "wholesale" %}
{%assign customer == 'wholesale' %}
{% endif %}
{% if customer != 'wholesale' %}
<span>Wholsaler</span>
{% endif %}
{% else %}
<span>Visitor</span>
{% endif %}

但现在我有一个问题无法解决,我的老板希望我将液体脚本更改为如下内容:

  1. 如果访问者是"不是";登录后将出现一条消息";您没有登录">
  2. 如果访问者以"访客"身份登录;零售";,将出现一条消息";欢迎零售商">
  3. 如果访问者以"访客"身份登录;批发商";,将出现一条消息";欢迎批发商">

我无法让第三个脚本工作,如果有人能友好地解决我的问题,请给我指明正确的方向或正确的脚本编写方式?

您的代码试图用非客户对象的内容覆盖customer变量。您将需要创建一个新的变量名,以使您的脚本工作。例如,如果有登录的customer,则将该变量设置为等于单词new。单词new没有任何与之相关的标记,因此在for循环中不会发生任何事情。(此外,看起来你并没有用endfor关闭forloop(

我会提出的另一个建议是使用contains关键字而不是forloop来检查客户标签。contains可以用于简单的数组(如标签列表(,以检查是否有任何标签与提供的值完全相等,并且比forloop更干净、更高效,尤其是当客户有很多标签时。

最后的代码可能看起来像这样:

{% if customer %} 
{% comment %} Shopper is logged in, but are they special? {% endcomment %}
{% assign lowercase_tags = customer.tags | join: ',' | downcase | split: ',' %}
{% if lowercase_tags contains 'wholesale' %}
{% assign customer_message = 'Welcome, Wholesaler!' %}

{% comment %}OPTIONAL: We can use one or more elsif (Liquid's version of "else if") statements to check for any other special tags, if desired. {% endcomment %}
{% elsif lowercase_tags contains 'some-other-special-tag' %}
{% assign customer_message = 'Super Buddy' %}

{% else %} 
{% comment %} This will be the message for logged-in customers without any special tags {% endcomment %}
{% assign customer_message = 'Logged in as  ' | append: customer.name %}
{% endif %}
{% comment %} Print the appropriate welcome message {% endcomment %}
<span class="welcome-message">{{ customer_message }}</span>
{% else %}
{% comment %} Customer is not logged in {% endcomment %}
<span class="log-in-prompt">You are not logged in</span>
{% endif %}

加分:Shopify没有将上述客户信息硬编码到模板文件中,而是在locales文件夹中为您提供语言文件。将任何语言文本放入商店的主语言文件(通常为en.default.json,除非您更改了语言(将允许您在将来使用";自定义语言"选项。参考:https://shopify.dev/tutorials/develop-theme-localization-use-translation-keys

最新更新