Magento – Make custom option price to be added to bundle product price in cart

bundlecustom-optionsmagento-1.9price

All this for bundle products with price type dynamic – when prices are calculated based on selections products.

Mage_Bundle_Model_Product_Price::PRICE_TYPE_DYNAMIC === 0

A custom option of type checkbox was added to a bundle product.
The custom option is passed correctly when product is added to cart and further in the order in admin. This is ok.
On the product page the correct price is displayed: bundle price + custom option price.

The price of the custom option is not cumulated with the bundle product price on cart page.

I've debugged a lot Magento on this feature and I can't seem to find where the price of the custom option is added to a simple product price in cart.
Better said: where is the sum of custom option price and simple product price first time calculated or/and stored in quote item.

I also searched for the part of code for bundle products where the custom option price is disregarded or even unset/destroyed.

I've followed a few debug backtraces:
– from cart controller addAction function,
– getFinalPrice in type/Price.php classes
and other.

What I can say is that getFinalPrice function seems to return the correct price (bundle price + custom option price).

Anyone knows how this feature could be approached ?

I'm not interrested in using an extension for this. I know there are available extensions for this feature.

EDIT:

Further investigating, quote items may be saved for the first time when adding a product to cart (addAction) in:

Mage_Sales_Model_Quote::_afterSave()
$this->getItemsCollection()->save(); // with this

The price in quote item is set for the first time during

Mage_Sales_Model_Quote::collectTotals()
$address->collectTotals(); // with this line when address is shipping

Function setPrice of quote item is called for the first time in:

Mage_Sales_Model_Quote_Address_Total_Subtotal::_initItem

Until now the price is correct. It sums the bundle total selections price with the custom option price and set it with setPrice.

The problem is in:

Mage_Tax_Model_Sales_Total_Quote_Subtotal::_recalculateParent()

where it recalculates the parent product price (the bundle product – current item) using selections product prices (the sum of the simple products prices).
In the same class in function, the parent product price is recalculated because the bundle quote item is marked as isChildrenCalculated:

Mage_Tax_Model_Sales_Total_Quote_Subtotal::collect()

if ($item->getHasChildren() && $item->isChildrenCalculated()) {
            foreach ($item->getChildren() as $child) {
                $this->_processItem($child, $addressRequest);
            }
            $this->_recalculateParent($item);

Resolving the custom option price is related to:

Mage_Sales_Model_Quote_Item_Abstract::isChildrenCalculated

Now I'm not sure if I should mess with 'isChildrenCalculated' because there are other parts of Magento code which depend on it.

EDIT 2:

A workaround would be to add custom options prices to one of the simple products (of a selection) using the event 'sales_quote_product_add_after' like they do here:
https://stackoverflow.com/questions/5104482/programmatically-add-product-to-cart-with-price-change

Best Answer

In answer to your question of: 'where is the sum of custom option price and simple product price first time calculated or/and stored in quote item.'

Static Code Trace for the locations where it is added. With a plausible path for adding the item and calculating the price.

Actually Adding an Item to the Cart:

SEE Mage_Catalog_Model_Product_Type_Abstract::_prepareOptions
AND Mage_Catalog_Model_Product::prepareCustomOptions
AND Mage_Catalog_Model_Product::addCustomOption
Called By: Mage_Catalog_Model_Product_Type_Abstract::_prepareProduct
Called By: Mage_Catalog_Model_Product_Type_Abstract::prepareForCartAdvanced
Called By: Mage_Sales_Model_Quote::addProductAdvanced
Called By: Mage_Sales_Model_Quote::addProduct
Called By: Mage_Checkout_Model_Cart::addProduct
Called By: Mage_Checkout_CartController::addAction

Calculating the Price:

Mage_Catalog_Model_Product_Type_Price:_applyOptionsPrice
Called By: Mage_Catalog_Model_Product_Type_Price::getFinalPrice 

Further look at Mage_Catalog_Model_Product_Option_Group_Abstract::getOptionPrice
Called by Mage_Catalog_Model_Product_Option::groupFactory
Called by Mage_Catalog_Model_Product_Type_Price::_applyOptionsPrice