简体   繁体   中英

Add to cart with product custom option in Magento

I have a virtual product with custom option (type-1 and type-2). Price may change depend on type. How can I add to cart with this option directly and change the price depend on custom option. I tried this code but it doesn't work.

<?php
// the ID of the product
$product_id  = "123";

$product     = Mage::getModel('catalog/product')->load($product_id);

$cart = Mage::getModel('checkout/cart');
$cart->init();
$params = array(
    'product' => $product_id,
    'qty' => 1,
    'options' => array(
        'options' => array(
            '7462' => 'Type Option Id' ,
            '3731' => 'Type-1',
        )

    )
);

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

Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
$this->_redirect('checkout/cart');

?>

Please try following code to add option to your product :

$quoteItem->addOption(new Varien_Object(
    array(
        'product' => $quoteItem->getProduct(),
        'code' => 'additional_options',
        'value' => serialize($a_options)
    )
));

Ref Here : add product to cart with custom options

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