Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
852 views
in Technique[技术] by (71.8m points)

shopify - Remove variants from variants array on product in Liquid

Good evening! I am trying to remove variants from the variant array on a product using pure Liquid(Shopify templating language). I would only like to use javascript as a last resort.

Below is where I am so far. Anything that is the variant in the if check needs to be removed from currentProduct.variants.

{% assign currentProduct = product %}
   {% for variant in currentProduct.variants %}
     {% include 'shappify-variant-is-csp' %}
     {% if csp_variant != 1 %}
        //need to remove the object that meets this if statement
     {% endif %}
{% endfor %}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm pretty sure you're going to need to use some javascript to achieve this. Take a look at this article on the Shopify wiki: How do I remove sold out variants from my options drop-downs.

Modifying the code in that article for your situation, you'll want something like this:

{% for variant in product.variants %}
   {% include 'shappify-variant-is-csp' %}
   {% if csp_variant != 1 %}
      jQuery('.single-option-selector option').filter(function() { return jQuery(this).html() === {{ variant.title | json }}; }).remove();
   {% endif %}
{% endfor %}
jQuery('.single-option-selector').trigger('change');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...