I'm trying to add product's custom option to cart programmatically using event


In my observer function I'm not able to to get product object using following methods




Though I'm getting product Id using

$obsPId = Mage::app()->getRequest()->getParam('product')

Check Details here.

Now I've created product object using

$product = Mage::getSingleton('catalog/product')->load($obsPId);

I've created product's additional custom option in array

$additionalOptions = array(
                        'label' => $sareeLabel,
                        'value' => $sareeOptionId,

Now issue is here: Code being used to add additional option to product

                        'code' => 'additional_options',
                        'value' => serialize($additionalOptions),

Error: Fatal error: Call to a member function getId() on a non-object in Magento's core model Product.php.

I've also tried using observer $observer->getProduct() ->addCustomOption('additional_options', serialize($additionalOptions));

but it gives Fatal error: Call to a member function addCustomOption() on a non-object in my observer's function.


  1. Is it normal in Magento that below observer functions won't work on controller_action_predispatch_checkout_cart_add event? $observer->getProduct()->getId(); $observer->getEvent()->getProduct()->getId();
  2. In my above code, how I can add custom options to cart?

1 Answer 1


The event is called before the action Mage_Checkout_CartController::addAction() which calls $this->_initProduct() that does the load of the product.

Better set the request parameters with your custom options with ... ->getRequest()->setParam(name, value) in your observer function. See the parameters by adding a product in cart, log all ...->getRequest()->getParams() in logs and see the key names of the array with custom options.

But you'll need ids of the custom options which implies at most a product load in your observer. A better implementation would be to fetch only the custom option ids and labels, not the entire prouduct (+100 SQL queries).

Your Answer

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

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