Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
905 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 Answer

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');

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...