Created
February 26, 2026 06:51
-
-
Save jmabbas/ea08707941f22c5efec680b85ce39ba3 to your computer and use it in GitHub Desktop.
WooCommerce - Sale label to percentage %
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| add_filter( 'woocommerce_sale_badge_text', 'custom_sale_percentage_badge', 10, 2 ); | |
| function custom_sale_percentage_badge( $sale_text, $product ) { | |
| if ( $product->is_type( 'simple' ) ) { | |
| $regular = (float) $product->get_regular_price(); | |
| $sale = (float) $product->get_sale_price(); | |
| if ( $regular > 0 && $sale > 0 ) { | |
| $percentage = round( ( ( $regular - $sale ) / $regular ) * 100 ); | |
| return '-' . $percentage . '%'; | |
| } | |
| } | |
| if ( $product->is_type( 'variable' ) ) { | |
| $max_regular = max( $product->get_variation_regular_price( 'max', true ) ); | |
| $max_sale = max( $product->get_variation_sale_price( 'max', true ) ); | |
| if ( $max_regular > 0 && $max_sale > 0 ) { | |
| $percentage = round( ( ( $max_regular - $max_sale ) / $max_regular ) * 100 ); | |
| return '-' . $percentage . '%'; | |
| } | |
| } | |
| return $sale_text; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment