-
-
Save ashokmhrj/9c1c26a450adb3c3b22f0ebd73a5f598 to your computer and use it in GitHub Desktop.
<?php | |
/** | |
* shows percentage in flash sales | |
*/ | |
add_filter( 'woocommerce_sale_flash', 'ask_percentage_sale', 11, 3 ); | |
function ask_percentage_sale( $text, $post, $product ) { | |
$discount = 0; | |
if ( $product->get_type() == 'variable' ) { | |
$available_variations = $product->get_available_variations(); | |
$maximumper = 0; | |
for ($i = 0; $i < count($available_variations); ++$i) { | |
$variation_id=$available_variations[$i]['variation_id']; | |
$variable_product1= new WC_Product_Variation( $variation_id ); | |
$regular_price = $variable_product1->get_regular_price(); | |
$sales_price = $variable_product1->get_sale_price(); | |
if( $sales_price ) { | |
$percentage= round( ( ( $regular_price - $sales_price ) / $regular_price ) * 100 ) ; | |
if ($percentage > $maximumper) { | |
$maximumper = $percentage; | |
} | |
} | |
} | |
$text = '<span class="onsale">' . $maximumper . '%</span>'; | |
} elseif ( $product->get_type() == 'simple' ) { | |
$percentage = round( ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100 ); | |
$text = '<span class="onsale">' . $percentage . '%</span>'; | |
} | |
return $text; | |
} |
Thanks for this!
If anyone has any trouble getting this to work like i did, in my case i just increased the priority parameter.
I used my own custom code works super nice then you can add css to change the appearance of the badge
`/* Replace text of Sale! badge with percentage */
add_filter( 'woocommerce_sale_flash', 'ds_replace_sale_text' );
function ds_replace_sale_text($text) {
global $product;
$stock = $product->get_stock_status();
$product_type = $product->get_type();
$sale_price = 0;
$regular_price = 0;
if($product_type == 'variable'){
$product_variations = $product->get_available_variations();
foreach ($product_variations as $kay => $value){
if($value['display_price'] < $value['display_regular_price']){
$sale_price = $value['display_price'];
$regular_price = $value['display_regular_price'];
}
}
if($regular_price > $sale_price && $stock != 'outofstock') {
$product_sale = intval(((intval($regular_price) - floatval($sale_price)) / floatval($regular_price)) * 100);
if ($product_sale > 5 ) {
return ' - ' . esc_html($product_sale) . '% Sonderpreis!';
}
if ($product_sale <= 5 ) {
return ' Sale!';
}
}else{
return '';
}
}else{
$regular_price = get_post_meta( get_the_ID(), '_regular_price', true);
$sale_price = get_post_meta( get_the_ID(), '_sale_price', true);
if($regular_price > 5) {
$product_sale = intval(((floatval($regular_price) - floatval($sale_price)) / floatval($regular_price)) * 100);
return ' -' . esc_html($product_sale) . '% Sonderpreis!';
}
if($regular_price >= 0 && $regular_price <= 5 ) {
$product_sale = intval(((floatval($regular_price) - floatval($sale_price)) / floatval($regular_price)) * 100);
return ' Sale!';
}
else{
return '';
}
}
}`
seems effective, yet I experience huge slowdown when using this snippet on WooCommerce 3.5.3, on 3.4.7 it works fine (or similar code to this one as I had some more calculations in my snippet but yours is far more effective and still it slows down the page from 1s to 11s)