Skip to content

Instantly share code, notes, and snippets.

@mennwebs
Created October 26, 2019 11:10
Show Gist options
  • Select an option

  • Save mennwebs/facb2c98aa594d7ba7906d2b505cf3e7 to your computer and use it in GitHub Desktop.

Select an option

Save mennwebs/facb2c98aa594d7ba7906d2b505cf3e7 to your computer and use it in GitHub Desktop.
WooCommerce - Show Sale Percentage Tag - put in content-product.php
function seed_show_sale_percentage() {
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'><small>ส่วนลด</small>" . round($max_percentage) . "%</div>";
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment