Can't get selected variant

Solved
QnavWhat
Excursionist
43 2 6

Hi there,

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.

Can someone please advise me what to do ... thanks

Accepted Solutions (2)

Accepted Solutions
tim
Shopify Expert
2925 143 1041

This is an accepted solution.

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 %}

 

Want to hire me to tweak a theme? Mail me at tairli@yahoo.com!
My post solved your problem? Like it!

View solution in original post

tim
Shopify Expert
2925 143 1041

This is an accepted solution.

'Cause product object has no .selected property, and for variant  it has different meaning

https://help.shopify.com/themes/liquid/objects/product

https://help.shopify.com/themes/liquid/objects/variant#variant-selected

Want to hire me to tweak a theme? Mail me at tairli@yahoo.com!
My post solved your problem? Like it!

View solution in original post

Replies 11 (11)
tim
Shopify Expert
2925 143 1041

This is an accepted solution.

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 %}

 

Want to hire me to tweak a theme? Mail me at tairli@yahoo.com!
My post solved your problem? Like it!

View solution in original post

QnavWhat
Excursionist
43 2 6

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

tim
Shopify Expert
2925 143 1041

This is an accepted solution.

'Cause product object has no .selected property, and for variant  it has different meaning

https://help.shopify.com/themes/liquid/objects/product

https://help.shopify.com/themes/liquid/objects/variant#variant-selected

Want to hire me to tweak a theme? Mail me at tairli@yahoo.com!
My post solved your problem? Like it!

View solution in original post

QnavWhat
Excursionist
43 2 6

Thanks for your help

Akshay_V
Shopify Partner
433 16 56


@tim Is there any way to get selected variant title like cart page on the product page after selecting from the swatch.

Akshay Vaghasiya | Shopify Expert
Skype: akshayvaghasiya84
 - Like and Mark as an Accepted Solution if reply helpful
 - Feel free to contact me on info@lavitastic.com regarding any help
kuldeep
New Member
7 0 0

Hi Tim 

 

{%- assign current_variant = product.selected_or_first_available_variant -%}
 
 
{% if current_variant.id == 123 %}
<a href=""><img src="{{ 'sample.png' | asset_url }}" alt="sample" class="product-form " id="new-button"></a>
{% endif %}
 
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 .
 
your help will be appreciated .
 
Thank you 
kuldeep
New Member
7 0 0

Hi All 

 

I found that i can get variant sku by {{ variant.sku }} and if i am adding this in anchor tag , it's working .

but below code is not working . I will be very grateful if someone can help 

 

{% if variant.sku == 'H0-S3TC-64ZA' %}
do something
{% endif %}

 

Many Thanks 

Product
Tourist
5 0 2

How can I get that value at runtime? I mean, how can I get with JavaScript the selected variation object when the select elements change.

FurnaceX
Shopify Partner
31 2 30

@ProductDid you figure this out?

"How can I get that value at runtime? I mean, how can I get with JavaScript the selected variation object when the select elements change."