Replace the Variable Price Range by the Chosen Variation Price in Woocommerce 4+

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

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.

How can I remove price range for variable products with only one variation?

Updated (replaced incorrect $this with $product).

Try the following, that should remove the price range for variable products with one variation:

add_filter( 'woocommerce_variable_price_html', 'variable_products_with_one_variation', 10, 2 );
function variable_products_with_one_variation( $price_html, $product ) {
$prices = $product->get_variation_prices( true );

if( count($prices['price']) == 1 ) {
$active_price = end( $prices['price'] );
$reg_price = end( $prices['regular_price'] );

if ( $this->is_on_sale() ) {
$price = wc_format_sale_price( wc_price( $reg_price ), wc_price( $active_price ) );
} else {
$price = wc_price( $active_price );
}
}
return $price;
}

It should work. Anyways if it doesn't work, this is the right hook to be used.



Related Topics



Leave a reply



Submit