I have very simple woocommerce code to show product price after a deduction. Please see the below code.
add_action( 'woocommerce_get_price_html' , 'rp_get_price' );
function rp_get_price($price){
$rp_percentage = 10;
$regular_price = get_post_meta( get_the_ID(), '_price', true );
$rp_discount = $regular_price * $rp_percentage / 100;
$price = $regular_price - $rp_discount;
return $price;
}
add_filter( 'woocommerce_variable_sale_price_html', 'wc_wc20_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wc_wc20_variation_price_format', 10, 2 );
function wc_wc20_variation_price_format( $price, $product ) {
$rp_percentage = 10;
$min_price = $product->get_variation_price( 'min', true );
$max_price = $product->get_variation_price( 'max', true );
$rp_min_discount = $min_price*10/100;
$rp_min_price = $min_price - $rp_min_discount;
$rp_max_discount = $max_price*10/100;
$rp_max_price = $max_price - $rp_max_discount;
if ($min_price != $max_price)
{
$price = $rp_min_price . '-' . $rp_max_price;
}
else
{
$price = $rp_min_price;
}
return $price;
}
For single product the price shows correctly. for variation product with same minimum and maximum rate shows correctly but my problem is for variation price low price to high price it is not showing like '20 -30' (it only shows the minimum price) Could you please help me to sort it out.
Try out this code :
add_action( 'woocommerce_get_price_html' , 'rp_get_price' );
function rp_get_price($price){
global $product;
//get the sale price of the product whether it be simple, grouped or variable
$sale_price = get_post_meta( get_the_ID(), '_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);
$rp_percentage = 10;
if( $product->is_type( 'simple' ) ):
$rp_discount = $regular_price * $rp_percentage / 100;
$price = $regular_price - $rp_discount;
else:
if ($regular_price == ""){
$min_price = $product->get_variation_price( 'min', true );
$max_price = $product->get_variation_price( 'max', true );
$rp_min_discount = $min_price*10/100;
$rp_min_price = $min_price - $rp_min_discount;
$rp_max_discount = $max_price*10/100;
$rp_max_price = $max_price - $rp_max_discount;
if ($min_price != $max_price)
{
$price = $rp_min_price . '-' . $rp_max_price;
}
else
{
$price = $rp_min_price;
}
}
endif;
return $price;
}
EDITED:
add_action( 'woocommerce_get_price_html' , 'rp_get_price' );
function rp_get_price($price){
global $product;
//get the sale price of the product whether it be simple, grouped or variable
$sale_price = get_post_meta( get_the_ID(), '_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);
$rp_percentage = 10;
if ($regular_price == ""){
$currency = get_woocommerce_currency_symbol();
$min_price = $product->min_variation_price;
$max_price = $product->max_variation_price;
$rp_min_discount = $min_price*10/100;
$rp_min_price = $min_price - $rp_min_discount;
$rp_max_discount = $max_price*10/100;
$rp_max_price = $max_price - $rp_max_discount;
if ($min_price != $max_price)
{
$price = $rp_min_price . '-' . $rp_max_price;
}
else
{
$price = $rp_min_price;
}
}
else{
$rp_discount = $regular_price * $rp_percentage / 100;
$price = $regular_price - $rp_discount;
}
return $price;
}
Try new edited code as old one was not up to the mark.