Created
November 18, 2021 12:58
-
-
Save AshlinRejo/1abe38a4a224612cf157135b722c8c04 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="badge-container is-larger absolute left top z-1"> | |
| <div class="callout badge badge-circle"><div class="badge-inner secondary on-sale"><span class="onsale">' . $percentage . '%</span> | |
| </div></div></div>'; | |
| } | |
| } | |
| return $html; | |
| }, 10, 3); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment