Você está visualizando atualmente WooCommerce: Set Product Price Based On Other Products!

WooCommerce: Set Product Price Based On Other Products!

  • Autor do post:
  • Categoria do post:Woocommerce
  • Tempo de leitura:6 minutos de leitura

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 a future 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 ); } }

Was this article helpful?

YesNo

Where to add custom code?

You should place PHP snippets at the bottom of your child theme functions.php file and CSS at the bottom of its style.css file. Make sure you know what you are doing when editing such files – if you need more guidance, please take a look at my guide “Should I Add Custom Code Via WP Editor, FTP or Code Snippets?” and my video tutorial “Where to Place WooCommerce Customization?”

Does this snippet (still) work?

Please let me know in the comments if everything went as expected. I would be happy to revise the snippet if you report otherwise (please provide screenshots). I have tested this code with Storefront theme, the WooCommerce version listed above and a WordPress-friendly hosting.

If you think this code saved you time & money, feel free to join 17,000+ WooCommerce Weekly subscribers for blog post updates and 250+ Business Bloomer supporters for 365 days of WooCommerce benefits. Thank you in advance!

Need Help with WooCommerce?

Check out these free video tutorials. You can learn how to customize WooCommerce without unnecessary plugins, how to properly configure the WooCommerce plugin settings and even how to master WooCommerce troubleshooting in case of a bug!

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

Post navigation

Source