https://0db1d3-5b.myshopify.com/products/third-product
I have a product(s) with multiple variants. In this example:
FR34 - has inventory quantity 5
FR36 - has inventory quantity 0 and has “Continue selling when out of stock” option turned ON
FR54 - has inventory quantity 0 and has “Continue selling when out of stock” option turned OFF
-
When product page loads and it has variants select, I want the first option to be blank, forcing the user to pick a Size and not have a first variant as default.
-
If FR34 is picked, button should say Add to cart (default and works fine)
-
if FR36 is picked, button should say Preorder (because quantity 0 and continue selling is on)
and a custom text message should appear under the Preorder button.
- if FR54 is picked, button should say Sold out (default and works fine)
I have managed to find the Add to cart button in buy-buttons.liquid and have added an if statement to show Preorder as the button label if a variant has “Continue selling when out of stock” option turned ON.
{%- if product.selected_or_first_available_variant.available == false or quantity_rule_soldout -%}
{{ 'products.product.sold_out' | t }}
{%- else -%}
{% if product.selected_or_first_available_variant.inventory_policy == 'continue' %}
Preorder
{%- else -%}
{{ 'products.product.add_to_cart' | t }}
{%- endif -%}
{%- endif -%}
This shows a correct label, but only when the page loads and FR34 is a selected variant, which has inventory. If you pick another variant, this label disappears and doesn’t show ever again, no matter which option you choose. I’ve also tried to combine the if statement with selected_or_first_available_variant <=0 but this seems to render no results at all.
Question 1: How can I make this label stick and show only when a variant is chosen which has 0 stock and continue is ON? This will also help me understand how to show the custom text under the button, because it will have the same rule.
Question 2: How can I add a blank variant to be the first on the list and make the variant field required for the user to pick from?
Question 3: If the variant name (value) contains the word “YES”, show the “Text for Engraving” field.
I’ve tried looping over the variant values in main-product.liquid and have failed miserably.
This is not a fixed product and solution will need to work dynamically on other products which have variants, where some variants might have different names and the number of variants might be different as well.