Hide custom field date after it is in the past

Solved

Hide custom field date after it is in the past

racso
Visitor
2 0 1

Hi,

 

Hope you guys can help me. I use a custom liquid on all product pages.

 

{% elsif product.metafields.custom.expected_date != blank %}
<p style="color: #ff9f0f;">Expected on stock date: {{ product.metafields.custom.expected_date | date: '%d-%m-%Y' }}

This works nicely but I want to hide this when the date is in the past. 

I have tried the following code but that does not work.

 

{% assign today_date = 'now' | date: '%s' %}
{% assign pre_date = product.metafields.custom.expected_date | date: '%s' %}
{% elsif product.metafields.custom.expected_date != blank and today_date < pre_date %}

<p style="color: #ff9f0f;">Expected on stock date:: {{ product.metafields.custom.expected_date | date: '%d-%m-%Y' }}</p>
{% endif %}

 

Any ideas on how to get this working?

 

Thanks!

Accepted Solution (1)

PaulNewton
Shopify Partner
7721 678 1626

This is an accepted solution.

Pages are cached on shopify so even if the comparison was working when you check it today the values being compared would never change in many instances of the page being served so in the future the stale values are what's compared.

 

If can be a bit more reliable using shopify-flow , such as with a schedule trigger , to toggle a metafield for the display;  which should trigger a cache update,  rather than try to rely on dates being cached. 

Alternatively use javascript and check the time with an external time service (don't trust the users browser time/date)

 

As for finding any issues in liquid code itself, sanity check assumptions and just raw output everything being used:

{% assign today_date = 'now' | date: '%s' %}
today_date : {{ today_date }}<br>
{% assign pre_date = product.metafields.custom.expected_date | date: '%s' %}
pre_date  : {{ pre_date }}<br>
{% if today_date < pre_date %}
 it's expected
{% else %}
 it's arrived
{% endif %}

 

 

Contact paull.newton+shopifyforum@gmail.com for the solutions you need


Save time & money ,Ask Questions The Smart Way


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Thank Paul with a Coffee for more answers or donate to eff.org


View solution in original post

Replies 3 (3)

PaulNewton
Shopify Partner
7721 678 1626

This is an accepted solution.

Pages are cached on shopify so even if the comparison was working when you check it today the values being compared would never change in many instances of the page being served so in the future the stale values are what's compared.

 

If can be a bit more reliable using shopify-flow , such as with a schedule trigger , to toggle a metafield for the display;  which should trigger a cache update,  rather than try to rely on dates being cached. 

Alternatively use javascript and check the time with an external time service (don't trust the users browser time/date)

 

As for finding any issues in liquid code itself, sanity check assumptions and just raw output everything being used:

{% assign today_date = 'now' | date: '%s' %}
today_date : {{ today_date }}<br>
{% assign pre_date = product.metafields.custom.expected_date | date: '%s' %}
pre_date  : {{ pre_date }}<br>
{% if today_date < pre_date %}
 it's expected
{% else %}
 it's arrived
{% endif %}

 

 

Contact paull.newton+shopifyforum@gmail.com for the solutions you need


Save time & money ,Ask Questions The Smart Way


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Thank Paul with a Coffee for more answers or donate to eff.org


racso
Visitor
2 0 1

Thanks @PaulNewton , that explains it then. 🙏

PaulNewton
Shopify Partner
7721 678 1626

Notes for inventory date behaviors in shopify-flow,

 

Good Hunting.

 

Contact paull.newton+shopifyforum@gmail.com for the solutions you need


Save time & money ,Ask Questions The Smart Way


Problem Solved? ✔Accept and Like solutions to help future merchants

Answers powered by coffee Thank Paul with a Coffee for more answers or donate to eff.org