Search code examples
shopifyliquidshopify-template

How can filter orders made after a certain date in liquid/ Shopify?


{% for orders in checkout.customer.orders %}
//count the orders 
{% endfor %}

I need to count orders made only after a certain date? How can I do this in Liquid / Shopify?


Solution

  • All Orders have a created_at date that you can output in various formats using the Liquid date filter — you would loop thru the Orders as above and compare that with whatever the "threshold date" in question is, using unix-format dates for comparison:

    {% assign ordersThresholdUnix = '2019-01-01' | date: '%s' %}
    {% assign ordersCount = 0 %}
    {% for orders in checkout.customer.orders %}
      {% assign orderDateUnix =  order.created_at | date: '%s' %}
      {% if orderDateUnix > ordersThresholdUnix %}
        {% assign ordersCount = 0 %}
      {% endif %}
    {% endfor %}
    

    You can then output {{ ordersCount }}.

    Note: that I don't think Shopify will allow you to paginate further back than 50 Orders.