【问题标题】:IF statement that shows a different text according to the shopping cart amount根据购物车金额显示不同文本的 IF 语句
【发布时间】:2016-09-06 12:29:11
【问题描述】:

我需要在我的购物车上创建一个 IF 语句脚本,它会根据购物车中的数量显示不同的文本。目前我只有两个选择。如果别的。我如何制作几个在特定金额上发生变化的 IF 语句?

到目前为止,我已经做了这个,它只适用于低于/超过 1000 的金额:

{% assign totalCart = 0 %}
            {% for item in cart.items %}
            {% assign totalCart = totalCart | plus:item.totalCart_raw %}
            {% endfor %}

    {% comment %}
If the amount 
{% endcomment %}

{% if totalCart >= 1000 %}

               Volume Discount 20% 

                {%else%}        

               Volume Discount available 30% 
            {%endif%}

【问题讨论】:

  • 您可以使用case + when 语句吗?或者可能是链 if + elsif + else 取决于你到底想要做什么。编辑:Documentation
  • @UnholySheep case+when 语句是最好的选择,当我需要 5 个不同的文本时,取决于数量?
  • 最有可能,尽管这取决于。 case + when 只能进行相等比较,不能进行更大/更小的比较。在这种情况下,您将不得不使用 if + elsif + else
  • 好的,我需要 5 个不同的更大/更小比较。如何使用 5 种不同的 if + elseif + else 语句编写代码?你能帮我举个例子@UnholySheep吗?

标签: shopping-cart liquid


【解决方案1】:

if+elsif的使用示例(根据问题中的代码)

{% assign totalCart = 0 %}
            {% for item in cart.items %}
            {% assign totalCart = totalCart | plus:item.totalCart_raw %}
            {% endfor %}

    {% comment %}
If the amount 
{% endcomment %}

{% if totalCart < 1000 %}
Volume Discount 20% use this code 
{% elsif totalCart < 2000 %}
Volume Discount available 30% use this code
{% elsif totalCart < 3000 %}
Volume Discount available 40% use this code
{% elsif totalCart < 4000 %}
Volume Discount available 50% use this code
{% elsif totalCart > 4000 %}
Volume Discount available 60% use this code
{% endif%}

【讨论】:

  • 完美。我做了一些小的修改,但这完美地解决了我的问题。不知道我可以只使用几个 elseif。感谢您的帮助和时间。
猜你喜欢
  • 2017-12-11
  • 2013-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-18
  • 2013-06-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多