简体   繁体   中英

Magento - add product to cart with custom option using PHP

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

Try to use Varien Object:

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

Should work as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM