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?
The variable product is a product type in WooCommerce, which allows generating multiple variations of the same product with control over price, image, size, weight, etc. You may need to offer the same price for all variations or different prices for each variation of a product.
WooCommerce Variation Price Range Display plugin helps you to show variations in a more user-friendly way. It allows you to adjust how your variation price ranges display to create a better WooCommerce store. The Variation Price Display plugin lets WooCommece store owners complete flexibility in displaying variable prices.
Go to the product page dashboard > Product data meta box > Attributes. Choose your attribute. Here, I have chosen Size. Then, go to the Custom product attribute > Click Add. You can add all required attribute values which were created before for generating variation of the products.
The code snippet is not only limited to ELEX plugins but can be used on any WordPress store running WooCommerce. Consider we have a product, having two color variations, Black and Red, and are priced $30 and $40 respectively.
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
<?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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With