Skip to content

Instantly share code, notes, and snippets.

@jmabbas
Created February 26, 2026 06:51
Show Gist options
  • Select an option

  • Save jmabbas/ea08707941f22c5efec680b85ce39ba3 to your computer and use it in GitHub Desktop.

Select an option

Save jmabbas/ea08707941f22c5efec680b85ce39ba3 to your computer and use it in GitHub Desktop.
WooCommerce - Sale label to percentage %
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