How do I get the variant ID of a product I’m currently viewing? For example, if I’m viewing a product at example.com/collections/bowls/product-name?variant=123456789
I need to get the variant’s ID number. I’ve tried {{ variant.id }}
but it does not return anything. I can get it to return all of the variant ID’s when I put it within a {% for variant in product.variants %}
loop but that returns all of the IDs. I just need the ID of the one I’m currently viewing.
2
Answers
if you are using javascript you can get variant id of selected variant from the product page with querySelector.
document.querySelector(‘.product__pickup-availabilities’).dataset.variantId
it works for both conditions(with variants or not),