skip to Main Content

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


  1. 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 -->
    
    Login or Signup to reply.
  2. 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:

    <script>
      jQuery(document).ready(function(){
        var variants_list = {{ product.variants | json }};
        var date = new Date(Date.now()).toDateString();
    
        for(var i=0; i < variants_list.length; i++){
          var variant = variants_list[i];
          if(variant.title == 'Monday' && (date.indexOf('Mon') > -1 || date.indexOf('Tue') > -1){
            jQuery('[value="' + variant.id + '"]').remove();
          //Note: If you theme uses swatches or anything like that, you may need to remove additional elements from the document as well.
          }
          if(variant.title == 'Tuesday' && (date.indexOf('Tue') > -1 || date.indexOf('Wed') > -1){
            jQuery('[value="' + variant.id + '"]').remove();
          }
        }
        //Lather-rinse-repeat for each day of the week that you care about
    
    }
    </script>
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search