Be default Woocommerce sale badge reveals solely textual content (“Sale”) however there isn’t a out-of-the-box choice if you need the sale badge to indicate the low cost proportion. For instance, as an alternative of claiming ”Sale” you want to it to indicate “- 40%”.
Due to this fact, on this publish I’m going to indicate you the right way to show the low cost proportion on the Woocommerce sale badge.
Video: Easy methods to Add the Low cost Share on the Woocommerce Sale Badge?
Easy methods to Show the Low cost Share on the Sale Badge (single merchandise solely)?
Step 1: Copy the code right here under
Step 2: Add it to your features.php file. Higher choice, set up and add the code utilizing Code snippets plugin
operate sale_badge_percentage() {
world $product;
if ( ! $product->is_on_sale() ) return;
if ( $product->is_type( 'easy' ) ) {
$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 );
$value = $variation->get_regular_price();
$sale = $variation->get_sale_price();
if ( $value != 0 && ! empty( $sale ) ) $proportion = ( $value – $sale ) / $value * 100;
if ( $proportion > $max_percentage ) {
$max_percentage = $proportion;
}
}
}
if ( $max_percentage > 0 ) echo "<span class='onsale'>-" . spherical($max_percentage) . "%
add_action( 'woocommerce_sale_flash', 'sale_badge_percentage', 25 );
operate sale_badge_percentage() {
world $product;
if ( ! $product->is_on_sale() ) return;
if ( $product->is_type( 'easy' ) ) {
$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 );
$value = $variation->get_regular_price();
$sale = $variation->get_sale_price();
if ( $value != 0 && ! empty( $sale ) ) $proportion = ( $value - $sale ) / $value * 100;
if ( $proportion > $max_percentage ) {
$max_percentage = $proportion;
}
}
}
if ( $max_percentage > 0 ) echo "<span class='onsale'>-" . spherical($max_percentage) . "%</span>"; // If you need to indicate -40% off then add textual content after % signal
}
Easy methods to Show the Low cost Share on the Sale Badge for variable merchandise, single merchandise and grouped merchandise?
If you should show the low cost proportion on the sale badge for each variable merchandise and sale merchandise then use this code.
// Show the Woocommerce Low cost Share on the Sale Badge for variable merchandise and single merchandise
add_filter( 'woocommerce_sale_flash', 'display_percentage_on_sale_badge', 20, 3 );
operate display_percentage_on_sale_badge( $html, $publish, $product ) {
if( $product->is_type('variable')){
$percentages = array();
// This can get all of the variation costs and loop all through them
$costs = $product->get_variation_prices();
foreach( $costs['price'] as $key => $value ){
// Solely on sale variations
if( $costs['regular_price'][$key] !== $value ){
// Calculate and set within the array the share for every variation on sale
$percentages[] = spherical( 100 - ( floatval($costs['sale_price'][$key]) / floatval($costs['regular_price'][$key]) * 100 ) );
}
}
// Shows most low cost worth
$proportion = max($percentages) . '%';
} elseif( $product->is_type('grouped') ){
$percentages = array();
// This can get all of the variation costs and loop all through them
$children_ids = $product->get_children();
foreach( $children_ids as $child_id ){
$child_product = wc_get_product($child_id);
$regular_price = (float) $child_product->get_regular_price();
$sale_price = (float) $child_product->get_sale_price();
if ( $sale_price != 0 || ! empty($sale_price) ) {
// Calculate and set within the array the share for every baby on sale
$percentages[] = spherical(100 - ($sale_price / $regular_price * 100));
}
}
// Shows most low cost worth
$proportion = max($percentages) . '%';
} else {
$regular_price = (float) $product->get_regular_price();
$sale_price = (float) $product->get_sale_price();
if ( $sale_price != 0 || ! empty($sale_price) ) {
$proportion = spherical(100 - ($sale_price / $regular_price * 100)) . '%';
} else {
return $html;
}
}
return '<span class="onsale">' . esc_html__( 'as much as -', 'woocommerce' ) . ' '. $proportion . '</span>'; // If wanted then change or take away "as much as -" textual content
}