Site logo

Archived topic

Discount rate on product page

16 replies · Started by Pedro on March 8, 2019

Viewing posts 1–15 of 17

Hello David! Can you tell me how to do it please? I'm not an expert in programming.

What I want is to add a small box next to the discounted price with the percentage of the discount, as I showed you in the image.

Thank you!

You'll have to start paying us sales commissions soon lol :)

So this PHP Snippet add it to your child theme function.php or the Code Snippets plugin:

add_filter( 'woocommerce_get_price_html', 'change_displayed_sale_price_html', 10, 2 );
function change_displayed_sale_price_html( $price, $product ) {
    // Only on sale products on frontend and excluding min/max price on variable products
    if( is_product() && $product->is_on_sale() && ! is_admin() ){
        // Get product prices
        $regular_price = (float) $product->get_regular_price(); // Regular price
        $sale_price = (float) $product->get_price(); // Active price (the "Sale price" when on-sale)

        // "Saving Percentage" calculation and formatting
        $precision = 1; // Max number of decimals
        $saving_percentage = round( 100 - ( $sale_price / $regular_price * 100 ), 1 ) . '%';

        // Append to the formated html price
        $price .= sprintf( __('<span class="saved-sale">-%s</span>', 'woocommerce' ), $saving_percentage );
    }
    return $price;
}

Then add this CSS and adjust it for your colors etc:

.saved-sale {
    margin-left: 10px;
    padding: 2px 10px;
    background-color: #ff0000;
    color: #fff;
}

aah the problem is with variable products. That code won't work as it doesn't use the standard prices. And i can't find ant smart guys code that actually works.

Right so last attempt lol - if this don't work then you will need to look for a developer or possibly a plugin. So this snippet:

add_action( 'woocommerce_single_product_summary', 'db_show_sale_percentage_loop', 10 );
  
function db_show_sale_percentage_loop() {
    global $product;
    if ( ! $product->is_on_sale() ) return;
    if ( $product->is_type( 'simple' ) ) {
        $max_percentage = ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100;
    } elseif ( $product->is_type( 'variable' ) ) {
        $max_percentage = 0;
        foreach ( $product->get_children() as $child_id ) {
            $variation = wc_get_product( $child_id );
            $price = $variation->get_regular_price();
            $sale = $variation->get_sale_price();
            if ( $price != 0 && ! empty( $sale ) ) $percentage = ( $price - $sale ) / $price * 100;
            if ( $percentage > $max_percentage ) {
                $max_percentage = $percentage;
            }
        }
    }
    if ( $max_percentage > 0 ) echo "<div class='sale-perc'>-" . round($max_percentage) . "%</div>"; 
}

And this CSS:

.woocommerce div.product p.price {
display: inline;
}

.sale-perc {
    display: inline;
    margin-left: 10px;
    padding: 2px 10px;
    background-color: #ff0000;
    color: #fff;
}

In this line of code:

add_action( 'woocommerce_single_product_summary', 'db_show_sale_percentage_loop', 10 );

See the number 10. Thats the priority. Increasing the number will move it down the stack of elements. So try increasing this.

If you want to add it back it at 15 then try this CSS:

.woocommerce div.product p.price, .saved-sale {
    display: inline-block;
}

Hello david! Yes please, it would be great

Hello david!

It worked very well, however for the mobile version it is not next to the price since it does not fit on the screen.

Is it possible to reduce the size of the text?

Thank you!

You can edit this CSS and add a font-size:

.sale-perc {
    display: inline;
    margin-left: 10px;
    padding: 2px 10px;
    background-color: #ff0000;
    color: #fff;
    font-size: 12px; /* Adjust font size */
}
This archived topic is closed to new replies.