Skip to content

Instantly share code, notes, and snippets.

@vividvilla
Last active October 18, 2018 10:10
Show Gist options
  • Save vividvilla/7137659 to your computer and use it in GitHub Desktop.
Save vividvilla/7137659 to your computer and use it in GitHub Desktop.
Display Discount/Offer percentage in WooCommerce
<?php
/**
* Product loop sale flash
*
* @author Vivek R @ WPSTuffs.com
* @package WooCommerce/Templates
* @version 1.6.4
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
global $post, $product;
?>
<?php if ($product->is_on_sale() && $product->product_type == 'variable') : ?>
<div class="bubble">
<div class="inside">
<div class="inside-text">
<?php
$available_variations = $product->get_available_variations();
$maximumper = 0;
for ($i = 0; $i < count($available_variations); ++$i) {
$variation_id=$available_variations[$i]['variation_id'];
$variable_product1= new WC_Product_Variation( $variation_id );
$regular_price = $variable_product1 ->regular_price;
$sales_price = $variable_product1 ->sale_price;
$percentage= round((( ( $regular_price - $sales_price ) / $regular_price ) * 100),1) ;
if ($percentage > $maximumper) {
$maximumper = $percentage;
}
}
echo $price . sprintf( __('%s', 'woocommerce' ), $maximumper . '%' ); ?></div>
</div>
</div><!-- end callout -->
<?php elseif($product->is_on_sale() && $product->product_type == 'simple') : ?>
<div class="bubble">
<div class="inside">
<div class="inside-text">
<?php
$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
echo $price . sprintf( __('%s', 'woocommerce' ), $percentage . '%' ); ?></div>
</div>
</div><!-- end bubble -->
<?php endif; ?>
@marcelo2605
Copy link

For me, it return an error:

Undefined variable: price in ...woocommerce\loop\sale-flash.php on line 30

@bruoneightwo
Copy link

bruoneightwo commented Sep 19, 2017

Is not working on the single product page. How do I fix that?

@ishtiyaq
Copy link

ishtiyaq commented Sep 26, 2017

Thanks! it's working for me, I added this code using snippet plugin.

add_filter('woocommerce_sale_flash', 'ishu_woo_savings_on_sales_flash');

function ishu_woo_savings_on_sales_flash() {
	global $post, $product;
   $sale_flash = '';
	
	if ($product->is_on_sale() && $product->product_type == 'variable') {
		$available_variations = $product->get_available_variations();								
		$maximumper = 0;
		
	  	for ($i = 0; $i < count($available_variations); ++$i) {
			$variation_id=$available_variations[$i]['variation_id'];
			$variable_product1= new WC_Product_Variation( $variation_id );
			$regular_price = $variable_product1 ->regular_price;
			$sales_price = $variable_product1 ->sale_price;
			$percentage= round((( ( $regular_price - $sales_price ) / $regular_price ) * 100),1) ;
			
		  	if ($percentage > $maximumper) {
				$maximumper = $percentage;
			}
		}
	  
		$savings = $price . sprintf( __('%s', 'woocommerce' ), $maximumper . '%' );
	} else if ($product->is_on_sale() && $product->product_type == 'simple') {
		$percentage = round( ( ( $product->regular_price - $product->sale_price ) / $product->regular_price ) * 100 );
		$savings = $price . sprintf( __('%s', 'woocommerce' ), $percentage . '%' );
	}
  
  	if ($product->is_on_sale()) {
  		$sale_flash = '<span class="onsale">' . $savings . ' OFF</span>';
	}
  
  	return $sale_flash;
}

@anhdq1801
Copy link

Hi,

This have issue when one variation of product have sale price and one variation don't have sale price

  • Variation 1: Regular price: $18 - Sale price: $16
  • Variation2: Regular price: $20

The error message is
A non-numeric value encountered in woocommerce/templates/single-product/sale-flash.php on 27

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment