Skip to content

Instantly share code, notes, and snippets.

@akther80
Last active January 9, 2019 14:49
Show Gist options
  • Save akther80/e717ca05b9a437dcf8f813897ed43217 to your computer and use it in GitHub Desktop.
Save akther80/e717ca05b9a437dcf8f813897ed43217 to your computer and use it in GitHub Desktop.
Tokoo- Display percentage of discount instead of sale badge in Single Product page
add_action( 'init', 'tk_child_display_discount_percentage_in_single_product_page' );
function tk_child_display_discount_percentage_in_single_product_page() {
remove_action( 'woocommerce_before_single_product_summary', 'woocommerce_show_product_sale_flash', 10 );
add_action( 'woocommerce_before_single_product_summary', 'tokoo_get_sale_flash', 21 );
}
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>";
}
}
.single-product .single-product-inner .onsale {
position: absolute;
top: 0;
left: 15px;
z-index: 1;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment