WooCommerce 2.1 change price range to WooCommerce 2.0 From priceWith WooCommerce 2.1 just being released a couple of days ago, a lot of users may have noticed a couple of big changes to the plugin, like a refined settings section and the introduction of an all new REST API.

The aim with each major WooCommerce release is to simplify and make it faster and more scalable, and with WooCommerce 2.1 this meant that a lot of the setting that was rarely uses was removed and a few formatting changes was made based on customer feedback.

One of the formatting changes that was made was to remove the “From: $x” price formatting of variation products in favor of a range ie “$x – $y”. This new range format for variable product may not appeal to everyone and that is where the following snippet comes into play.

The code snippet will change the new range price format back to the “From:” price format that users are accustomed to in WooCommerce 2.0.

To revert your WooCommerce variation prices back to the “From:” price format add the following code to your theme’s functions.php file


/**
* Use WC 2.0 variable price format, now include sale price strikeout
*
* @param string $price
* @param object $product
* @return string
*/
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( __( '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;
}
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 );

view raw

functions.php

hosted with ❤ by GitHub