Get variable product price with jquery - WooCommerce

colapsnux picture colapsnux · Jun 14, 2016 · Viewed 7.8k times · Source

I have some variation per products which contain different price, I'm trying to get the product variable price with jQuery and I get it but this code bellow only get the min variation price and I would like to get the current price for the current variation. Then when I change another select value, the variation change in the front end with the correct price, but in the console log the min price is showed again and again and not the corresponding price for the corresponding variation.

jQuery( '.variations_form' ).each( function() {
    // when variation is found, do something
    jQuery(this).on( 'found_variation', function( event, variation ) {
        var product    = '<?php echo wc_get_product($variation_id) ?>',
            price      = <?php echo $product->get_price() ?>;
        console.log(price);
    });
});

I tried to look into the add-to-cart-variation.js and the wp-util.js file to find some way but I 'm not able to do it.

Is there a way to retrieve correctly the current variation price dynamically with jQuery ?

Any help would be appretiated

Answer

Andy picture Andy · Jun 28, 2018

Your code are on the track just use variation to get price, image, etc

jQuery(document).ready(function() {
    jQuery( '.variations_form' ).each( function() {
        jQuery(this).on( 'found_variation', function( event, variation ) {
            console.log(variation);//all details here
            var price = variation.display_price;//selectedprice
            console.log(price);
        });
    });
   });