Support » Plugin: WooCommerce » Showing percentage discounted under price
Showing percentage discounted under price
-
I’m setting up WooCommerce, and im running a discount on some products, so i want to show in percentage how much the customer is saving.
I already have the before price, and the new price. But i would like to show under the price how much they save in percent.
I also have the bubble showing “Discount”, but i don’t want to show the percentage inside the bubble.The page I need help with: [log in to see the link]
-
I found this
add_filter( 'woocommerce_format_sale_price', 'woocommerce_custom_sales_price', 10, 3 ); function woocommerce_custom_sales_price( $price, $regular_price, $sale_price ) { // Getting the clean numeric prices (without html and currency) $regular_price = floatval( strip_tags($regular_price) ); $sale_price = floatval( strip_tags($sale_price) ); // Percentage calculation and text $percentage = round( ( $regular_price - $sale_price ) / $regular_price * 100 ).'%'; $percentage_txt = __('<br/>Du sparer ', 'woocommerce' ).$percentage; return '<del>' . wc_price( $regular_price ) . '</del> <ins>' . wc_price( $sale_price ) . $percentage_txt . '</ins>'; }
But it doesn’t work on my product variations 🙁 Any suggestions?
I get this error on my variable products: “Warning: Division by zero in /customers/d/e/b/malerbutikken.dk/httpd.www/test/wp-content/themes/yootheme/functions.php on line 308”.
While the normal products gets calculated correctly.That code is actually pretty solid, and works for me: http://cld.wthms.co/BFYGuV
Either
( $regular_price - $sale_price )
is returning 0, or$regular_price * 100
is returning 0.Either way, check the outputs of the $regular_price and $sale_price in that filter, as something is going wrong.
@icaleb
Thanks for your response 🙂I’m not so experienced coder, so i don’t really know what to write. I’ve tried finding some code pieces from Stackoverflow, but without luck.
My problem is i have variation prices on my variable products, and when i try to calculate % saved on those variable products, i get the error:
“Warning: Division by zero in /customers/d/e/b/XXXXX/httpd.www/test/wp-content/themes/yootheme/functions.php on line 314”I’m not seeing that on a variable product w/ sale prices on my test site, so there is quite possibly something intefering on your site (like another plugin or the theme).
That said, I don’t see an extra sale price on the variable product – it just shows a range instead. So this enhancement may need extra work for variable products.
If you can’t quite figure out what code you need and this is a must-have change, I would recommend hiring a developer:
– http://jobs.wordpress.net/
– https://codeable.io/
– https://woocommerce.com/experts/-
This reply was modified 5 years, 2 months ago by
Caleb Burks.
Hey Caleb,
I think i figured out most of it myself. But i need to target only simple products in the first code. Can you help me target only simple products in the first function?
I made this://PROCENT SPARET PÅ SIMPLE PRODUCT 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 ); /* DU SPARER %-DEL I PRODUKT KODE PÅ VARIABLE PRODUCT */ add_filter( 'woocommerce_variable_price_html', 'bbloomer_variation_price_format_310', 10, 2 ); function bbloomer_variation_price_format_310( $price, $product ) { // Getting the clean numeric prices (without html and currency) $min_var_reg_price = $product->get_variation_regular_price( 'min', true ); $min_var_sale_price = $product->get_variation_sale_price( 'min', true ); // Percentage calculation and text $percentage = round( ( $min_var_reg_price - $min_var_sale_price ) / $min_var_reg_price * 100 ).'%'; $percentage_txt = __('<br/>Du sparer ', 'woocommerce' ).$percentage; return '<del>' . wc_price( $min_var_reg_price ) . '</del> <ins>' . wc_price( $min_var_sale_price ) . $percentage_txt . '</ins>'; }
$product->get_type()
will return the product type. So at the very top of the function you could do something like this:if ( 'simple' !== $product->get_type() ) { return $price; }
Thanks Caleb. I have the two functions setup now, but for some reason when i have both functions active only the percentage saved for the Simple product is shown. But when i remove the Simple products function i can see the Variables function just fint. Maybe you can help? I guess it is just a very simple issue, i’m just not so good with it.
//PROCENT SPARET PÅ SIMPLE PRODUCT add_filter( 'woocommerce_get_price_html', 'woocommerce_saved_sales_price', 10, 2 ); function woocommerce_saved_sales_price( $price, $product ) { if ( 'simple' === $product->get_type() ) { $percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 ); if ($percentage == "100") { return $price; } else { return $price . sprintf( __('<br/>Du sparer %s', 'woocommerce' ), $percentage . '%' ); } } } /* DU SPARER %-DEL I PRODUKT KODE PÅ VARIABLE PRODUCT */ add_filter( 'woocommerce_variable_price_html', 'bbloomer_variation_price_format_310', 10, 2 ); function bbloomer_variation_price_format_310( $price, $product ) { // Getting the clean numeric prices (without html and currency) $min_var_reg_price = $product->get_variation_regular_price( 'min', true ); $min_var_sale_price = $product->get_variation_sale_price( 'min', true ); // Percentage calculation and text $percentage = round( ( $min_var_reg_price - $min_var_sale_price ) / $min_var_reg_price * 100 ).'%'; $percentage_txt = __('<br/>Du sparer ', 'woocommerce' ).$percentage; return '<del>' . wc_price( $min_var_reg_price ) . '</del> <ins>' . wc_price( $min_var_sale_price ) . $percentage_txt . '</ins>'; }
I have also tried adding
if ( 'simple' !== $product->get_type() ) {
but without luck..-
This reply was modified 5 years, 2 months ago by
andersjytzler.
-
This reply was modified 5 years, 2 months ago by
- The topic ‘Showing percentage discounted under price’ is closed to new replies.