Created
November 5, 2021 07:01
-
-
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
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('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