Display woocommerce variable product price

St Pavel picture St Pavel · Jun 21, 2017 · Viewed 32.6k times · Source

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. Any ideas how to display current price for active variation?

Answer

Alex Sholom picture Alex Sholom · Jun 15, 2019

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