ShrockCo
ShrockCo

Reputation: 387

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

I am running a number of if, elseif and and statements in Timber/Twig in my custom WordPress theme that I am developing. But I seem to be having issues properly evaluating these statements especially with dates involved and the multiple and's - I am just wondering if I am maybe not doing it correctly or maybe I can't use more than one and in a statement ?? Here is a my sample data:

{% 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 %}

It doesn't seem make a difference if I was using <= or >= or wrapping in parentheses {% if (post.product_widget.stock_qty < '400') and (datenow < seasonend) %} any help and enlightment will be a great relief as this issue has been a bit of discouragement.

~ Thank-you!

Upvotes: 0

Views: 215

Answers (1)

DarkBee
DarkBee

Reputation: 15634

First issue is (probably) that your dates are strings. Convert them to a date first before comparing. Also it's not necessary to store the current date in a variable

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

Second issue is that your second elseif will never be triggered if the stock is greater than 0

Third issue is kinda similar to the first one, if a stock is lesser than X the if will kick in and all the other elseif blocks won't be triggered. You have to reverse those around or add an extra constraint.


If tried to simplify your code to the following snippet and made some adjustments in the logic as well, especially for the shortage message. I imagine you want to show this message when the stock falls below a certain threshold no matter what.

{% 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

Replaced date("NOW") in demo so you can play around to test the snippet


I also mentioned adding a constraint as a solution as well, this is what I mean with this,

{% 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

Upvotes: 1

Related Questions