On my Products details page I have 2 selects. The 1st select list out the Primary variants. Based on the selected Primary variant the 2nd select is auto-completed in with Secondary variants that have the same Primary variant.

I am doing setting this initially via an array:

{% set myVariants = [] %}

{% for variant in product.variants %}

{% set variantId = variant.id %}
{% set Primary = variant.primary %}
{% set Secondary = variant.secondary %}

{% if Primary not in myVariants | keys %}
    {% set myVariants = myVariants | merge({
        (Primary): [Secondary]
    }) %}
{% else %}
    {% set myVariants = myVariants | merge({
        (Primary): myVariants[Primary] | merge([Secondary])
    }) %}
{% endif %}

{% endfor %}

This outputs an array like so when dumped:

array(5) { 
  ["0.375"]=> array(2) { 
    [0]=> string(5) "0.125" 
    [1]=> string(5) "0.415" 
  } 
  ["0.500"]=> array(1) { 
    [0]=> string(5) "0.768" 
  } 
  ["0.625"]=> array(1) { 
    [0]=> string(5) "1.151" 
  } 
}

So based on the selected Primary and Secondary variants a specific variant ID should be pulled.

I've tried starting with the variant IDs, then passing the Primary variant to reproduce a similar array like above but it doesn't put them in a 2nd array.

What am I missing?

Also, based on the selection of Primary and Secondary, not only will the ID need to be pulled but also 2 additional variants and the sku; each of which are specific to that variant.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.