Arin Issa
Arin Issa

Reputation: 31

Show total price for grouped product on shop page in Woocommerce

I want to show the total summed price of my grouped products instead of the price range. I already fixed this on the product page with this snippet.

How can I use this code or fix my issue on the shop page?

global $product;
$price = $product->get_price_html();
if ( $product->get_type() == 'grouped') {
    $children = $product->get_children();
    $price = 0;
    foreach ($children as $key => $value) {
        $_product = wc_get_product( $value );
        $price += $_product->get_price();
    }
    $price = get_woocommerce_currency_symbol( '' ) . ' ' . $price;
}
?>
<p class="price"><?php echo $price; ?></p>

Upvotes: 3

Views: 4835

Answers (2)

spinozarabel
spinozarabel

Reputation: 41

I tested the following solution and it works on Woocommerce 3.6.3. After this the group product displays a price which is the sum total of all its child products.

function max_grouped_price( $price_this_get_price_suffix, $instance, $child_prices ) { 

    return wc_price(array_sum($child_prices)); 
} 

add_filter( 'woocommerce_grouped_price_html', 'max_grouped_price', 10, 3 );

Upvotes: 2

Alexander Z
Alexander Z

Reputation: 604

Try to use global filter to change the price returned in shop loop:

add_filter( 'woocommerce_get_price_html', 'highest_price', 10, 2 );

function highest_price( $price, $product ) {
  if ( $product->get_type() == 'grouped') {
    $children = $product->get_children();
    $price = 0;
    foreach ($children as $key => $value) {
      $_product = wc_get_product( $value );
      $price += $_product->get_price();
    }
    $price = get_woocommerce_currency_symbol( '' ) . ' ' . $price;
  }
  return $price;
}

I can not test this solution, but if it's not working, try to add filter to another function, not woocommerce_get_price_html, for example woocommerce_template_loop_price

Upvotes: 1

Related Questions