4

I'm trying to create a controller to add to cart a product programmatically in magento 1.9, like this:

<?php
class Namespace_Module_IndexController extends Mage_Core_Controller_Front_Action{

    public function indexAction(){
    try {
      $productId = '146';

      $options = array('92'=>'49','144'=>'21');
      $insertData = array(
        'product_id' => $productId,
        'qty' => '1',
        'options' => $options
      );
      $cart = Mage::getSingleton('checkout/session')->getQuote();
      $product = Mage::getModel('catalog/product')->load($productId);
      $cart->addProduct($product, $insertData);
      $cart->save();
      Mage::getSingleton('customer/session')->setCartWasUpdated(true); 
    } catch (Exception $e) {
      print_r($e->getMessage());
    }
    }
}

but when I check the cart, it's still empty and I got an error like this:

Invalid request for adding the product to quote.
6
$cart = Mage::getSingleton('checkout/cart');
$cart->init();
$options = array('92'=>'49','144'=>'21');
$product = Mage::getModel('catalog/product')->load(146);

$paramater = array('product' => '146',
                    'qty' => '1',
                    'form_key' => Mage::getSingleton('core/session')->getFormKey(),
                    'options' => $options 
            );       

$request = new Varien_Object();
$request->setData($paramater);
$cart->addProduct($product, $request);
$cart->save();
  • i got this error: Please specify the product's option(s). – Shell Suite May 8 '17 at 7:35
  • 'options' => array('option_id'=>'sub_option_id') replace with $options = array('92'=>'49','144'=>'21'); – Sunil Patel May 8 '17 at 7:36
  • i've done that, the 92 is code for color, and 144 is for size – Shell Suite May 8 '17 at 7:39
  • @ShellSuite check my edited code – Sunil Patel May 8 '17 at 7:40
  • still got the same error – Shell Suite May 8 '17 at 7:42
0
$params = new Varien_Object(['product'=> $product->getId(),
        'super_attribute' => array('92'=>'49','144'=>'21'),
        'qty' => $item->getQty(),
        ]);
$quote->addProduct($product, $params)->save();
0

Try this:

    $product_id = 1;
    $product = Mage::getModel('catalog/product')->load($product_id);
    $cart = Mage::getModel('checkout/cart');
    $cart->init();
    $options = array(4 => 3, 5 => 1);
    $params = array(
        'product_id' => $product_id,
        'qty' => 1,
        'options' => $options
    );

    $cart->addProduct($product, $params);
    $cart->save();
    Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
0
<?php
class Namespace_Module_IndexController extends Mage_Core_Controller_Front_Action{

    public function indexAction(){
    try {
      $productId = '146';

      $options = array('92'=>'49','144'=>'21');
      $insertData = array(
        'product_id' => $productId,
        'qty' => '1',
        'options' => $options
      );
      $cart = Mage::getSingleton('checkout/session');
      $product = Mage::getModel('catalog/product')->load($productId);
      $cart->addProduct($product, $insertData);
      $cart->save();
      Mage::getSingleton('customer/session')->setCartWasUpdated(true); 
    } catch (Exception $e) {
      print_r($e->getMessage());
    }
    }
}

Remove getQuote() after Mage::getSingleton('checkout/session') . then try. your error is removed

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.