Skip to content

Instantly share code, notes, and snippets.

@shazdeh
Created December 9, 2013 06:35
Show Gist options
  • Save shazdeh/7868242 to your computer and use it in GitHub Desktop.
Save shazdeh/7868242 to your computer and use it in GitHub Desktop.
WooCommerce: Show the saved percentage on the sale products in the the sale flash
<?php
function custom_product_sale_flash( $output, $post, $product ) {
$percent = ( ( $product->regular_price - $product->get_price() ) / $product->regular_price ) * 100;
return '<span class="onsale">' . __( 'Save:', 'themify' ) . round( $percent ) . '% </span>';
}
add_filter( 'woocommerce_sale_flash', 'custom_product_sale_flash', 11, 3 );
@marcelo2605
Copy link

Exactly what I needed. Thanks

@soldier99
Copy link

It seems that this is not working with variable products. Any fix for that?

@moccy
Copy link

moccy commented Nov 23, 2017

@soldier99
Here is what I used :)

function custom_product_sale_flash( $output, $post, $product ) {
    global $product;
    if($product->is_on_sale()) {
        if($product->is_type( 'variable' ) )
        {
            $regular_price = $product->get_variation_regular_price();
            $sale_price = $product->get_variation_price();
        } else {
            $regular_price = $product->get_regular_price();
            $sale_price = $product->get_sale_price();
        }
        $percent_off = (($regular_price - $sale_price) / $regular_price) * 100;
        return '<span class="onsale">' . round($percent_off) . '% OFF</span>';
    }
}
add_filter( 'woocommerce_sale_flash', 'custom_product_sale_flash', 11, 3 );

@NicBeltramelli
Copy link

Thanks it works fine with WooCommerce 3 +

@deltaalpha150
Copy link

deltaalpha150 commented Oct 12, 2018

@soldier99
Here is what I used :)

function custom_product_sale_flash( $output, $post, $product ) {
    global $product;
    if($product->is_on_sale()) {
        if($product->is_type( 'variable' ) )
        {
            $regular_price = $product->get_variation_regular_price();
            $sale_price = $product->get_variation_price();
        } else {
            $regular_price = $product->get_regular_price();
            $sale_price = $product->get_sale_price();
        }
        $percent_off = (($regular_price - $sale_price) / $regular_price) * 100;
        return '<span class="onsale">' . round($percent_off) . '% OFF</span>';
    }
}
add_filter( 'woocommerce_sale_flash', 'custom_product_sale_flash', 11, 3 );

@moccy
Hey guys,

I know this is an old post but I think I need some help with this.

The code is working great but I have a issue with the style it comes up with. The Off is completely out of the "sale"circle. I attached a picture.
I highlighted the text so it can be seen more clearly.

ice_screenshot_20181012-030149.

I think it might be fixed with css styles, but I wouldn't know how to even call that variable to fix it.

Has anybody had the same issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment