• Resolved grunauer

    (@grunauer)


    Please, I need help I don’t know what to do as I don’t know to code things.

    I want the sales badge to show the discount rate of a product which price is below the base price for example:

    Instead of saying ” save 12 dollars” make it say “40% off” and let the site itself calculate the discount rate by itself every time the price of a product is changed

    Please help me, cause I don’t know how to do it for free with codes.

    The page I need help with: [log in to see the link]

Viewing 4 replies - 16 through 19 (of 19 total)
  • Perfect. Thanks very much

    Works perfect for me! I use this code to display -10% (no space between – and the number)

    add_filter( 'woocommerce_sale_flash', 'add_percentage_to_sale_badge', 20, 3 );
    function add_percentage_to_sale_badge( $html, $post, $product ) {
        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 ){
                    // 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));
                }
            }
            $percentage = max($percentages) . '%';
        } else {
            $regular_price = (float) $product->get_regular_price();
            $sale_price    = (float) $product->get_sale_price();
    
            $percentage    = round(100 - ($sale_price / $regular_price * 100)) . '%';
        }
        return '<span class="onsale">' . esc_html__( '-', 'woocommerce' ) . '' . $percentage . '</span>';
    }
    ranatrader

    (@ranatrader)

    I want to show the discount amount Not Percentage on the product badge.
    Please share the code. i am using wcfm woocommerce.

    thanks advance

    Margaret S. woo-hc

    (@margaretwporg)

    Hi @ranatrader

    Best to take this conversation onto a new thread. Can you please post your question as a new topic?

    Thanks

Viewing 4 replies - 16 through 19 (of 19 total)
  • The topic ‘Show % Discount on Sales Badge’ is closed to new replies.