Search
 
SCRIPT & CODE EXAMPLE
 

PHP

woocommerce show percentage in sales badge

For variable products:

// Display the Woocommerce Discount Percentage on the Sale Badge for variable products and single products
add_filter( 'woocommerce_sale_flash', 'display_percentage_on_sale_badge', 20, 3 );
function display_percentage_on_sale_badge( $html, $post, $product ) {

  if( $product->is_type('variable')){
      $percentages = array();

      // This will get all the variation prices and loop throughout them
      $prices = $product->get_variation_prices();

      foreach( $prices['price'] as $key => $price ){
          // Only on sale variations
          if( $prices['regular_price'][$key] !== $price ){
              // Calculate and set in the array the percentage for each variation on sale
              $percentages[] = round( 100 - ( floatval($prices['sale_price'][$key]) / floatval($prices['regular_price'][$key]) * 100 ) );
          }
      }
      // Displays maximum discount value
      $percentage = max($percentages) . '%';

  } elseif( $product->is_type('grouped') ){
      $percentages = array();

       // This will get all the variation prices and loop throughout them
      $children_ids = $product->get_children();

      foreach( $children_ids as $child_id ){
          $child_product = wc_get_product($child_id);

          $regular_price = (float) $child_product->get_regular_price();
          $sale_price    = (float) $child_product->get_sale_price();

          if ( $sale_price != 0 || ! empty($sale_price) ) {
              // Calculate and set in the array the percentage for each child on sale
              $percentages[] = round(100 - ($sale_price / $regular_price * 100));
          }
      }
     // Displays maximum discount value
      $percentage = max($percentages) . '%';

  } else {
      $regular_price = (float) $product->get_regular_price();
      $sale_price    = (float) $product->get_sale_price();

      if ( $sale_price != 0 || ! empty($sale_price) ) {
          $percentage    = round(100 - ($sale_price / $regular_price * 100)) . '%';
      } else {
          return $html;
      }
  }
  return '<span class="onsale">' . esc_html__( 'up to -', 'woocommerce' ) . ' '. $percentage . '</span>'; // If needed then change or remove "up to -" text
}
Comment

PREVIOUS NEXT
Code Example
Php :: php run python script with arguments json 
Php :: Cakephp api POST request , saving data without validation 
Php :: null coalesce operator in php (laravel) 
Php :: php debug backtrace last function 
Php :: nested relation 
Php :: source code in html to add two numbers together 
Php :: load player avatar url 
Php :: the_field 
Php :: Final class constants - PHP 8.1 
Php :: onde que fica a praia escondida no roblox jo mulher maravilha 
Php :: Type error: Argument 1 passed to IlluminateDatabaseGrammar::parameterize() 
Php :: Date and time Asia karachi php 
Php :: how to add user profile image in my account page in woocommerce 
Php :: protocals supported by php 
Php :: school management system in codeigniter free download 
Php :: customize response body with filters laravel 
Php :: $SERVER get cuurent directior PHP 
Php :: Verifying session info 
Php :: jobs laravel 
Php :: how to validate email or phone number single parameter request in laravel 
Php :: redaxo urlGenerator, urlGenerator::getId(), Class "UrlGenerator" not found 
Php :: CarbonTraitsUnits.php:69 
Php :: How to create an Invoice with watermark FPDF 
Php :: laravel title dynamic 
Php :: can we generate alphanumeric 6 digit primary key in phpmyadmin 
Php :: php isset and test 
Php :: add header image to woocomerce shop page 
Php :: Laravel - Controller get select value from Views 
Php :: Namespace declaration statement has to be the very first statement or after any declare call in the script in file D:Xampphtdocsprojectsmulti_vender_siteappModelsUser.php on line 5 
Php :: php if form fails keep data 
ADD CONTENT
Topic
Content
Source link
Name
3+9 =