I am trying to Add Products to Cart with respect to customer (programmatically) but getting error "Invalid request for adding product to quote". I have both Simple products (with custom options) and configurable products. Below is my code. Please help. Many thanks in advance.

public function addtocartAction(){
    try {
        $cusId = $this->getRequest()->getParam('cusId');
        $customer = Mage::getModel('customer/customer')->load($cusId);
        $quote = Mage::getModel('sales/quote')->loadByCustomer($customer);
        $quoteId = $quote->getId();
        //$products = $this->getRequest()->getParam('products');
        $products = json_decode('[{"proId": "906","proQty": "1", "options":{"17":"wq","16":"18"}}]');

        foreach($products as $product) {
            /*if (!$product->getId()) {
                throw new Exception();
            foreach ($product->options as $optKey => $optValue) {
                $optAll[$optKey] = $optValue;

            $mainProduct = Mage::getModel('catalog/product')->load($product->proId);
            $params = array(
                'product' => $product->proId,
                'qty'     => $product->proQty,
                'options' => $optAll
            echo "<pre />"; print_r($params);
            $quote->addProduct($mainProduct, $params);
        $rslt['success'] = '1';
        $rslt['message'] = 'Product has been succefully added to cart';
    catch(Exception $e){
        $rslt['success'] = '0';
        $rslt['message'] = $e->getMessage();


$params should be in Varien_Object when you used quote object.

Try this following

$request = new Varien_Object($params);
$quoteItem = $quote->addProduct($_product, $request);

To add onto pilathraj's answer, the error

Invalid request for adding product to quote

Come from app/code/core/Mage/Sales/Model/Quote.php from around line 965

public function addProductAdvanced(Mage_Catalog_Model_Product $product, $request = null, $processMode = null)

if (!($request instanceof Varien_Object)) {
            Mage::throwException(Mage::helper('sales')->__('Invalid request for adding product to quote.'));


For the testing code that i'm using to add to the cart, this is what it looks like and what pilathraj's suggestion fixed:

$productId = 906;
$params = array(
     'product' => $productId,
     'qty' => 1,
$request = new Varien_Object($params);
$product = Mage::getModel('catalog/product')->load($productId);
$quote = Mage::getSingleton('checkout/session')->getQuote();
if (! $quote->hasProductId($productId)) {
    $quote->addProduct($product, $request);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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