·

photo credit: calculator via photopin (license)

# How to calculate prices for bundled products in Magento

Ok, so here is my situation: I have a bundled product in Magento, who’s sub-products can be selected with checkboxes. Now, this bundled product also has 2 custom options that change the price by percentage. Now, the default behavior of Magento is as follows:

$finalPrice = basePrice + customOptions + bundledProducts\\$
But in my situation, I had to calculate the custom options of my bundled product according to the cumulative total of the products the customer had chosen. So, the behavior had to be altered to:

$finalPrice = (basePrice + bundleProducts) + customOptions\\$
This way, the price of the bundled products was calculated prior to the custom options of my bundled product (which would change the total price by percentage). I couldn’t find any working solution on the web, and after trying out a hacky kind of workaround, I finally came up with a very, very simple solution:

## Calculate the bundled items prior to it’s bundles’ custom options

The price of a bundled product is calculated in the class Mage_Bundle_Model_Product_Price, in the function getFinalPrice() to be exact. When you look into this function you’ll notice two lines in particular:

As you already might have noticed, these lines make sure that \$finalPrice is first adjusted with the custom options (_applyOptionsPrice()), and then the price of the bundled items are added (getTotalBundleItemsPrice()). So, if you want to calculate the bundled items price prior to it’s bundles’ custom options, you simple have to swap these two lines:

Tada! It’s that simple. Now up to the next part…

## Cumulative percentual prices for bundled products

This was my second problem I stumbled upon. When it comes to percentual price changes, Magento’s default behavior is to calculate everything according to it’s base price. So say you’ve got a bundled product with a base price of ? 100,-. Option A adds 10% to the price, and option B adds 20% to the price. Magento will calculate this as follows:

$finalPrice = basePrice + \\\indent (basePrice \times optionAPercent) +\\\indent (basePrice \times optionBPercent)$
The resulting price of this formula will be ? 130,-. But if you want the percentages to be cumulative, then you want the formula to be:

$finalPrice = basePrice + \\\indent (basePrice \times optionAPercent \times optionBPercent)$
The resulting price of this formula will be ? 132,-. Once again, the solution to this problem is fairly simple, and only requires you to change 1 parameter in the code. In the same class (Mage_Bundle_Model_Product_Price) there is also a function called _applyOptionsPrice(). In this function, locate the line:

And change it to:

This is a small change, but it will make sure that each custom option that is calculated, does so from the price it has build up so far, instead of the base price.

## How can I implement this?

Well, the most simple way is to copy the Mage_Bundle_Model_Product_Price to app/code/local/Mage/Bundle/Model/Product/Price.php, but the more lean way of doing this of course, is to create a tiny extension for this. Unfortunately, I haven’t got the time at the moment to do so, but perhaps next week I’ll have something up and running for you to download. Download it here.

Visitors give this article an average rating of 3.8 out of 5.