Skip to content

Instantly share code, notes, and snippets.

@aslamahamed13
Last active November 9, 2018 06:30
Show Gist options
  • Save aslamahamed13/6c85504b692eb0619c1badc2e174a1bd to your computer and use it in GitHub Desktop.
Save aslamahamed13/6c85504b692eb0619c1badc2e174a1bd to your computer and use it in GitHub Desktop.
Tokoo - Sale Percentage
add_filter( 'woocommerce_after_shop_loop_item_title', 'bbloomer_show_sale_percentage_loop', 30 );
function bbloomer_show_sale_percentage_loop() {
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='sale-perc'>(" . round($max_percentage) . "% OFF)</div>";
}
}
.sale-perc {
color: red !important;
display: inline;
padding: .2em .6em .3em;
font-size: 75%;
font-weight: normal !important;
border-radius: .25em;
background: transparent !important;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment