Metafields: data used to filter products in a custom section, rendering blank spaces

Topic summary

A user created a custom homepage section to display promotional products using start and end date metafields. Products are filtered by comparing these dates to the current date to determine visibility.

The Problem:

  • When products fall outside the date range (before start date or after end date), empty product cards render in the section instead of being hidden completely
  • Moving the filtering logic from card_product to the section liquid didn’t resolve the issue

Suggested Solutions:

  • Add an else clause to explicitly set show_product = false when date conditions aren’t met
  • Verify that show_product isn’t being overridden elsewhere in the theme
  • Check variable values (dates as strings vs numbers) by echoing them out
  • Disable JavaScript to test if it’s contributing to the empty cards

Important Considerations:

  • Dates in Liquid templates are subject to caching, potentially showing stale data to customers
  • Logic issue noted: start dates don’t natively exist for products, so encountering a product “before the start date” may be a conceptual problem

The discussion remains open with troubleshooting in progress.

Summarized with AI on November 18. AI used: claude-sonnet-4-5-20250929.

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.

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.