问题描述
在我正在开发的自定义 wordpress 主题中,我在 Timber/Twig 中运行了许多 if
、elseif
和 and
语句。但是我似乎在正确评估这些语句时遇到了问题,尤其是涉及日期和多个 and
时 - 我只是想知道我是否做错了或者我不能使用多个 {{ 1}} 在声明中 ??这是我的示例数据:
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 date("NOW") < date(launchdate) %}
{# do stuff #}
{% endif %}
第二个问题是,如果 elseif
大于 0,您的第二个 stock
将永远不会被触发
第三个问题有点类似于第一个问题,如果股票小于 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 %}
替换了演示中的 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 %}