shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php
shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php
shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php

Display Percentage Discounts on Sale Badges

So many clients want to see percentage discounts on badges.

For better understanding, please refer to the screenshot.

If your requirements are the same and look similar to the image above, please follow the instructions given below.

This is too easy. You just need to copy the given code snippet and paste it into your active theme’s functions.php file.

// Added by the Wisdmlabs.
add_filter('woocommerce_sale_flash', 'wdm_csp_add_percentage_discount', 10, 3);

function wdm_csp_add_percentage_discount($html, $post, $product) {
    global $wpdb;
    $product_id = $product->get_id();
    $user_id = get_current_user_id();

    $user = wp_get_current_user();
    $roles = $user->roles;

    $csp_price = $product->get_price();
    $regular_price = $product->get_regular_price();

    if ( $csp_price === $regular_price ) {
        $role_price = $wpdb->get_row( $wpdb->prepare( 'SELECT price, min_qty, flat_or_discount_price as price_type FROM ' . $wpdb->prefix . 'wusp_role_pricing_mapping WHERE role=%d AND product_id=%d ORDER BY min_qty LIMIT 1', $roles, $product_id ) );
        $user_price = $wpdb->get_row( $wpdb->prepare( 'SELECT price, min_qty, flat_or_discount_price as price_type FROM ' . $wpdb->prefix . 'wusp_user_pricing_mapping WHERE user_id=%d AND product_id=%d ORDER BY min_qty LIMIT 1', $user_id, $product_id ) );
        $get_price = empty( $user_price ) ? $role_price : $user_price;
        if ( $get_price ) {
            if ( $get_price->min_qty > 1 && $get_price->price_type == 2) {
                $percentage_discount = ($get_price->price / 100) * $regular_price;
                $csp_price = $regular_price - $percentage_discount;
            } else {
                $csp_price = $get_price->price;
            }
        }
    }

    if ($product->is_type('simple')) {
        $percentageDiscount = ($csp_price / $regular_price) * 100;
        $percentageDiscount = 100 - $percentageDiscount;
        $percentageDiscount = (int) ($percentageDiscount * -1);
        $percentageDiscount = $percentageDiscount . '%';

        $html = '<span class="onsale">' . esc_html( $percentageDiscount ) . '</span>';    
    } else if ($product->is_type('variable')) {
        $percentages = array(
            'min' => false,
            'max' => false,
        );
        $prices = $product->get_variation_prices();
        $label = '';

        foreach ( $prices['price'] as $variation_id => $price ) {
            $regular_price = (float) $prices['regular_price'][ $variation_id ];
            $csp_price     = (float) $prices['price'][ $variation_id ];

            if ( $csp_price < $regular_price ) {
                $variation_percentage = ( ( $regular_price - $csp_price ) / $regular_price ) * 100;

                $percentages['min'] = false !== $percentages['min'] ? $percentages['min'] : $variation_percentage;
                $percentages['max'] = false !== $percentages['max'] ? $percentages['max'] : $variation_percentage;

                if ( $variation_percentage < $percentages['min'] ) {
                    $percentages['min'] = $variation_percentage;
                }

                if ( $variation_percentage > $percentages['max'] ) {
                    $percentages['max'] = $variation_percentage;
                }
            }
        }

        $rounded_percentages = $percentages;
        $rounded_percentages = array_map( 'round', $percentages );
        $rounded_percentages = array_map( 'intval', $rounded_percentages );

        if ( ( empty( $percentages['min'] ) || empty( $percentages['max'] ) ) || ( $percentages['min'] === $percentages['max'] ) ) {
            /* translators: %1$d is a discount percentage. E.g. -60% */
            $label = sprintf( _x( '-%1$d%%', 'product discount', 'your-text-domain' ), max( $rounded_percentages ) );
        } else {
            /* translators: The whole string is a discount range. %1$d is the minimum discount percentage, %2$d is the maximum discount percentage. E.g. -10% / -60% */
            $label = sprintf( _x( '-%1$d%%', 'product discount', 'your-text-domain' ), $rounded_percentages['min'], $rounded_percentages['max'] );
        }
        $html = '<span class="onsale">' . esc_html( $label ) . '</span>';    
    }
    return $html;
}

Note: For a single item, it will be easy to understand our code. However, you may find it difficult to understand for variable quantities. Our code works as follows: if the CSP price set rule is for a single quantity then it will show you the percentage according to the single quantity. If the CSP price is set for more than two quantities, it will show you the percentage accordingly.

Updated on June 16, 2024
shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php
shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php
shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php

Was this article helpful?

shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php
shvsh > /home/1168859.cloudwaysapps.com/rcyqmhwrmv/public_html/docs/wp-content/plugins/ht-knowledge-base/ht-knowledge-base.php

Related Articles