How to Set a Product Price Based on Other Products in WooCommerce

How to Set a Product Price Based on Other Products in WooCommerce
On this page
  1. Why Calculate a Price Dynamically
  2. Step 1: Calculate the Category Total
  3. Troubleshooting
  4. Frequently Asked Questions
  5. Conclusion

You can set a product price based on other products in WooCommerce by calculating the sum of a related product category’s regular prices, then applying that total (with an optional discount) to a bundle product’s displayed and cart price — automatically recalculating whenever the category changes.

Why Calculate a Price Dynamically

  • A bundle or “all-access” product should always reflect the current total of its included items without manual updates
  • Adding a new product to the category should automatically adjust the bundle price
  • Manually updating a bundle price every time inventory changes is error-prone and easy to forget

Step 1: Calculate the Category Total

PHP
/** Sum the regular price of every published product in a category */
function calculate_category_price_total( $category_slug ) {
    $product_ids = wc_get_products( array(
        'limit'    => -1,
        'status'   => 'publish',
        'return'   => 'ids',
        'category' => array( $category_slug ),
    ) );

    $total = 0;
    foreach ( $product_ids as $product_id ) {
        $total += (float) get_post_meta( $product_id, '_regular_price', true );
    }

    return $total;
}

Step 2: Apply the Calculated Price to the Bundle’s Display

PHP
add_filter( 'woocommerce_get_price_html', 'display_dynamic_bundle_price', 9999, 2 );

function display_dynamic_bundle_price( $price_html, $product ) {
    $bundle_product_id = 33021; // Replace with your bundle product ID
    $discount_multiplier = 0.25; // 75% off the combined total

    if ( $product->get_id() !== $bundle_product_id ) {
        return $price_html;
    }

    $total = calculate_category_price_total( 'all-access-plugins' );
    return wc_format_sale_price( $total, $total * $discount_multiplier ) . $product->get_price_suffix();
}
Set a product price based on other products in WooCommerce: the page before adding the code
Before
Set a product price based on other products in WooCommerce: the page after adding the code
After

Step 3: Apply the Same Price in the Cart

PHP
add_action( 'woocommerce_before_calculate_totals', 'apply_dynamic_bundle_price_to_cart', 9999 );

function apply_dynamic_bundle_price_to_cart( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;

    $bundle_product_id = 33021;
    $discount_multiplier = 0.25;

    foreach ( $cart->get_cart() as $cart_item ) {
        if ( $cart_item['product_id'] !== $bundle_product_id ) continue;
        $total = calculate_category_price_total( 'all-access-plugins' );
        $cart_item['data']->set_price( $total * $discount_multiplier );
    }
}

Troubleshooting

The Bundle Product Counts Itself in the Total

If the bundle lives in the same category as the products it sums, add an exclude argument to wc_get_products() with the bundle’s own ID to avoid an inflated, self-referencing total.

Price Looks Correct on the Page But Wrong at Checkout

Make sure both the display filter (Step 2) and the cart price filter (Step 3) use identical calculation logic — a mismatch between the two is a common source of “advertised vs. charged” price discrepancies.

Frequently Asked Questions

Can I use a fixed discount amount instead of a percentage?

Yes, subtract a flat number from the total instead of multiplying by a percentage — just apply the same formula consistently in both the display and cart functions.

Does this work for variable products as the bundle?

The example targets a simple product; variable products would need the price filter applied per-variation instead, which requires additional logic.

Is there a plugin that does this instead?

Yes, dedicated product bundle plugins handle dynamic bundle pricing more robustly if your use case grows more complex than a single custom bundle.

Conclusion

Setting a product price based on other products in WooCommerce is achievable with a shared calculation function applied to both the price display and the cart total. It keeps a bundle product’s price accurate automatically as the underlying category changes.

See also How to Show a Call for Price Button on the WooCommerce Product Page. Then read How to Replace the Variable Product Price With the Active Variation Price in WooCommerce.

Prefer no code? All-in-One Utilities Woo Add-on adds over 50 WooCommerce features, so you can switch them on without code.

Leave a Reply

Schedule a call