Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- // WooCommerce - Show lowest price only for variable products
- add_filter( 'woocommerce_variable_price_html', 'custom_variation_price', 10, 2 );
- function custom_variation_price( $price, $product ) {
- // $price is the html for the variation range without suffixes (default)
- // we want the html for the lowest priced variation and with suffixes
- $price = '';
- $variations = $product->get_available_variations();
- $lowest_price = PHP_FLOAT_MAX;
- $all_same_price = true;
- foreach( $variations as $key => $variation ) {
- $display_price = $variation['display_price'];
- if( $display_price < $lowest_price ) {
- $lowest_price = $display_price;
- $variation_id = $variation['variation_id'];
- }
- if( $lowest_price < $display_price ) {
- $all_same_price = false;
- }
- }
- $product = wc_get_product( $variation_id ); // WC_Product_Variation
- // wc_price() gives: <span class="woocommerce-Price-amount amount"><bdi><span class="woocommerce-Price-currencySymbol">£</span>9.13</bdi></span>
- $ex_tax_price = wc_price( wc_get_price_excluding_tax( $product ) );
- if( ! $all_same_price ) {
- $price .= '<span class="from">From </span>';
- }
- $price .= $ex_tax_price;
- $inc_tax_price = wc_price( wc_get_price_including_tax( $product ) );
- $price .= '<small class="woocommerce-price-suffix"> (Exc VAT), ' . $inc_tax_price . ' (Inc VAT)</small>';
- return $price;
- } // end function
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement