Open reggi opened 9 years ago
I wonder if this can be used to beat the 100 variant limit that Shopify has. Have a parent product, and instead of showing its own variants, show an unlimited amount of child products as the variants using metafields
@mennyg I'm not exactly sure, you'll just have to test it out and report back :)
Well, i tried. Im pretty new at this, so dont judge too much :) I did it a bit differently using metafields, but it didnt work. See here:
{% assign productHandleArray = product.metafields.global.productHandle | split:',' %}
{% for productHandle in productHandleArray %}
{% for variant in all_products[productHandle].variants %}
{% capture allvariants %}{{ allvariants | append: variant.id }}{% endcapture %}
{% capture allvariants %}{{ allvariants | append: ',' }}{% endcapture %}
{% endfor %}
{% endfor %}
{% assign product_variants = allvariants | split:',' %}
<div class="product-options">
<div class="select clearfix">
<select id="product-select" name="id">
{% for variant in product_variants %}
<option value="{{ variant.id }}"{% if variant == product.selected_or_first_available_variant %} selected="selected" {% endif %}>{{ variant.title }} - {{ variant_price | money }}
{% endfor %}
</select>
</div>
It seems to not be able to pull the variant information from the array :(
@mennyg to my knowledge there is no allvariants
variable within your capture.
Got it to work :) thanx to zakhardage for his option selector Here Shopify's option_selection.js didnt work for this)
<form action="/cart/add" method="post">
{% assign productHandleArray = product.metafields.global.productHandle | split:',' %}
{% assign productHandle = productHandleArray | first %}
{% if all_products[productHandle].options.size > 1 %}
{% if all_products[productHandle].options[0] %}
{% assign used = '' %}
<label for="select-one">{{ all_products[productHandle].options[0] }}</label>
<select id='select-one' onchange="letsDoThis()">
{% for productHandle in productHandleArray %}
{% for variant in all_products[productHandle].variants %}
{% unless used contains variant.option1 %}
<option value="{{ variant.option1 }}">{{ variant.option1 }}</option>
{% capture used %}{{ used }} {{ variant.option1 }}{% endcapture %}
{% endunless %}
{% endfor %}
{% endfor %}
</select>
{% endif %}
{% if all_products[productHandle].options[1] %}
{% assign used = '' %}
<label for="select-one">{{ all_products[productHandle].options[1] }}</label>
<select id='select-two' onchange="letsDoThis()">
{% for productHandle in productHandleArray %}
{% for variant in all_products[productHandle].variants %}
{% unless used contains variant.option2 %}
<option value="{{ variant.option2 }}">{{ variant.option2 }}</option>
{% capture used %}{{ used }} {{ variant.option2 }}{% endcapture %}
{% endunless %}
{% endfor %}
{% endfor %}
</select>
{% endif %}
{% if all_products[productHandle].options[2] %}
{% assign used = '' %}
<label for="select-one">{{ all_products[productHandle].options[2] }}</label>
<select id='select-three' onchange="letsDoThis()">
{% for productHandle in productHandleArray %}
{% for variant in all_products[productHandle].variants %}
{% unless used contains variant.option3 %}
<option value="{{ variant.option3 }}">{{ variant.option3 }}</option>
{% capture used %}{{ used }} {{ variant.option3 }}{% endcapture %}
{% endunless %}
{% endfor %}
{% endfor %}
</select>
{% endif %}
{% endif %}
<input type="hidden"name="id" id="product-select" value="{{ all_products[productHandle].variants.first.id }}" />
</form>
<script>
function letsDoThis() {
{% assign productHandleArray = product.metafields.global.productHandle | split:',' %}
{% assign productHandle = productHandleArray | first %}
{% if all_products[productHandle].options[0] %}var opt1 = document.getElementById('select-one').value;{% endif %}
{% if all_products[productHandle].options[1] %}var opt2 = document.getElementById('select-two').value;{% endif %}
{% if all_products[productHandle].options[2] %}var opt3 = document.getElementById('select-three').value;{% endif %}
var id = '';
{% for productHandle in productHandleArray %}
{% for v in all_products[productHandle].variants %}
if(opt1=="{{ v.option1 }}"{% if all_products[productHandle].options[1] %} && opt2=="{{ v.option2 }}"{% endif %}{% if all_products[productHandle].options[2] %} && opt3=="{{ v.option3 }}"{% endif %}) {
var id = {{ v.id }};
var price = "{{ v.price | money }}";
}
{% endfor %}
{% endfor %}
if(id!='') {
document.getElementById('product-select').value = id;
document.getElementById('price').innerHTML = price;
} else {
document.getElementById('product-select').value = '';
document.getElementById('price').innerHTML = 'Unavailable';
}
}
</script>
@mennyg Glad you got it working, thanks for posting the code here!
Spoke more about this here
The only way I have of beating that limit is storing collection
handles
in alinklist
like thispullProducts1
,pullProducts2
,pullProducts3
. Then you can loop over collections with 50 products in them.