Created
December 9, 2013 06:35
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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 ); |
It seems that this is not working with variable products. Any fix for that?
@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 );
Thanks it works fine with WooCommerce 3 +
@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.
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
Exactly what I needed. Thanks