Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save AshlinRejo/39218bc6cd6dbe5d1e5a960ed55cbeda to your computer and use it in GitHub Desktop.
Save AshlinRejo/39218bc6cd6dbe5d1e5a960ed55cbeda to your computer and use it in GitHub Desktop.
Discount rules v2: Display sale badge percentage if they have default sale price instead of sale override
add_filter('advanced_woo_discount_rules_on_sale_badge_html', function ($html, $post, $_product){
if (strpos($html, 'Sale!') !== false) {
$percentage = 0;
$reg_p = floatval( $_product->get_regular_price() );
$sale_p = floatval( $_product->get_sale_price() );
if ( $reg_p && $sale_p) {
$percentage = - round( ( ( $reg_p - $sale_p ) / $reg_p ) * 100 );
} elseif ( 'variable' == $_product->get_type() && $_product->get_variation_regular_price() && $_product->get_variation_sale_price() ) {
$percentage = - round( ( ( $_product->get_variation_regular_price() - $_product->get_variation_sale_price() ) / $_product->get_variation_regular_price() ) * 100 );
}
if($percentage){
$html = '<div class="onsale">' . $percentage . '%</div>';
}
}
return $html;
}, 10, 3);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment