Ad

Shopify: Change The Availability Of Variants

- 1 answer

In my case Shopify product has 5 variants: Monday, Tuesday, Wednesday, Thursday, Friday. I want to limit variants in 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.

Ad

Answer

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 variable

After 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:

{% assign day_of_week = 'now' | date: "%A" %}

{% for variant in product.variants %}
<!-- Addition below to filter out products that customer shouldn't see -->
  {% case day_of_week %}
    {% when 'Monday' %}
      {% if variant.title contains 'Monday' or variant.title contains 'Tuesday' %}{% continue %}{% endif %}
    {% when 'Tuesday' %}
      {% if variant.title contains 'Tuesday' or variant.title contains 'Wednesday' %}{% continue %}{% endif %}

<!-- Repeat for each day of the week -->
   {% endcase %}
  <!-- Regular code from your variant loop -->
Ad
source: stackoverflow.com
Ad