To fix sale badges are not shown while using Shoptimizer v2.6
Fix sale badges not appearing issue while using Shoptimizer v2.6.
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);
});
}