In my case Shopify product has 5 variants: Monday, Tuesday, Wednesday, Thursday, Friday
.
I want to limit variants within 48 hours. For example:
For Monday – Wednesday, Thursday, Friday
For Wednesday – Monday, Tuesday, Friday
For Friday – Monday, Tuesday, Wednesday, Thursday
I’m wondering if I can use standard functions. Through the admin panel or internal structure of Shopify.
2
Answers
You can filter out variants that you don’t want to display using Liquid, preventing shoppers from being able to add those products to the cart.
In your product template, find the line
{% for variant in product.variants %}
Before this line, add
{% assign day_of_week = 'now' | date: '%A' %}
to store the current day-of-week in a Liquid variableAfter this line, add code to filter out the variants that you do not want to see (below). For each day of the week that you care about, you will need a ‘when’ statement followed by a check against some property of the variant (I used the variant title). If it’s a variant that you do not want to show, use the ‘continue’ statement to skip that variant and go on to the next.
Example:
Separate from my other answer, you can also filter out variants using Javascript. I generally prefer pre-filtering variants through Liquid – relying on Javascript may result in a flicker on page load as the variants are there briefly before the code runs.
Assuming that jQuery is already on the page, we could write this as: