2

I have used the following code to pass options onto a quote item before adding it to the shopping cart. The options show just fine up through the checkout process, but disappear once the order has been placed. The options do not show up in the confirmation email for the order, on the order review page or in the backend. I need these options to persist for later review.

$cart = Mage::getModel('checkout/cart')
$quote = Mage::getSingleton('checkout/session')->getQuote()
$product = Mage::getModel('catalog/product')->load(7)

$quoteItem=Mage::getModel('sales/quote_item')->setProduct($product)

$a_options = array(
   'options1' => array(
      'label' => 'Ingredients',
      'value' => $recipecontents,
   ),
   'options2' => array(
      'label' => 'Crush Grains',
      'value' => $crush,
   ),
);

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

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

The variables $recipecontents and $crush are defined earlier in the code using data passed from a form.

Everything else works, but as soon as the order has been placed, Magento forgets what the options had been set to. I need these values to persist into the backend in order to fill orders. I am using Community Edition 1.7.0.2.

Does anyone know why these values are being lost and how to prevent it from happening?

swiftikasu
  • 21
  • 1
  • 2
  • You can check this link: http://stackoverflow.com/questions/9412074/magento-quote-order-product-item-attribute-based-on-user-input/9496266#9496266 Pay attention on the paragraph: "Add options to order item". I assume, that your question is similar to "Magento - Quote/order product item attribute based on user input" - http://stackoverflow.com/questions/9412074/magento-quote-order-product-item-attribute-based-on-user-input – ceckoslab Nov 02 '13 at 08:24
  • 1
    I had already read that article several times without success, but after reading it again for the 10th time and consulting an article on module creation, I was able to get it working. Thanks. – swiftikasu Nov 03 '13 at 17:01

1 Answers1

1

In you config.xml

    <events>
        <sales_convert_quote_item_to_order_item>
            <observers>
                <Your_Module>
                    <type>model</type>
                    <class>Your_Module_Model_Observer</class>
                    <method>salesConvertQuoteItemToOrderItem</method>
                </Your_Module>
            </observers>
        </sales_convert_quote_item_to_order_item>
    </events>

Then in your Observer.php

<?php
public function salesConvertQuoteItemToOrderItem(Varien_Event_Observer $observer)
{
    $quoteItem = $observer->getItem();
    if ($additionalOptions = $quoteItem->getOptionByCode('additional_options')) {
        $orderItem = $observer->getOrderItem();
        $options = $orderItem->getProductOptions();
        $options['additional_options'] = unserialize($additionalOptions->getValue());
        $orderItem->setProductOptions($options);
    }
}
?>
screamzt
  • 37
  • 3