Code Snippet: Display WooCommerce highest variable product price only

Last Updated on: 13th July 2018, 02:24 pm

Here is a code snippet that displays the highest price only for WooCommerce variable products instead of the default price range.

add_filter( 'woocommerce_variable_sale_price_html', 'con_show_max_variation_price_only', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'con_show_max_variation_price_only', 10, 2 );
 
function con_show_max_variation_price_only( $price, $product ) {
 
// Main Variation Price
$prices = array( $product->get_variation_price( 'max', true ), $product->get_variation_price( 'min', true ) );

$price = $prices[0] !== $prices[1] ? sprintf( __( '%1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
 
// Sale Variation Price
$prices = array( $product->get_variation_regular_price( 'max', true ), $product->get_variation_regular_price( 'min', true ) );
sort( $prices );

$saleprice = $prices[0] !== $prices[1] ? sprintf( __( '%1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

//Output
if ( $price !== $saleprice ) {
    
    $price = '<span style="display:none;">' . $saleprice . '</span> <ins>' . $price . '</ins>';
    
    }
    
    return $price;

}

Need more? Try this post by Anna Schneider: https://annaschneider.me/hide-the-price-range-for-woocommerce-variable-products/

Based on this handy snippet by Gerhard. Not sure how to use this? Here is how to use custom code snippets like this.

4 responses

  1. This is great! Do you know how I could display the variable pricing (or the highest price, rather than the lowest) on my Shop page? I’m ok w/ the variable pricing showing on the product page, but I want it to also show up on the shop page.

    If it’s helpful, I’m using the Weston theme. See theme shop here: http://demos.themetrust.com/weston/shop/

    Do you have any guidance?

    Thank you!!

    1. Con Schneider Avatar
      Con Schneider

      Hi Molly,

      Glad you like the snippet!
      The code should also alter the display at `/shop/` unless your active theme tampers with the output. I just tried on my local install and it works ok there.
      Additionally it seems to me that http://demos.themetrust.com/weston/shop/ is being correctly altered. Since you have set sale prices, it correctly falls back to the highest sale price.
      When you say “but I want it to also show up on the shop page.” – which product exactly differs in what way, that makes you say that?

      Kind regards,

  2. Hi,

    This is great, thank you for the snippet. How could I display the minimum instead of the maximum with your snippet ?

    Also, when the user changes variations, the variation price also shows on the product page. Is there a way to replace the maximum price of your snippet with the variation price once variations have been chosen ?

    Thank you.

    1. Con Schneider Avatar
      Con Schneider

      Hi Malcolm,

      Thank you for your feedback. I haven’t looked at the snippet for a while, but the modifications sound feasible. At the moment I am busy with other things, but I might give it a shot.

Leave a Reply

Your email address will not be published. Required fields are marked *

Con Schneider