WooCommerce: Set Product Price Based On Other Products!

I’m Italian and I love exclamation marks! I also love WooCommerce customization, as you may know. This time I want to show you how I programmatically define the price of my WooCommerce Mini-Plugin All-Access-Bundle product… based on other products.

The backstory: as of today, I sell 18 WooCommerce plugins, and soon I should reach the 400 mark if all goes well. So, I came up with the idea of creating a bundle, and let customers gain access to all of them within a single, discounted purchase.

Yes, I could have purchased a Product Bundle plugin… but I wanted to see if I could create a bundle out of a Simple product.

The only requirements were: set the regular price based on the total price of the plugins, set its sale price based on a percentage discount, automate this so I don’t need to manually update the bundle price every time I add a new product, and add all plugin products to the order upon bundle purchase (we will see this in another snippet). Enjoy!

This is a WooCommerce Simple product. Its price is calculated based on the price sum total of a whole product category! Every time I add a new product within this category, the price is automatically recalculated.

PHP Snippet: Programmatically Set Product Price Based On The Total Value Of A Product Category

Ok, the heading may be a little confusing so I’ll give you more context:

  • My WooCommerce Mini-Plugin Full Access Bundle product regular price should be the sum of all plugins’ regular price. If I sell 3 plugins – plugin A is $9, plugin B is $7 and plugin C is $14 – the bundle regular price should be 9+7+14 = $30
  • Also, let’s say I wish to discount the bundle price by 80%, I need to set its sale price to $30 * 0.2 = $6

In the snippet below you find 3 sections:

  1. I first calculate the total sum of all products in a category (if the bundle is in the same category, you also need to add the “exclude” parameter to the wc_get_products arguments)
  2. I then filter the bundle product “price display” on shop and single product pages, by showing a slashed price
  3. I finally filter the bundle product price in the Cart, so that this is correctly added to the order
/**
 * @snippet       Set Simple Product Regular and Sale Price
 * @how-to        Get CustomizeWoo.com FREE
 * @author        Rodolfo Melogli
 * @compatible    WooCommerce 7
 * @community     https://businessbloomer.com/club/
 */


// ------------------
// 1. CALCULATE SUM OF PRODUCT PRICES BELONGING TO CATEGORY "PLUGINS"

function bbloomer_calculate_plugin_bundle_price() {
	$args = array(
		'limit' => -1,
		'status' => 'publish',
		'return' => 'ids',
		'category' => array( 'plugins' ),
	);
	$products = wc_get_products( $args );	
	$price = 0;
	foreach ( $products as $plugin_product_id ) {
		$price += (float) get_post_meta( $plugin_product_id, '_regular_price', true );
	}
	return $price;
}

// ------------------
// 2. SET THE DISPLAY PRICE OF THE BUNDLE PRODUCT (ID = 123)

add_filter( 'woocommerce_get_price_html', 'bbloomer_alter_price_display', 9999, 2 );
 
function bbloomer_alter_price_display( $price_html, $product ) {
	if ( $product->get_id() !== 123 ) return $price_html;	
    $price = bbloomer_calculate_plugin_bundle_price();    
    return wc_format_sale_price( $price, $price * 0.2 ) . $product->get_price_suffix(); 
}

// ------------------
// 3. SET THE CART PRICE OF THE BUNDLE PRODUCT (ID = 123)

add_action( 'woocommerce_before_calculate_totals', 'bbloomer_alter_price_cart', 9999 );
 
function bbloomer_alter_price_cart( $cart ) {
    if ( is_admin() && ! defined( 'DOING_AJAX' ) ) return;
    if ( did_action( 'woocommerce_before_calculate_totals' ) >= 2 ) return;
    foreach ( $cart->get_cart() as $cart_item_key => $cart_item ) {
		if ( $cart_item['product_id'] !== 123 ) continue;	
    	$price = bbloomer_calculate_plugin_bundle_price();    
    	$cart_item['data']->set_price( $price * 0.2 ); 
    } 
}

Where to add custom code?

You should place custom PHP in functions.php and custom CSS in style.css of your child theme: where to place WooCommerce customization?

This code still works, unless you report otherwise. To exclude conflicts, temporarily switch to the Storefront theme, disable all plugins except WooCommerce, and test the snippet again: WooCommerce troubleshooting 101

Related content

  • WooCommerce: Disable Variable Product Price Range $$$-$$$
    You may want to disable the WooCommerce variable product price range which usually looks like $100-$999 when variations have different prices (min $100 and max $999 in this case). With this snippet you will be able to hide the highest price, and add a “From: ” prefix in front of the minimum price. At the […]
  • WooCommerce: Hide Price & Add to Cart for Logged Out Users
    You may want to force users to login in order to see prices and add products to cart. That means you must hide add to cart buttons and prices on the Shop and Single Product pages when a user is logged out. All you need is pasting the following code in your functions.php (please note: […]
  • WooCommerce: Hide Prices on the Shop & Category Pages
    Interesting WooCommerce customization here. A client of mine asked me to hide/remove prices from the shop page and category pages as she wanted to drive more customers to the single product pages (i.e. increasing the click-through rate). As usual, a simple PHP snippet does the trick. I never recommend to use CSS to “hide” prices, […]
  • WooCommerce: Add Prefix / Suffix to Product Prices
    Sometimes you may want to add a prefix or a suffix to your prices. It could be something like “From…”, “Only…”, “…tax free” and so on. The first good news is this is very easy to do with a WooCommerce filter (remember, filters change the value of an existing variable, while actions add content). The […]
  • WooCommerce: Display Total Discount / Savings @ Cart & Checkout
    If you love Ecommerce as much as I do, and are passionate about Sales Conversion Rate and reducing Shopping Cart Abandonment, today’s snippet will come in handy. Besides, this is officially the first guest blog on Business Bloomer (have ideas? Send me your proposal here)… so let me officially introduce you to today’s author: Jamie […]

Rodolfo Melogli

Business Bloomer Founder

Author, WooCommerce expert and WordCamp speaker, Rodolfo has worked as an independent WooCommerce freelancer since 2011. His goal is to help entrepreneurs and developers overcome their WooCommerce nightmares. Rodolfo loves travelling, chasing tennis & soccer balls and, of course, wood fired oven pizza. Follow @rmelogli

Questions? Feedback? Customization? Leave your comment now!
_____

If you are writing code, please wrap it like so: [php]code_here[/php]. Failure to complying with this, as well as going off topic or not using the English language will result in comment disapproval. You should expect a reply in about 2 weeks - this is a popular blog but I need to get paid work done first. Please consider joining the Business Bloomer Club to get quick WooCommerce support. Thank you!

Your email address will not be published. Required fields are marked *