Created
December 19, 2018 05:43
-
-
Save akther80/d6ceff5062659adb4700032902359b6a to your computer and use it in GitHub Desktop.
Tokoo- Display percentage of discount instead of sale badge
This file contains hidden or 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
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