PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, July 1, 2022

[FIXED] How to call option.value in the loop of product.variants in Shopify?

 July 01, 2022     shopify     No comments   

Issue

I have one hidden dropdown and two variant-selector dropdowns.

One dropdown is for color and the second one is for size.

I want to add size data in data-size="" and color data in data-color="" in the dropdown of name=id.

<form method="post" action="/cart/add">          
 {% if product.variants.size > 1 %} 
 {% for option in product.options_with_values %}          
    <select  class="variant-selector {{option.name}}" data-var="{{forloop.index}}">
       {% if product.available %}
       {% for values in option.values %}
          <option value="{{values}}">{{values}}</option>
       {% endfor %}
       {% endif %}
    </select>
 {% endfor %}
          
 <select name="id" style="display:none;" id="data{{forloop.index}}">
 {% for variant in product.variants %}
    {% if variant.available %}
       <option data-size="" data-color="" value="{{ variant.id }}"  >{{ variant.title }}</option>
    {% endif %}
 {% endfor %}
 </select>
 {% if variant.available%}
    <input type="hidden" min="1" type="number" name="quantity" value="1" />
    <input type="submit" value="{{ 'products.product.add_to_cart' | t }}"  class="btn add-to-cart" />
 {% else %}
    <input type="submit" value="{{ 'products.product.sold_out' | t }}"  class="btn add-to-cart" disabled="disabled">
 {% endif %}
 {% endif %}
 </form>


Solution

As Dave B comment I don't need option.value in the loop of product.variants in Shopify. I simply add data-size="{{ variant.option1 }}" and data-color="{{ variant.option2 }}"

<select name="id" style="display:none;" id="data{{forloop.index}}">             
 {% for variant in product.variants %}
    {% if variant.available %}             
       <option data-size="{{ variant.option1 }}" data-color="{{ variant.option2 }}" value="{{ variant.id }}"  >{{ variant.title }}</option>             
    {% endif %}
 {% endfor %}             
</select>



Answered By - Anupam Mistry
Answer Checked By - Terry (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing