Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

249
Visualizações
shopify display selected variant price

I have rendered the product variants as radio buttons on liquid. Now I am trying to listen to the variant change and render different prices accordingly. However, I do not know how to listen to the variant change here. Here is my code:

<form>
   {% for product_option in product.options_with_values %} 
      {{ product_option.name }}
     
        {% for value in product_option.values %}
            <input type="radio" id = "{{ value }}" name="{{ product_option.name}}" value="{{ value }}" >
            <label for="{{ value }}">{{ value }}</label>  
        {% endfor %}
        <br>
    {% assign current = product.selected_or_first_available_variant %}
    {% endfor %}
    <p>Price: {{current.price}} </p> 
    <input type="number" min="1"> 
    <button type="submit">Add to Cart</button>
</form>  

The price only shows for the first available variant. Even when I select different radio button options, the variant price does not update.

about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Welcome to stack overflow

Ideally, you probably should use some classes to use for your listeners as the page can have different radio buttons possibly even in other forms.

Below probably would get you going in the right direction.

<form>
   {% for product_option in product.options_with_values %} 
      {{ product_option.name }}
        {% for value in product_option.values %}
            <input class="option" type="radio" id = "{{ value }}" name="{{ product_option.name}}" value="{{ value }}" >
            <label for="{{ value }}">{{ value }}</label>  
        {% endfor %}
        <br>
    {% assign current = product.selected_or_first_available_variant %}
    {% endfor %}
    <p>Price: <span id="variantPrice">{{current.price}}</span></p> 
    <input type="number" min="1"> 
    <button type="submit">Add to Cart</button>
</form> 

You haven't mentioned if you are using jquery or not. I'm going to assume you are.

<script>
  $( document ).ready(function() {
     // Load all product data to an object
     var productData = {{ product | json }}; 

     // Listen to radio button updates and trigger a function to identify the variant
     $('.option').change(function() {
        idSelectedVariant();
     });

     function idSelectedVariant(){
         //1. Loop through the options and identify the selected options
         //2. Once the selected options are identified, loop through the variants in the productData object and identify the selected variant by checking the variant options
         //3. Keep in mind that not all product variants are going to have 3 options all the time. Some products can have 1 or 2 options as well.
         //4. Once the relevant variant is identified display the variant price by updating the content in the variantPrice span.
     }
  });
</script>
about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda