I'm trying to add product's custom option to cart programmatically using event
controller_action_predispatch_checkout_cart_add
In my observer function I'm not able to to get product object using following methods
$observer->getProduct()->getId();
or
$observer->getEvent()->getProduct()->getId();
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
$product->addOption(array(
'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.
Questions:
- 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();
- In my above code, how I can add custom options to cart?