WooCommerce Measurement Price Calc - replace price range by min price
Hopefully the title is fairly self explanatory! I'm using Measurement Price Calculator (MPC) in WooCommerce here. I have added the tweak in my functions.php to alter the price display on variable products so that it hides the from - to price range and just shows "from {lowest price}" . I believe this is a fairly common tweak users make in WooCommerce but I'll include the code I used below for good measure as I hope it might be adaptable for MPC also.
My issue is that when using the Pricing Tables feature of MPC, it goes back to showing the price range of the table, e.g. "€0.86 - €0.98 / sq m" . My tweak does not catch this and I have contacted the developers Skyverge, who couldn't give me a fix but did point me to "wc_measurement_price_calculator_get_price_html" filter.
Has anyone here managed to this or similar when using MPC or can anyone suggest a use for the filter suggested by Skyverge?
The code I'm using for variable products is as below:
// Hide Price Range on Variable Products
add_filter( 'woocommerce_variable_sale_price_html', 'variation_price_format', 10, 2 );
add_filter( 'woocommerce_variable_price_html', 'variation_price_format', 10, 2 );
function 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( __( 'from %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( __( 'from %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
if ( $price !== $saleprice ) {
$price = '<del>' . $saleprice . '</del> <ins>' . $price . '</ins>';
}
return $price;
}
Solutions
Without any guaranty, try this:
add_filter('wc_measurement_price_calculator_get_price_html', 'm_p_caculator_variable_product_prices', 10, 1 );
function m_p_caculator_variable_product_prices( $price_html ) {
if( ! $product->is_type('variable') ) return $price_html; // Only variable products
// Main Price
$prices = array( $product->get_variation_price( 'min', true ), $product->get_variation_price( 'max', true ) );
$price_html = $prices[0] !== $prices[1] ? sprintf( __( 'from %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 );
$sale_price = $prices[0] !== $prices[1] ? sprintf( __( 'from %1$s', 'woocommerce' ), wc_price( $prices[0] ) ) : wc_price( $prices[0] );
if ( $price_html !== $saleprice ) {
$price_html = '<del>' . $saleprice . '</del> <ins>' . $price_html . '</ins>';
}
return $price_html;
}
Code goes in function.php file of your active child theme (or active theme). Untested.