2

I'm having a problem with wrong results when filtering order by date.

I use this code:

{% set orders = craft.commerce.orders.dateOrdered(' >= 2016-07-01 00:00:00').dateOrdered(' <= 2016-07-01 23:59:59').find() %}

The last order has been placed on 2016-06-29, and there has been no orders placed on 2016-07-01, so I would expect order to have no results. However it just lists all orders in the system.

If I change the date to 2016-06-10, the result is empty - as expected.

Is this bug in Craft / Craft Commerce, or do I miss something?

How do I make sure I get no results, if there have been no orders on this date?

1

Try something like:

{% set firstDayOfMonth = year ~ "-" ~ month ~ "-01" %}
{% set daysInThisMonth = firstDayOfMonth|date('t') %}
{% set lastDayOfMonth  = year ~ "-" ~ month ~ "-" ~ daysInThisMonth %}

{% set thisMonthsOrders = craft.commerce.orders.dateOrdered('and', '>= ' ~ firstDayOfMonth, '<= ' ~ lastDayOfMonth) %}
| improve this answer | |
  • @YuriSalimovskiy this won't return elements for the last day of the month. You need to add 23:59 as a time to lastDayOfMonth or use date_modify('first day next month') instead. – carlcs Jul 1 '16 at 21:33
  • My dates are provided with GET variable, so that's not a problem. My problem was solved with 'and' keyword – Yuri Salimovskiy Jul 2 '16 at 8:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.