Magento – Magento : Add Info Buy Request Option to Quote Item

magento-1.9quotequoteitem

I load quote by quote id and adding items to quote_item table.

I need to know how to add product options i.e. info_buyrequest options to quote item from controller only. I tried below code.

    $quote = Mage::getModel('sales/quote')->load('1125');
    $quote = $quote->setUpdatedAt(now());

    $product = Mage::getModel('catalog/product')->load($product_id);
    $quoteItem = Mage::getModel('sales/quote_item')->setProduct($product)->setQty(1);
    $quote->addItem($quoteItem);

    $additionalOptions = array(
            'label' => 'shop_data',
            'value' => 1
        );

     /* Here the modification is needed */
    foreach($quote->getAllVisibleItems() as $row){
        if($row->getProductId() == 95 ){
            $row->getProduct()->addCustomOption('info_buyRequest', serialize($additionalOptions));
        }
    }

    try{
        $quote->save();
    }catch(Exception $e){
        echo $e->getMessage();
    }

Best Answer

please try belo

<?php 
    $quote = Mage::getModel('sales/quote')->load('1125');
    $quote = $quote->setUpdatedAt(now());

    $product = Mage::getModel('catalog/product')->load($product_id);
    $quoteItem = Mage::getModel('sales/quote_item')->setProduct($product)->setQty(1);
    $quote->addItem($quoteItem);

    $additionalOptions = array(
            'label' => 'shop_data',
            'value' => 1
        );

     /* Here the modification is needed */
    foreach($quote->getAllVisibleItems() as $row){
        if($row->getProductId() == 95 ){
            $row->addOption(array(
                'code' => 'info_buyRequest',
                'value' => serialize($additionalOptions)
            ));
        }
    }

    try{
        $quote->save();
    }catch(Exception $e){
        echo $e->getMessage();
    }
Related Topic