in my collection I added ‘?variant=here-comes-the-variant-id’ to the link url (like it’ done here). On my product page I try to access this url parameter via variant.selected (or product.selected) (or even product.selected_or_first_available_variant).
{% assign current_variant = variant.selected %}
But I can’t get the variant and I have no clue why.
Out of the 3 options you’ve mentioned, only the last one, product.selected_or_first_available_variant references proper variable and property. This should be right:
{% assign current = product.selected_or_first_available_variant %}
With product.selected_or_first_available_variant it is working now - I have no clue why I seemed not to work before. Still I’m confussed why product.selected and variant.selected do not give me any values. I will ignore these as I get my data with your advised method. Thanks a lot
I am not getting this new id on variant change on product page . Always default variant id is coming . I am writing this code in product template . need a anchor tag based on selected variant .
You can get the current product’s variant ID using below.
{{ product.selected_variant.id }}
You might actually want to use product.selected_or_first_available_variant.id - as I understand it, selected_variant is null if there’s no variant in the URL of the page you are on.
Having the same problem here, especially with the features of OS 2.0. Can’t add a text-block with the wanted metadata to the products template since the meta-data is assigned to the variants, not the products. Can however add a custom-liquid-block
but this doesn’t change when another varient is selected. Is there a way to react to variant-selection without messing with the actual templates code / js - files?
I’m also stuck here. product.selected_or_first_available_variant is working when the page load and shows the first variant, still when changing through variants, it’s not working it stays in the first variant, and also selected_variant is not working !!!
Things get even worse. We are using the empire-theme by pixelunion and want to upgrade to a halfway decent version. We managed to write some custom javascript-code and attach a “change”-event-listener to the hidden selectbox, that is changed when a variant is selected. For some reason this doesn’t work propperly in empire 7.0.0. The strange thing is, that when we inspect the document, change the visibility of the selectbox from “hidden” to “block” an change the selected value manually, the event is triggered.
There should be a way, since shopify updates some info when variant picker is clicked (changes to variant image, inventory status, pick-up availability, etc.)