can you help me figure out how to show a notification for specific variants?
If customer selected "IPhone 14" show notification "Thats a Pre-order" and hide this message if the customer changes to "IPhone 13".
{% if selected variant.name contains '14' %}
{p} Thats a Pre-order {/p}
{% endif %}
The main Problem is, that the URL does not change by selecting the variant. Otherwise i could simply check the URL. But in my Case the URL is always the same..
3
Answers
You have a misunderstanding on how liquid works.
Liquid finish executing before the DOM is loaded. (a.k.a the moment you see the page liquid is finished and no code will be processed after that).
What you are describing is interacting with the DOM elements (select or radio buttons) and expecting liquid to execute some code after the variant is changed, which is not possible.
Long story short you need to use Javascript to handle this logic, not liquid.
You can use js to show an specific text for some options and then hide it with all other options, something like this:
You can do this with Shopify metafields.
Add a metafiled data to the variant
For example:
Product – iPhone – variant – iPhone 14, iPhone 13, iPhone SE
Variant Metafield – namespace&key :pro.preorder Value – Preorder
In the javascript on the change of variant, you can add a condition if the metafield is not empty then show this:
{{variant.metafields.pro.preorder}}
The output will be the value you wrote for that metafield: Preorder