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

<p>You could look to do the following:</p>

<pre><code>{% 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() %}
  &lt;ul&gt;
    {% for order in orders %}
      &lt;li&gt;{{ order.id }}&lt;/li&gt;
    {% endfor %}
  &lt;/ul&gt;
{% endcache %}
</code></pre>

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

CC BY-SA 4.0
2019-11-07T09:48:26.897Z
2019-11-07T09:48:26.897Z
nfourtythree
<p>I have a list of orders from Commerce 1 (Craft 2) in the frontend.</p>

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

<pre><code>{% set latestOrder = craft.commerce.orders.first() %}

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

<p>The <code>Last Updated</code> tag shows always the latest browser reload. What's the error here?</p>

<plugin-craftcommerce><craft2>
Invalidate cache on new order
# Invalidate cache on new order

(Asked by: username_0 on 2019-11-06)

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?

**Tags:** plugin-craftcommerce, craft2


---

## 1 answers

### Answer by username_1 on 2019-11-07. Score: 0 | Accepted answer

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() %}
  
    {% for order in orders %}
      {{ order.id }}
    {% endfor %}
  
{% 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.


---

