1

In my magento store, I have a script to add product programmatically in cart using below code :

$cart = Mage::getSingleton('checkout/cart');
$cart->init();
$_product = Mage::getModel('catalog/product')->load($id);
$params = array(    
        'product' => $_product->getId(),
        'qty' => $qty,
        'options' =>'',
        'form_key' => Mage::getSingleton('core/session')->getFormKey()              
    );

$cart->addProduct($_product, $params);
$cart->save();

For this item checkout_cart_add_product_complete event not fired.

However the checkout_cart_add_product_complete event is working nice while adding a product to cart from a catalog.

Is there any other event in magento which fire on programmatically add product (similar to checkout_cart_add_product_complete)?

3 Answers 3

0

You can dispatch your custom event after you code.

$cart = Mage::getSingleton('checkout/cart');
$cart->init();
$_product = Mage::getModel('catalog/product')->load($id);
$params = array(    
    'product' => $_product->getId(),
    'qty' => $qty,
    'options' =>'',
    'form_key' => Mage::getSingleton('core/session')->getFormKey()              
);

$cart->addProduct($_product, $params);
$cart->save();
/***My Custom Event***/    
    Mage::dispatchEvent('custom_cart_add_product_event',array('product' => $_product));
/***My Custom Event***/ 

Now just like magento event you can use that.

0

If you check app\code\core\Mage\Checkout\controllers\CartController.php::addAction, you will see that the event is fired using below code,

Mage::dispatchEvent('checkout_cart_add_product_complete',
                array('product' => $product, 'request' => $this->getRequest(), 'response' => $this->getResponse())
);

So, to call the event, you have to use the same code in your function after you save cart.

0

Replace your code with this :

$cart = Mage::getSingleton('checkout/cart');
$cart->init();
$_product = Mage::getModel('catalog/product')->load($id);
$params = array(    
        'product' => $_product->getId(),
        'qty' => $qty,
        'options' =>'',
        'form_key' => Mage::getSingleton('core/session')->getFormKey()              
    );

$cart->addProduct($_product, $params);
$cart->save();

There was a issue in your 3rd line:

1
  • That was a typing mistake, product added in cart successfully with my script, my question is checkout_cart_add_product_complete event not fired. Commented Feb 1, 2017 at 13:40

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.