/*********************************************************************/ /* 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 = '' . $saleprice . '' . $price . ''; } return $price; }
5 thoughts on “WooCommerce – Show the smallest variation price with from X”
Leave a Comment
You must be logged in to post a comment.
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?
in the source code replace “>” for “>”
replace “& g t ;” (in original without spaces)
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;
}
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/