-
-
Save kloon/4512943 to your computer and use it in GitHub Desktop.
<?php | |
// Add save percent next to sale item prices. | |
add_filter( 'woocommerce_sale_price_html', 'woocommerce_custom_sales_price', 10, 2 ); | |
function woocommerce_custom_sales_price( $price, $product ) { | |
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 ); | |
return $price . sprintf( __(' Save %s', 'woocommerce' ), $percentage . '%' ); | |
} | |
?> |
Its not working on variable products.Please help me out.
Its Urgent!
Thanks in advance.
the code above does not work with WC 3.1.2....
It doesn't throw any errors, simply does nothing....
I'll work on it but was thinking I couldn't possibly be the only one facing this...
any help would be great!
I'll see what I can come up with in the meantime.
Thanks!
found a solution here-
https://stackoverflow.com/questions/43757920/display-the-discounted-percentage-near-sale-price-in-single-product-pages-for-wc
answered May 3 at 22:46
LoicTheAztec
Thank you Loic !!
I looked for 2 hours and you saved me e few more I am certain!!
This code apparently doesn't work in my instance of WooCommerce Version 5.6.0, although it's been copied into the functions.php of the Storefront theme as is. Any suggestions for troubleshooting would be appreciated!
Your snippet only works on simple products. For variable products, instead, give the following code a try:
// For Variable Products Discount
add_filter( 'woocommerce_available_variation', 'custom_variation_price_saving_percentage', 10, 3 );
function custom_variation_price_saving_percentage( $data, $product, $variation ) {
$active_price = $data['display_price'];
$regular_price = $data['display_regular_price'];
if( $active_price !== $regular_price ) {
$saving_percentage = round( 100 - ( $active_price / $regular_price * 100 ), 1 ) . '%';
$data['price_html'] .= sprintf( __('You Saved: %s', 'woocommerce' ), $saving_percentage );
}
return $data;
}
try this:
`function woocommerce_saved_sales_price( $price, $product ) {
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
if ($percentage == "100") {
return $price . sprintf();
} else {
return $price . sprintf( __('-%s', 'woocommerce' ), $percentage . '%' );
}
}
add_filter( 'woocommerce_get_price_html', 'woocommerce_saved_sales_price', 10, 2 );`