How to Display Sale Percentage for Variable Products using Dynamic Pricing and Discounts for WooCommerce? (Code Snippet)

You are here:

Folks using VendiPro theme are used to seeing a sale percentage tag when a discount is applied to variable products. But when you apply discounts using our Dynamic Pricing and Discounts Plugin for WooCommerce, the sale tag does not seem to show the discounted percentage, even though the discount is applied to the products.

Hence to resolve this, you can add the code snippet provided below.

Code snippet to display Sale Percentage for Variable products in VendiPro theme

Add the following code snippet in the functions.php file of your website theme.

add_filter( 'woocommerce_sale_flash', 'set_sale_flash', 100, 3 );

function set_sale_flash( $html, $post, $product ) {
		$html = '';
		if ( $product->get_type() == 'variable' ) {
                    $prices = $product->get_variation_prices(true);

                    if (empty($prices['price'])) {
                        return $html;
                    }
                    foreach ($prices['price'] as $pid => $old_price) {
                        $pobj = wc_get_product($pid);
                        $prices['price'][$pid] = wc_get_price_to_display($pobj);
                    }
                    asort($prices['price']);
                    $sale_min = current($prices['price']);
                    $sale_max = end($prices['price']);
                    asort($prices['regular_price']);
                    $regular_min = current($prices['regular_price']);
                    $regular_max = end($prices['regular_price']);
                    if ( $regular_min > 0 && $regular_max > 0 ) {
			$perc_min = round( 100 - ( (100 / $regular_min) * $sale_min ) );
			$perc_max = round( 100 - ( (100 / $regular_max) * $sale_max ) );
			$html = '<span class="badge badge-sale">-' . ( ( $perc_min != $perc_max ) ? ( $perc_min . '-' . $perc_max ) : $perc_max ) . '%</span>';
                    }
		}
		return $html;
	}

Remember, this code snippet will only work if you are using the VendiPro theme.

 

To know more about the product, check out Dynamic Pricing and Discounts Plugin for WooCommerce.

To know more about other features of the plugin, read product setting up article.

Or check out documentation section for more related articles.

2 Comments

  1. davidanthonydit
    How can you display the difference instead of the discount percent? For example You save:$. The difference between the full price and the sale price? Thanks
    • Raji
      Hello!Please find below the updated code for achieving your business case and let us know the feedback.
      add_filter( 'woocommerce_sale_flash', 'set_sale_flash', 100, 3 );function set_sale_flash( $html, $post, $product ) { $html = ''; if ( $product->get_type() == 'variable' ) { $prices = $product->get_variation_prices(true); if (empty($prices['price'])) { return $html; } foreach ($prices['price'] as $pid => $old_price) { $pobj = wc_get_product($pid); $prices['price'][$pid] = wc_get_price_to_display($pobj); } $min_price_diff = 0; $max_price_diff = 0; $temp = 0; foreach($prices['regular_price'] as $key=>$reg_price){ if($temp == 0) { $min_price_diff = $max_price_diff = $reg_price - $prices['price'][$key]; } else { if(($reg_price - $prices['price'][$key]) < $min_price_diff) { $min_price_diff = $reg_price - $prices['price'][$key]; } if(($reg_price - $prices['price'][$key]) > $max_price_diff) { $max_price_diff = $reg_price - $prices['price'][$key]; } } $temp++; }asort($prices['price']); $sale_min = current($prices['price']); $sale_max = end($prices['price']); asort($prices['regular_price']); $regular_min = current($prices['regular_price']); $regular_max = end($prices['regular_price']);if ( $regular_min > 0 && $regular_max > 0 ) { $html = 'save: $' . ( ( $min_price_diff != $max_price_diff ) ? ( $min_price_diff . ' to $' . $max_price_diff ) : $min_price_diff ) . ''; } } return $html; }
      Thanks,

Leave a Reply

WordPress Image Lightbox Plugin