Skip to content

Instantly share code, notes, and snippets.

@akther80
Created December 19, 2018 05:43
Show Gist options
  • Save akther80/d6ceff5062659adb4700032902359b6a to your computer and use it in GitHub Desktop.
Save akther80/d6ceff5062659adb4700032902359b6a to your computer and use it in GitHub Desktop.
Tokoo- Display percentage of discount instead of sale badge
add_action( 'init', 'tk_child_display_discount_percentage_instead_sale_badge' );
function tk_child_display_discount_percentage_instead_sale_badge() {
remove_action( 'woocommerce_before_shop_loop_item_title', 'woocommerce_show_product_loop_sale_flash', 10 );
add_action( 'woocommerce_before_shop_loop_item_title', 'tokoo_get_sale_flash', 10 );
}
function tokoo_get_sale_flash() {
global $product;
if ( $product->is_on_sale() ) {
if ( ! $product->is_type( 'variable' ) ) {
$max_percentage = ( ( $product->get_regular_price() - $product->get_sale_price() ) / $product->get_regular_price() ) * 100;
} else {
$max_percentage = 0;
foreach ( $product->get_children() as $child_id ) {
$variation = wc_get_product( $child_id );
$price = $variation->get_regular_price();
$sale = $variation->get_sale_price();
if ( $price != 0 && ! empty( $sale ) ) {
$percentage = ( $price - $sale ) / $price * 100;
}
if ( $percentage > $max_percentage ) {
$max_percentage = $percentage;
}
}
}
echo "<div class='onsale'>-" . round($max_percentage) . "%</div>";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment