WooCommerce – Show the smallest variation price with from X

/*********************************************************************/
/*			SHOW ONLY THE SMALLEST VARIATION PRICE					*/
/*********************************************************************/

add_filter( 'woocommerce_variable_sale_price_html', 'wc_wc20_variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'wc_wc20_variation_price_format', 10, 2 );

function wc_wc20_variation_price_format( $price, $product ) {
    // Main Price
    $prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
    $price = $prices[0] !== $prices[1] ? sprintf( __( 'od %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

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

    if ( $price !== $saleprice ) {
        $price = '' . $saleprice . ' ' . $price . '';
    }

    return $price;
}

5 thoughts on “WooCommerce – Show the smallest variation price with from X”

  1. Hi, thanks a lot for the code snippet above. It works great on the product pages after tweaking the code (as shown below) to show the prices as “Starting $9.99” for example.

    The only issue is, in the shop and category pages for on sale products it shows 3 lines. For example, “Starting at ,11.99 9.99 is show in 3 lines.
    Line 1: Starting at
    Line 2: $11.99 (which is formatted to show a strike out for the regular price)
    Line 3: $9.99 (regular price)

    Any idea how to fix this? Variable products not on sale display fine (i.e. Starting at $9.99 all in 1 line). This issue is only for shop, category and other pages where products are listed.

    Appreciate any help in tweaking the code. I am using Divi as my theme if that makes any difference and have added this code to functions.php in my child theme.

    function wc_wc20_variation_price_format( $price, $product ) {
    // Main Price
    $prices = array( $product->get_variation_price( ‘min’, true ), $product->get_variation_price( ‘max’, true ) );
    $price = $prices[0] !== $prices[1] ? sprintf( __( ‘%1$s’, ‘woocommerce’ ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

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

    if ( $price !== $saleprice ) {
    $price = ‘Starting at ‘.’‘ . $saleprice . ‘ ‘ . $price . ”;
    }

    if ( $price == $saleprice ) {
    $price = ‘Starting at ‘.’ ‘ . $price . ”;
    }

    return $price;
    }

Leave a Comment