

/**
 * Sujana Store - WooCommerce Global Sale Percentage
 * Safe function name to avoid redeclaration conflicts.
 */

if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

if ( ! function_exists( 'sujana_global_percentage_badge_v2' ) ) {

    add_filter(
        'woocommerce_sale_flash',
        'sujana_global_percentage_badge_v2',
        9999,
        3
    );

    function sujana_global_percentage_badge_v2( $html, $post, $product ) {

        if ( ! ( $product instanceof WC_Product ) ) {
            return $html;
        }

        $discounts = array();

        // Variable products.
        if ( $product->is_type( 'variable' ) ) {

            $variations = $product->get_children();

            foreach ( $variations as $variation_id ) {

                $variation = wc_get_product( $variation_id );

                if ( ! $variation || ! $variation->is_on_sale() ) {
                    continue;
                }

                $regular = (float) $variation->get_regular_price();
                $sale    = (float) $variation->get_sale_price();

                if ( $regular > 0 && $sale > 0 && $sale < $regular ) {

                    $discounts[] = round(
                        ( ( $regular - $sale ) / $regular ) * 100
                    );
                }
            }

        } else {

            // Simple products.
            if ( ! $product->is_on_sale() ) {
                return $html;
            }

            $regular = (float) $product->get_regular_price();
            $sale    = (float) $product->get_sale_price();

            if ( $regular > 0 && $sale > 0 && $sale < $regular ) {

                $discounts[] = round(
                    ( ( $regular - $sale ) / $regular ) * 100
                );
            }
        }

        if ( empty( $discounts ) ) {
            return $html;
        }

        $percentage = max( $discounts );

        return '<span class="woocommerce-sale-percentage-badge onsale">'
            . esc_html( '-' . $percentage . '%' )
            . '</span>';
    }
}



add_action( 'wp_footer', function() {
    if ( ! current_user_can( 'manage_options' ) ) {
        return;
    }

    global $wp_filter;

    echo '<!-- SUJANA SALE BADGE DIAGNOSTIC -->';

    if ( isset( $wp_filter['woocommerce_sale_flash'] ) ) {
        echo '<!-- SALE FLASH FILTER EXISTS -->';
    } else {
        echo '<!-- SALE FLASH FILTER NOT FOUND -->';
    }

    echo '<!-- SUJANA SALE BADGE DIAGNOSTIC END -->';
}, 9999 );

<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://sujanastore.com/wp-sitemap-index.xsl" ?>
<sitemapindex xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"><sitemap><loc>https://sujanastore.com/wp-sitemap-posts-post-1.xml</loc></sitemap><sitemap><loc>https://sujanastore.com/wp-sitemap-posts-page-1.xml</loc></sitemap><sitemap><loc>https://sujanastore.com/wp-sitemap-posts-product-1.xml</loc></sitemap><sitemap><loc>https://sujanastore.com/wp-sitemap-taxonomies-category-1.xml</loc></sitemap><sitemap><loc>https://sujanastore.com/wp-sitemap-taxonomies-product_cat-1.xml</loc></sitemap><sitemap><loc>https://sujanastore.com/wp-sitemap-users-1.xml</loc></sitemap></sitemapindex>
