Asif hhh
Asif hhh

Reputation: 1552

add product to cart with custom options

I have to create a (virtual, simple )product and then add to cart both programmatically, i have done this so far. now i have to set custom options when this product add to cart. but nothing happens . here is my code

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

    $cart = Mage::getModel('checkout/cart');
    $cart->init();


    $params = array(
        'product' => $product->getId(), // This would be $product->getId()
        'qty' => 1,
        'options' => array(
            34 => "value",
            35 => "other value",
            53 => "some other value"
        )
    );      



    try {   
        $cart->addProduct($product, new Varien_Object($params));
        Mage::getSingleton('checkout/session')->setCartWasUpdated(true);
        $cart->save();
    }
    catch (Exception $ex) {
        echo $ex->getMessage();
    }

Upvotes: 4

Views: 16180

Answers (2)

Rodrigo Siqueira
Rodrigo Siqueira

Reputation: 1

I was having a trouble with date custom option field, for add product by my controller function.

So I must to break/explode on the Magento way, and put it on params to AddProduct(), follow as below.

    try {
        $cart = Mage::getModel('checkout/cart');
        $previousItemCount = $cart->getQuote()->getItemsCount();

        if ($previousItemCount <= 0) {
            $cart->init();
        }

        $params = $this->getRequest()->getParams();
        $product = Mage::getModel('catalog/product')->load($params['product_id']);

        $date = explode('/', $params['product_dtinvoice']);
        $date = array(
            'month' => $date[0],
            'day' => $date[1],
            'year' => $date[2],
        );

        $cart->addProduct(
            $product,
            new Varien_Object(array(
                'product' => $product->getId(),
                'qty' => 1,
                'options' => array(
                    '4' => array(
                        'month' => $date['month'],
                        'day' => $date['day'],
                        'year' => $date['year']
                    ),
                    '2' => $params['product_ean'],
                    '3' => $params['product_serialnumber'],
                    '1' => $params['product_seller'],
                ),
            ))
        );

        $cart->save();

        if ($previousItemCount < $cart->getQuote()->getItemsCount()) {
            $return = array('result' => true, 'msg' => '');
        } else {
            $return = array('result' => false, 'msg' => 'Did not possible to add this product to cart. Please contact the administrator');
        }

        $this->getResponse()->setBody(Mage::helper('core')->jsonEncode($return));
    } catch(Exception $e) {
        Mage::throwException($e->getMessage());
    }

Upvotes: 0

Asif hhh
Asif hhh

Reputation: 1552

Here is the code i was come up with success.

$a_options = array(
    'options' => array(
         'label' => 'Choice',
         'value' => $pkg_selected_products,
    )
);

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

$quote->addItem($quoteItem);
$quote->save();

Upvotes: 6

Related Questions