1

I am trying to add a product to the cart programmatically with some custom options. The item gets added to the cart correctly but none of the options ever get added. My code is:

require_once '../../app/Mage.php';
umask(0);
/* not Mage::run(); */
Mage::app('default');

Mage::getSingleton("core/session", array("name" => "frontend"));

$product_id = 2364;
$id_opt_value = 6072;
$final_opt_value = 6074;

$product = Mage::getModel('catalog/product')->load($product_id);
$cart = Mage::getModel('checkout/cart');
$cart->init();
$params = array(
    'product' => $product_id,
    'qty'     => 1,
    'options' => array(         
        $id_opt_value => '123456',
        $final_opt_value => 'black gloss finish',
     )
);

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

I have double checked and the option values are correct. I am using magento ce-1.9.0.0

odd_duck
  • 3,941
  • 7
  • 43
  • 85
  • I see you've not updated the session, it might be that `$session->setCartWasUpdated(true);' as per this stack http://stackoverflow.com/questions/21867045/magento-1-8-add-product-to-cart-using-php – McNab Nov 17 '14 at 14:46
  • just tried updating the session and still no luck – odd_duck Nov 17 '14 at 14:55
  • Check out below link, may be you're facing similar issue. http://stackoverflow.com/questions/23838558/how-to-programmatically-add-a-product-with-custom-options-to-cart-in-magento – Vivek Nov 17 '14 at 19:03
  • Seems similar to https://stackoverflow.com/questions/21867045/ as well. – Wilbert van de Ridder Jan 16 '15 at 08:45

1 Answers1

0

Try to use Varien Object:

$request = new Varien_Object();
$request->setData($param);
$cart->addProduct($productInstance, $request);

Should work as well.

Serge
  • 417
  • 2
  • 12
  • just tried like so but still no joy : http://paste.ofcode.org/r7r32q6L94tceT8KMJ2bPE – odd_duck Nov 17 '14 at 14:56
  • Have you tried to update cart: before - $session = Mage::getSingleton('customer/session'); and after: $session->setCartWasUpdated(true); – Serge Nov 17 '14 at 16:57