Skip to content

Instantly share code, notes, and snippets.

@thesadoo
Last active October 3, 2016 11:33
Show Gist options
  • Save thesadoo/4c2cd63f4834ac6ba1ef599ac9f4c15d to your computer and use it in GitHub Desktop.
Save thesadoo/4c2cd63f4834ac6ba1ef599ac9f4c15d to your computer and use it in GitHub Desktop.
Get WooCommerce product's discount percentage with a function
<?php
function off_percentage($id=null){
$pid = ($id) ? $id : get_the_ID();
$product = new WC_Product($pid);
$sale_price = $product->sale_price;
$regu_price = $product->regular_price;
$percentage = round( ( ( $regu_price - $sale_price ) / $regu_price ) * 100 );
return $percentage;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment