Last active
June 20, 2023 11:04
-
-
Save AnanthFlycart/a6214e2191abbaf85ff4af47aa2c355a to your computer and use it in GitHub Desktop.
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
| if (!function_exists('shoptimizer_child_change_displayed_sale_price_html')) { | |
| function shoptimizer_child_change_displayed_sale_price_html() { | |
| if (function_exists('shoptimizer_get_option') && function_exists('shoptimizer_safe_html')) { | |
| global $product, $price; | |
| $shoptimizer_sale_badge = ''; | |
| $shoptimizer_layout_woocommerce_display_badge = shoptimizer_get_option('shoptimizer_layout_woocommerce_display_badge'); | |
| $shoptimizer_layout_woocommerce_display_badge_type = shoptimizer_get_option('shoptimizer_layout_woocommerce_display_badge_type'); | |
| // Use WDR discount percentage if the product has discount | |
| $discount_percentage = apply_filters('advanced_woo_discount_rules_get_product_discount_percentage', 0, $product); | |
| if ($discount_percentage > 0 && !$product->is_type(['grouped', 'bundle', 'yith_bundle'])) { | |
| $percentage = round($discount_percentage) . '%'; | |
| } elseif ($product->is_on_sale() && !$product->is_type('grouped') && !$product->is_type('bundle')) { | |
| if ($product->is_type('variable')) { | |
| $percentages = array(); | |
| // Get all variation prices. | |
| $prices = $product->get_variation_prices(); | |
| // Loop through variation prices. | |
| foreach ($prices['price'] as $key => $price) { | |
| // Only on sale variations. | |
| if ($prices['regular_price'][$key] !== $price && $prices['regular_price'][$key] > 0.005) { | |
| // Calculate and set in the array the percentage for each variation on sale. | |
| $percentages[] = round(100 - ($prices['sale_price'][$key] / $prices['regular_price'][$key] * 100)); | |
| } | |
| } | |
| // Keep the highest value. | |
| if (!empty($percentages)) { | |
| $percentage = max($percentages) . '%'; | |
| } | |
| } else { | |
| $percentage = 0; | |
| $regular_price = (float)$product->get_regular_price(); | |
| if ($regular_price > 0.005) { | |
| $sale_price = (float)$product->get_price(); | |
| $percentage = round(100 - ($sale_price / $regular_price * 100), 0) . '%'; | |
| } | |
| } | |
| } | |
| if (isset($percentage) && $percentage > 0) { | |
| if ('bubble' === $shoptimizer_layout_woocommerce_display_badge_type) { | |
| $shoptimizer_sale_badge .= sprintf(__('<span class="sale-item product-label type-bubble">-%s</span>', 'shoptimizer'), $percentage); | |
| } else { | |
| $shoptimizer_sale_badge .= sprintf(__('<span class="sale-item product-label type-circle">-%s</span>', 'shoptimizer'), $percentage); | |
| } | |
| } | |
| if (true === $shoptimizer_layout_woocommerce_display_badge) { | |
| echo shoptimizer_safe_html($shoptimizer_sale_badge); | |
| } | |
| } | |
| } | |
| add_action('after_setup_theme', function () { | |
| remove_action('woocommerce_before_shop_loop_item_title', 'shoptimizer_change_displayed_sale_price_html', 7); | |
| remove_action('woocommerce_single_product_summary', 'shoptimizer_change_displayed_sale_price_html', 10); | |
| add_action('woocommerce_before_shop_loop_item_title', 'shoptimizer_child_change_displayed_sale_price_html', 7); | |
| add_action('woocommerce_single_product_summary', 'shoptimizer_child_change_displayed_sale_price_html', 10); | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment