9

How to display woocommerce variable price for current active variation on single product page? I use the code:

<?php 
global $product;
if ($product->is_type( 'simple' )) { ?>
  <p class="price"><?php echo $product->get_price_html(); ?></p>
<?php } ?>
<?php 
if($product->product_type=='variable') {
  $available_variations = $product->get_available_variations();
  $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.
  $variable_product1= new WC_Product_Variation( $variation_id );
  $regular_price = $variable_product1 ->regular_price;
  $sales_price = $variable_product1 ->sale_price;
  echo $regular_price+$sales_price;
  }
?>

But it shows only lowest variable price instead of currently selected variation's price.

How can I display the current price for the active variation?

starball
  • 20,030
  • 7
  • 43
  • 238
St Pavel
  • 339
  • 1
  • 3
  • 18

4 Answers4

12

So, you can just modify \your-theme\woocommerce\single-product\sale-flash.php file

Or, your can also use filter. By the way there is more simple solution:

if ($product->is_type( 'simple' )) {
        $sale_price     =  $product->get_sale_price();
        $regular_price  =  $product->get_regular_price();
    }
    elseif($product->is_type('variable')){
        $sale_price     =  $product->get_variation_sale_price( 'min', true );
        $regular_price  =  $product->get_variation_regular_price( 'max', true );
    }


    $discount = round (($sale_price / $regular_price -1 ) * 100);
}

Or you can copy this gist https://gist.github.com/sholex/4064fc2b656c0857a78228cf5324b370

Alex Sholom
  • 399
  • 4
  • 4
5
<?php 
                global $product;
                if ($product->is_type( 'simple' )) { ?>
                    <p class="price"><?php echo $product->get_price_html(); ?></p>
                <?php } ?>
                <?php 
                if($product->product_type=='variable') {
                    $available_variations = $product->get_available_variations();
$count = count($available_variations)-1;
                    $variation_id=$available_variations[$count]['variation_id']; // Getting the variable id of just the 1st product. You can loop $available_variations to get info about each variation.
                    $variable_product1= new WC_Product_Variation( $variation_id );
                    $regular_price = $variable_product1 ->regular_price;
                    $sales_price = $variable_product1 ->sale_price;
                    echo $regular_price+$sales_price;
                    }
                ?>

try this. This may help you.

Karthik Rajan
  • 164
  • 1
  • 15
  • Thanks for answer, but this one shows the highest price of variations. If i switch to another variation, it still shows the same price :( – St Pavel Jun 21 '17 at 12:03
  • For example there are 2 variations: 1st costs 7 USD, 2nd 14 USD. My code always shows lowest price, and yours - the highest. I need to change that price dynamically: if I select 1st variation, it shows price 7 USD, and if I select 2nd - 14 USD – St Pavel Jun 21 '17 at 12:03
  • Do you have any unique id for that selected variants? – Karthik Rajan Jun 21 '17 at 12:07
  • Is there a way to get ID automatically? I'd like to avoid creating or counting them manually :) – St Pavel Jun 21 '17 at 12:10
  • you should use a ajax call to display the variable price on your drop-down change – Tehseen Ahmed Mar 19 '18 at 09:43
1
<?php 
                global $product;
                if ($product->is_type( 'simple' )) { ?>
                    <p class="price"><?php echo $product->get_price_html(); ?></p>
                <?php } ?>
                <?php 
                if($product->product_type=='variable') {
                    $available_variations = $product->get_available_variations();
foreach($available_variations as $key=>$val){ 
     if(trim($val['variation_id'])==**"your selected variant id"**){
                    $variation_id=$available_variations[$key]['variation_id']; // Getting the variable id of just the 1st product. You can loop $available_variations to get info about each variation.
     }
}
                    $variable_product1= new WC_Product_Variation( $variation_id );
                    $regular_price = $variable_product1 ->regular_price;
                    $sales_price = $variable_product1 ->sale_price;
                    echo $regular_price+$sales_price;
                    }
                ?>
Karthik Rajan
  • 164
  • 1
  • 15
  • May be this may help you to get the selected variants id `$variable_product1=new WC_Product_Variation( )::get_variation_id();` – Karthik Rajan Jun 21 '17 at 12:24
  • The code throws lots of notices but works. Needs fixing for indirect data access. Please use calls for data, not directly accessing. – GhostPengy Mar 26 '18 at 12:12
1
    add_action( 'woocommerce_before_single_product', 'check_if_variable_first' );
function check_if_variable_first(){
    if ( is_product() ) {
        global $post;
        $product = wc_get_product( $post->ID );
        if ( $product->is_type( 'variable' ) ) {
            // removing the price of variable products
remove_action( 'woocommerce_single_product_summary', 'woocommerce_template_single_price', 10 );

// Change location of
add_action( 'woocommerce_single_product_summary', 'custom_wc_template_single_price', 10 );
function custom_wc_template_single_price(){
    global $product;

// Variable product only
if($product->is_type('variable')):

    // Main Price
    $prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
    $price = $prices[0] !== $prices[1] ? sprintf( __( 'От: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

    // Sale Price
    $prices = array( $product->get_variation_regular_price( 'min', true ), $product->get_variation_regular_price( 'max', true ) );
    sort( $prices );
    $saleprice = $prices[0] !== $prices[1] ? sprintf( __( 'От: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

    if ( $price !== $saleprice && $product->is_on_sale() ) {
        $price = '<del>' . $saleprice . $product->get_price_suffix() . '</del> <ins>' . $price . $product->get_price_suffix() . '</ins>';
    }

    ?>
    <style>
        div.woocommerce-variation-price,
        div.woocommerce-variation-availability,
        div.hidden-variable-price {
            height: 0px !important;
            overflow:hidden;
            position:relative;
            line-height: 0px !important;
            font-size: 0% !important;
        }
    </style>
    <script>
    jQuery(document).ready(function($) {
        $('select').blur( function(){
            if( '' != $('input.variation_id').val() ){
                $('p.price').html($('div.woocommerce-variation-price > span.price').html()).append('<p class="availability">'+$('div.woocommerce-variation-availability').html()+'</p>');
                console.log($('input.variation_id').val());
            } else {
                $('p.price').html($('div.hidden-variable-price').html());
                if($('p.availability'))
                    $('p.availability').remove();
                console.log('NULL');
            }
        });
    });
    </script>
    <?php

    echo '<p class="price">'.$price.'</p>
    <div class="hidden-variable-price" >'.$price.'</div>';

endif;
}

        }
    }
}