Last active
February 9, 2023 12:16
-
-
Save ashokmhrj/9c1c26a450adb3c3b22f0ebd73a5f598 to your computer and use it in GitHub Desktop.
Woocommerce sale flash in percentage
This file contains 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 | |
/** | |
* 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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 '';
}
}
}`