Menu Close

WooCommerce: Set Product Price Based On Other Products!

default

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
 * @donate $9     https://businessbloomer.com/bloomer-armada/
 */


// ------------------
// 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 ); 
    } 
}
View Source
Posted in WooCommerce Tips