Magento – add custom Order Item attribute and Quote Item attribute

custom-attributesmagento-1.9ordersquoteitemsales-order

I need to add custom order item attribute and quote item attribute at when product add to cart. Attribute value store in quote item table but not in order item table.

config.xml

<?xml version="1.0"?>
<config>
  <modules>
    <DD_Customoption>
      <version>0.1.0</version>
    </DD_Customoption>
  </modules>
  <global>
      <fieldsets>
             <sales_convert_quote>
                   <result_image>
                       <to_order_item>*</to_order_item>
                    </result_image>
            </sales_convert_quote>               
       </fieldsets>
    <helpers>
      <customoption>
        <class>DD_Customoption_Helper</class>
      </customoption>
    </helpers>
    <models>
      <customoption>
        <class>DD_Customoption_Model</class>
        <resourceModel>customoption_mysql4</resourceModel>
      </customoption>
    </models>
    <events>
      <controller_action_predispatch_checkout_cart_add> <!-- identifier of the event we want to catch -->
        <observers>
          <controller_action_predispatch_checkout_cart_add_handler> <!-- identifier of the event handler -->
            <type>model</type> <!-- class method call type; valid are model, object and singleton -->
            <class>customoption/observer</class> <!-- observers class alias -->
            <method>updateOptions</method>  <!-- observer's method to be called -->
            <args></args> <!-- additional arguments passed to observer -->
          </controller_action_predispatch_checkout_cart_add_handler>
        </observers>
      </controller_action_predispatch_checkout_cart_add>
    </events>
  </global>
</config>  

Observer.php

public function updateOptions(Varien_Event_Observer $observer) {

        $params = Mage::app()->getRequest()->getParams();


        Mage::getSingleton('core/session')->setEditValue(serialize($params));

        $headStoneValue = Mage::getSingleton('core/session')->getImageDivValue();
        $html = $params['result_image'];

        $headStoneValue = Mage::getSingleton('core/session')->getHeadStoneValue();
        $headStone = unserialize($headStoneValue);
        $quoteItem = Mage::getModel('sales/quote_item');
        $quote = Mage::getSingleton('checkout/cart')->getQuote();
        $product_id = $headStone['productId'];
        $stoneType = $headStone['stoneType'];



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


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


        $options = array();

        if (!empty($headStone['stoneType'])) {
            $stoneType = $headStone['stoneType'];
            $options[7] = $stoneType;
        }

        if (!empty($headStone['stoneSize'])) {
            $stoneSize = $headStone['stoneSize'];
            $options[6] = $stoneSize;
        }


        if (!empty($headStone['pannelName'])) {
            $pannelName = $headStone['pannelName'];
            $options[3] = $pannelName;
        }

        if (!empty($headStone['colorName'])) {
            $colorName = $headStone['colorName'];
            $options[5] = $colorName;
        }


        if (!empty($params['name'])) {
            $name = $params['name'];
            $options[11] = $name;
        }

        if (!empty($params['bod'])) {
            $bod = $params['bod'];
            $options[10] = $bod;
        }

        if (!empty($params['expire-date'])) {
            $expire = $params['expire-date'];
            $options[9] = $expire;
        }

        if (!empty($params['result-about'])) {
            $aboutResult = $params['result-about'];
            $options[8] = $aboutResult;
        }


        $cart->addProduct($productCollection, array('product_id' => $product_id,
            'qty' => 1,
            'options' => $options
        ));
        $cart->save();

        $items = $quote->getAllVisibleItems();



        foreach ($items as $item) {

            $product = $item->getProduct();
            $productIds = $product->getEntityId();
            if ($productIds == $product_id) {
                $item->setCustomPrice($params['finalPrice']);
                $item->setOriginalCustomPrice($params['finalPrice']);
                $item->setResultImage($html);
                $item->getProduct()->setIsSuperMode(true);
                $item->save();
            }
        }


        /* add Result data */

        $resultModel = Mage::getModel('headstone/stoneresult');



    }

Best Answer

In order to save some additional details in quote items table, you need to first create a new column in sales_flat_quote_item table. Then only your save action will work.

Magento Sales Quotes and Sales Orders are not EAV entities. They are flat entities.