How to get the active price of a selected variable product

I am trying to access the price of a product for a selected product, so I can use it in my PHP code to do some calculations. I found code that only works partially. Only the price of the first variable product is displayed in the code. When I change the value from 0 to 1 in my line of code, it displays the price of the second option. $ availablevariations [0] ['variation_id']. I am trying to get the current price of a product based on any option selected from the drop-down list. Here is the code I'm using that doesn't work the way I would like to

    // Get current product variation price
    global $product;

    //get the sale price of the product whether it be simple, grouped or variable
    echo $saleprice = getpostmeta( gettheID(), '_price', true);

    //get the regular price of the product, but of a simple product
    $regular_price = get_post_meta( get_the_ID(), '_regular_price', true);

    //oh, the product is variable to $sale_price is empty? Lets get a variation price

    if ($regular_price == ""){
    #Step 1: Get product varations
    $available_variations = $product->get_available_variations();

    #Step 2: Get product variation id
    $variation_id=$available_variations[0]['variation_id']; // Getting the variable id of just the 1st product. You can loop $available_variations to get info about each variation.

    #Step 3: Create the variable product object
    $variable_product1= new WC_Product_Variation( $variation_id );

    #Step 4: You have the data. Have fun :)
    $regular_price = $variable_product1 ->regular_price;

}

        echo "<br> $regular_price ---This is the price I am trying to fix "

, , , , http://www.emotionadv.it/shop/volantini/volantino/, , . , .

+3
2

WooCommerce , . ... variation , , .

jQuery( document ).ready( function($) {

    $( '.variations_form' ).each( function() {

        // when variation is found, do something
        $(this).on( 'found_variation', function( event, variation ) {
            console.log(variation);
        });

    });

} );

variation :

, .

variation.price_html = "<span class="price"><span class="amount">$40.00</span></span>";

. , ajax variation.variation_id.

$product = wc_get_product($variation_id);
$price = $product->get_price();
0

script jQuery, "prova", , , $product = wc_get_product ( $variation_id); $price = $product- > get_price(); wp_enqueue_script ('prova'); $price - .

0

All Articles