What can we help you with?

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

Folks using the 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 ) {
		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 the Dynamic Pricing and Discounts Plugin for WooCommerce.

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

Or check out the documentation section for more related articles.

Previous How to hide striked out price on Product and Shop page in Dynamic Pricing and Discounts Plugin for WooCommerce? (Code Snippet)

5 Comments. Leave new

  • Avatar photo
    davidanthonydit
    July 31, 2019 12:37 PM

    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

    • 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,

  • Hi,
    This code worked for variable products but it removed the discount badge from single product. Can you pls help me with this

    • Avatar photo
      ELEXtensions
      May 17, 2022 9:12 AM

      Thanks for pointing out this. We have updated the code snippet. Please check and let me know if you are still facing any issues.

  • Thanks! It’s working as expected

You must be logged in to post a comment.