Advertisement
majeedraza

WooCommerce - Show lowest price only for variable products

Jan 9th, 2022
1,025
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.47 KB | None | 0 0
  1. <?php
  2.   // WooCommerce - Show lowest price only for variable products  
  3.   add_filter( 'woocommerce_variable_price_html', 'custom_variation_price', 10, 2 );
  4.   function custom_variation_price( $price, $product ) {
  5.     // $price is the html for the variation range without suffixes (default)
  6.     // we want the html for the lowest priced variation and with suffixes
  7.     $price = '';
  8.     $variations = $product->get_available_variations();
  9.     $lowest_price = PHP_FLOAT_MAX;
  10.     $all_same_price = true;
  11.     foreach( $variations as $key => $variation ) {
  12.       $display_price = $variation['display_price'];
  13.       if( $display_price < $lowest_price ) {
  14.         $lowest_price = $display_price;
  15.         $variation_id = $variation['variation_id'];
  16.       }
  17.       if( $lowest_price < $display_price ) {
  18.         $all_same_price = false;
  19.       }
  20.     }
  21.     $product = wc_get_product( $variation_id ); // WC_Product_Variation    
  22.     // wc_price() gives: <span class="woocommerce-Price-amount amount"><bdi><span class="woocommerce-Price-currencySymbol">&pound;</span>9.13</bdi></span>
  23.     $ex_tax_price = wc_price( wc_get_price_excluding_tax( $product ) );
  24.     if( ! $all_same_price ) {
  25.       $price .= '<span class="from">From </span>';
  26.     }
  27.     $price .= $ex_tax_price;
  28.     $inc_tax_price = wc_price( wc_get_price_including_tax( $product ) );
  29.     $price .= '<small class="woocommerce-price-suffix"> (Exc VAT), ' . $inc_tax_price . ' (Inc VAT)</small>';
  30.     return $price;
  31.   } // end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement