1

I am working on an online store using Woocommerce, many products have variations in size and price. Is there a way to have the range of prices (highest to lowest) of the variations on the product page?

  • can we see the code displaying your variations? – Tom J Nowell Mar 25 '13 at 13:57
4

try it like this:

/**
* This code should be added to functions.php of your theme
**/
add_filter('woocommerce_variable_price_html', 'custom_variation_price', 10, 2);

function custom_variation_price( $price, $product ) {
$price = '';

if ( !$product->min_variation_price || $product->min_variation_price !== $product->max_variation_price ) $price .= '<span class="from">' . _x('From', 'min_price', 'woocommerce') . ' </span>';
$price .= woocommerce_price($product->get_price());
if ( $product->max_variation_price && $product->max_variation_price !== $product->min_variation_price ) {
$price .= '<span class="to"> ' . _x('to', 'max_price', 'woocommerce') . ' </span>';

$price .= woocommerce_price($product->max_variation_price);
}

return $price;
}

source: https://gist.github.com/mikejolley/1600117

3

Stumbled upon this thread looking for the same solution for Grouped products. Eventually wound up with the code below... so I'm posting it if it helps others. This will not work for variable products, just grouped. Figured this would be relevant since grouped/variable products are fairly similar and I thought others might also stumble upon this thread. You could probably clean up the span some but this is the quick and dirty version that got me going!

/*** Returns Price Range for Grouped Products**/
function wc_grouped_price_html( $price, $product ) {
$all_prices = array();

foreach ( $product->get_children() as $child_id ) {
    $all_prices[] = get_post_meta( $child_id, '_price', true );
}

if ( ! empty( $all_prices ) ) {
    $max_price = max( $all_prices );
    $min_price = min( $all_prices );
} else {
    $max_price = '';
    $min_price = '';
}

$price = '<span class="from">' . _x('From: ', 'min_price', 'woocommerce') . woocommerce_price( $min_price ) .  _x(' to ', 'max_price', 'woocommerce') . ' </span>' . woocommerce_price( $max_price );

return $price;
}
add_filter( 'woocommerce_grouped_price_html', 'wc_grouped_price_html', 10, 2 );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.