Personalized checkout and custom promotions with Shopify Scripts
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
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
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
Alright then I get it.
If all is working for you now, please mark this as solved/accept a solution
Cheers 🙂
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
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,
The page where this code is being deployed is product.liquid.
Like said it works but not inside the HTML element <option ..>
Starting a B2B store is a big undertaking that requires careful planning and execution. W...
By JasonH Sep 23, 2024By investing 30 minutes of your time, you can unlock the potential for increased sales,...
By Jacqui Sep 11, 2024We appreciate the diverse ways you participate in and engage with the Shopify Communi...
By JasonH Sep 9, 2024