0

I have a list of orders from Commerce 1 (Craft 2) in the frontend.

I want to invalidate the cache when a new order is placed but it doesn't work:

{% set latestOrder = craft.commerce.orders.first() %}

{% cache until latestOrder.dateOrdered  %}
   Last Updated: {{ orders.dateOrdered.first() | date('D d.m.Y - H:i') }}
   {# stuff #}
{% endcache %}

The Last Updated tag shows always the latest browser reload. What's the error here?

0

In your code you are only caching until the dateOrdered of the latest order. That means that viewing the page will always be ahead of the cache time.

You could look to do the following:

{% set latestOrder = craft.commerce.orders.order('dateOrdered DESC').first() %}
{% set cacheKey = 'orders-' ~ latestOrder.dateOrdered|date('U') %}

{% cache using key cacheKey %}
  {% set orders = craft.commerce.orders.order('dateOrdered DESC').find() %}
  <ul>
    {% for order in orders %}
      <li>{{ order.id }}</li>
    {% endfor %}
  </ul>
{% endcache %}

This creates a cache key based on the date of the latest order. Therefore, when there is a new order this key will change.

| improve this answer | |

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.