Conversations about creating, managing, and using metafields to store and retrieve custom data for apps and themes.
I have created a custom section for the home page to feature products on promotion.
To manage this section in the simplest way, I have added start date and end date metafields which I compare to the current date to determine if the product should show in the section at this time.
Unfortunately, when the code encounters a product before the start date or after the end date it is rendering an empty product card in the section. I have tried several solutions including moving the code to the section liquid instead of in the card_product liquid to see if I could prevent this outcome.
Screenshot below.
Any ideas for how to solve this?
Code I am using:
{%- if card_product and card_product != empty -%}
{%- liquid
assign show_product = false
if card_product.metafields.custom.start_date and card_product.metafields.custom.end_date
assign start_date = card_product.metafields.custom.start_date | date: '%s'
assign end_date = card_product.metafields.custom.end_date | date: '%s'
assign current_date = 'now' | date: '%s'
if start_date <= current_date and end_date >= current_date
assign show_product = true
endif
endif
-%}
{%- if show_product -%}
Hottake, set the flag to false inside an else
if start_date <= current_date and end_date >= current_date
assign show_product = true
else
assign show_product = false
endif
Note that dates in liquid templates are subject to cache and thus customers getting stale data.
search forums discussions for this gotcha.
Disable javascript and check for the empty product cards, if missing then javascript is also involved.
Echo out the variables to ensure they are what you think they should be and whether numbers or strings.
Check that "show_product" isn't used elsewhere in the theme in way that sidesteps your setting it to false.
@colleenc wrote:
when the code encounters a product before the start date
Logic nitpick: The start date doesn't exist for products natively so you cannot encounter a product before the "start date" because it doesn't have one.
There is only the current_date in that regard.
Contact [email protected] 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