为什么我在 Timber/Twig 中的运算符不适用于我的条件语句?

Why are my operators in Timber/Twig not working for my conditional statements?

我在我正在开发的自定义 WordPress 主题中 运行 Timber/Twig 中的多个 ifelseifand 语句。但我似乎在正确评估这些陈述时遇到问题,尤其是涉及日期和多个 and - 我只是想知道我是否可能没有正确地做,或者我不能使用多个 and 在声明中 ??这是我的示例数据:

{% if datenow < launchdate %}
  {# Do not show Products #}
{% elseif datenow > launchdate and post.product_widget.stock_qty > '0' %}
  {# Display the Products #}
{% elseif post.product_widget.stock_qty < '500' %}
  {% if post.product_widget.stock_qty < '400' and datenow < seasonend %}
    {# Run a 10% discount until stock drop below 200 items #}
  {% elseif post.product_widget.stock_qty < '200' and datenow < seasonend %}
    {# Price products back at regular price #}
  {% elseif post.product_widget.stock_qty < '100' and datenow < seasonend and post.sale_status %}
    {# Display products banner and bump to top of site #}
  {% elseif post.product_widget.stock_qty < '50' and datenow < thanksgvng and datenow < seasonend %}
    {# Get another 100 items to last until season ends #}
  {% elseif post.product_widget.stock_qty > '10' and datenow < thanksgvng and datenow < seasonend %}
    {# Display shortage on hand and urge to buy now #}
  {% elseif post.product_widget.stock_qty > '40' and datenow > thanksgvng and datenow < seasonend %}
    {# Run Sale of the season on remaining stock #}
  {% elseif post.product_widget.stock_qty < '10' and datenow > thanksgvng and datenow < seasonend %}
    {# Double the discount on remaining stock #}
  {% elseif post.product_widget.stock_qty < '0' and datenow > thanksgvng and datenow < seasonend %}
    {# Thank-you but we are now out of stock on this item, please check back next year! #}
  {% elseif post.product_widget.stock_qty < '0' and datenow > thanksgvng and datenow > seasonend %}
    {# Hide product and remaining inventory #}
  {% else %}
    {# Display Error Code #}
  {% endif %}
{% else %}
  {# Display Error Code #}
{% endif %}

如果我使用 <= 或 >= 或用括号括起来,这似乎没有什么不同 {% if (post.product_widget.stock_qty < '400') and (datenow < seasonend) %} 任何帮助和启发都会让我松一口气,因为这个问题有点令人沮丧。

~谢谢!

第一个问题是(可能)您的日期是字符串。在比较之前先将它们转换为日期。也没有必要将当前日期存储在变量中

{% if date("NOW") < date(launchdate) %}
   {# do stuff #}
{% endif %}

第二个问题是,如果 stock 大于 0

,您的第二个 elseif 将永远不会被触发

第三个问题有点类似于第一个问题,如果一只股票小于 X,if 将启动,所有其他 elseif 块将不会被触发。你必须扭转那些周围或添加额外的约束。


如果尝试将您的代码简化为以下代码段并在逻辑上也进行了一些调整,尤其是针对短缺消息。我想你无论如何都想在股票跌破某个阈值时显示此消息。

{% if date("NOW") > launchdate|date %}
    {% if post.product_widget.stock_qty > 0 %}
        {% if date("NOW") < date(seasonend) %}
            {% if date("NOW") > date(thanksgvng) %}
                {% if post.product_widget.stock_qty < 10 %}
                     Double the discount on remaining stock 
                {% elseif post.product_widget.stock_qty < 40 %}
                     Run Sale of the season on remaining stock 
                {% endif %}
            {% else %}
                {% if post.product_widget.stock_qty < 50 %}
                     Get another 100 items to last until season ends 
                {% elseif post.product_widget.stock_qty < 100 %}
                     Display products banner and bump to top of site 
                {% elseif post.product_widget.stock_qty < 200 %}
                     Price products back at regular price 
                {% elseif post.product_widget.stock_qty < 400 %}
                     Run a 10% discount until stock drop below 200 items 
                {% else %}
                     Show normal situation 
                {% endif %}

                {% if post.product_widget.stock_qty < 10 %}
                     Display shortage on hand and urge to buy now 
                {% endif %}
            {% endif %}
        {% endif %}
    {% elseif date("NOW") < date(seasonend) %}
         Thank-you but we are now out of stock on this item, please check back next year! 
    {% endif %}
{% endif %}

demo

在演示中替换了 date("NOW"),因此您可以尝试测试代码段


我也提到了添加约束作为解决方案,这就是我的意思,

{% if post.product_widget.stock_qty <= 400 and post.product_widget.stock_qty > 200 %}
    Between 400 and 200
{% elseif post.product_widget.stock_qty <= 200 and post.product_widget.stock_qty > 100 %}
    Between 200 and 100
{% else %}
    Lesser than 100
{% endif %}

demo