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 = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>';
    }

    return $price;
}

5 Comments

  1. Chelsie Hall September 8, 2016 at 2:52 pm #

    This snippet is giving me the following error:

    The snippet has been deactivated due to an error on line 10:
    syntax error, unexpected ‘&’

    Can someone please help me?

    • Rosťa Nétek September 25, 2016 at 8:10 pm #

      in the source code replace “>” for “>”

    • Rosťa Nétek September 25, 2016 at 8:11 pm #

      replace “& g t ;” (in original without spaces)

  2. ballsl December 6, 2016 at 4:58 pm #

    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;
    }

  3. ballsl December 20, 2016 at 11:06 pm #

    Any ideas on how to tweak this if using the Woo Membership extension? The code works great, except for logged in members who still see the default display of price (i.e. $9.99 – $ 25.99)
    https://docs.woocommerce.com/document/woocommerce-memberships/

Leave a Reply