Re: Get a metafield value into product selection

Get a metafield value into product selection

Gonkas
Shopify Partner
11 0 1

Hi,

Trying to get the variant dropdown pre-selected with a product metafield.

This is the code 

 

{% assign my_variant = product.metafields.global.best_selling_variant %} 
{% if my_variant.inventory_quantity < 1 or my_variant.value = 'initial_setup' %} 
  {% assign my_variant = product.selected_or_first_available_variant.id | append: "" %} 
{% endif %} 
<select id="product-select" name="id"> 
{% for variant in product.variants %} 
  {% assign variant_id = variant.id | append: "" %} 
  {% if variant.inventory_policy == "deny" %} 
    {% if variant.inventory_quantity > 0 %} 
	  <option {% if variant_id == my_variant %} selected{% endif %} value="{{ variant.id }}">
        {{ variant.title }}
      </option> 
	{% endif %} 
  {% else %} 
    <option {% if variant_id == my_variant %} selected{% endif %} value="{{ variant.id }}">
      {{ variant.title }}
    </option> 
  {%endif %} 
{% endfor %} 
</select>

 

replacing the default

 

{% if variant == product.selected_or_first_available_variant %} selected{% endif %}

 

Does anyone see anything obvious that is missing my eye?

 

Cheers,

Gonkas

Replies 6 (6)

Xythia
Shopify Partner
18 2 0

Don't have any good answer for you but do you really need this "{% if variant.inventory_policy == "deny" %}". It seems abundant and possible to cause issues?

There should only be "deny" and "continue" but if I remember correctly, there are people that got that it was undefined.

 

Also I realise this only matter if you get it to work but regarding that else statement. Do you

really want the customer to get to the variant that is sold out

Gonkas
Shopify Partner
11 0 1

Hi Xythia,

 

Thanks for your input about the "deny" that is how it was before and it is working so is good for us. For the second one yes it is important to have variants without stock because we have another implementation that customer can get an email when restocked.

 

Cheers,

Gonkas

Xythia
Shopify Partner
18 2 0

Alright then I get it.

 

If all is working for you now, please mark this as solved/accept a solution 

Cheers 🙂

Gonkas
Shopify Partner
11 0 1

Hi Xythia,

 

Would believe that you totally missed the problem.

My problem is that this is not working

{% if variant_id == my_variant %} selected{% endif %}

I've done a test like this inside the for loop:

{% if valueyouwant == bestselling %}got selected{% endif %}<br>

and can see that activating in the correct variant but the option never gets selected.

Cheers,

Goncalo

 

Xythia
Shopify Partner
18 2 0

You're right I misread your sentence.

the only thing I can come up with is that the select id must match with the one in theme.js

as in debut

liquid

<select name="id" id="ProductSelect-{{ section.id }}" class="product-form__variants no-js">

js

originalSelectorId: '#ProductSelect-' + sectionId,

 

 

Gonkas
Shopify Partner
11 0 1

The page where this code is being deployed is product.liquid.

Like said it works but not inside the HTML element <option ..>