Magento – Add product to cart with custom option using PHP

cartce-1.9.0.0option

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

Best Answer

Creating a quote may help instead of using Checkout/Cart Model.

$quote = Mage::getModel('sales/quote')
            ->setStoreId(Mage::app()->getStore('default')->getId());

set your products $params here.

force the Varien_Object with your parameters to addProduct

$quote->addProduct($product, new Varien_Object($params));
$quote->save();