skip to Main Content

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


  1. Chosen as BEST ANSWER
    {{ product.selected_variant.id }}
    

  2. 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),

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search