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 -%}
