Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace variable products price range with "From:" + lowest price in WooCommerce

In WooCommerce when a Variable product has variations with different prices it displays a price range with 2 amounts: For example 89.00 - 109.00.

I'd like to change it, displaying only "From: " and the lowest price like for example From: 89.00 (removing the Max price).
("Fra: " means from in my language, just to clarify).

Here is the code that I have tried:

// Main Price
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price = $prices[0] !== $prices[1] ? sprintf( __( 'Fra: %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( __( 'Fra: %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );

if ( $price !== $saleprice ) {
$price = '<del>' . $saleprice . $product->get_price_suffix() . '</del> <ins>' . $price . $product->get_price_suffix() . '</ins>';
}
return $price;
}

This code doesn't work. Whenever I add it nothing happens.

What do I need to change, to get "From: " + the Min price instead?

like image 298
eMikkelsen Avatar asked Dec 07 '25 06:12

eMikkelsen


1 Answers

Your code is a bit incomplete as the hook and the function are missing…

Here is the correct way to make it works for your variable products:

add_filter( 'woocommerce_get_price_html', 'change_variable_products_price_display', 10, 2 );
function change_variable_products_price_display( $price, $product ) {

    // Only for variable products type
    if( ! $product->is_type('variable') ) return $price;

    $prices = $product->get_variation_prices( true );

    if ( empty( $prices['price'] ) )
        return apply_filters( 'woocommerce_variable_empty_price_html', '', $product );

    $min_price = current( $prices['price'] );
    $max_price = end( $prices['price'] );
    $prefix_html = '<span class="price-prefix">' . __('Fra: ') . '</span>';

    $prefix = $min_price !== $max_price ? $prefix_html : ''; // HERE the prefix

    return apply_filters( 'woocommerce_variable_price_html', $prefix . wc_price( $min_price ) . $product->get_price_suffix(), $product );
}

Code goes in function.php file of your active child theme (or theme) or also in any plugin file.

Tested and works.

like image 120
LoicTheAztec Avatar answered Dec 09 '25 21:12

LoicTheAztec