4

I need to modify the price of a specific custom option set to a product when the user adds the product to the cart.

Business logic requires that prices be stored with 5 decimals of precision. Since this is not possible in Magento without significant core and database changes (see: Pricing precision beyond 4 digits ), I am exploring the use of a text field attribute (as this is only way to store values such as 0.02992) that will modify the price during add to cart (via observer on sales_quote_add_item event).

Typical use of an observer like this would probably be similar to: https://stackoverflow.com/questions/5104482/programmatically-add-product-to-cart-with-price-change

This is great if I want to simply change the price of the simple product, but I require changing the price of one the custom options instead, and leaving the rest of the price calculation alone.

From what I can tell, $observer->getEvent()->getQuoteItem() only includes the custom options' ids and values which leads me to believe that custom option prices are then later grabbed from another resource.

I feel like there is probably a more appropriate event to observe where the custom options' price is accessed, but am not finding much help.

How can I change the price of a single specific custom option through an observer when adding a product to the cart?

3 Answers 3

2

I was unable to find a suitable solution.

The cleanest method seemed to be simply overwriting Mage_Core_Model_Catalog_Product_Option_Value::getPrice() where I can check to see if my attribute is set and then use that value for the price before returning the method early.

Ultimately I decided to go in a completely different direction. I'll leave this open for anyone who might find a better solution down the road.

2
  • This answer isn't completely satisfactioning. I also tried the approach by manipulating the output of Mage_Core_Model_Catalog_Product_Option_Value::getPrice(). This works from the product detail page and adding the product to the cart. But on the cart itself, when you change the product there, the function is no longer called to get the price of the selected option. Jul 6, 2015 at 13:39
  • I stand corrected: this is the correct solution. I had a $this->getProduct()-method in my overwritten function. This works on the product detail page, but not in the cart. If you need a reference to the product in Mage_Core_Model_Catalog_Product_Option_Value::getPrice(), use $product = Mage::getModel('catalog/product')->load($this->getOption()->getProductId()); instead. Jul 6, 2015 at 13:55
0

I did some research and will share my results:

From what I can tell, $observer->getEvent()->getQuoteItem() only includes the custom options' ids and values which leads me to believe that custom option prices are then later grabbed from another resource.

Yes, the custom option prices are loaded later, in Mage_Catalog_Model_Product_Type_Price::_applyOptionsPrice(), which is called within Mage_Catalog_Model_Product_Type_Price::getFinalPrice(). Unfortunately that's right after catalog_product_get_final_price has been dispatched, which is usually the best event to modify prices dynamically. Also it does not save the single custom option prices anywhere, just adds them to the final price.

And getFinalPrice() is not called before the totals are collected, so after all "add item to cart" related events have been triggered.

You could observe checkout_cart_add_product_complete and checkout_cart_update_item_complete and recalculate all the totals, but this will be complicated and error prone, so I guess your solution to override getPrice() is still the best one.

0

I could not find an observer to modify a custom option price. I needed to access the custom option and the custom option values.

I override:

Mage_Catalog_Model_Product_Option_Type_Select::getOptionPrice()

This calls _getChargableOptionPrice() which gets it's value from getPrice().

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.