Magento 1.9 – Save Quote Custom Data and Read It Again

custom-optionsmagento-1.9ordersquote

I store my custom data in my ajax CartController issuing:

foreach ( $quote->getAllItems() as $item ) {
    /* @var $item Mage_Sales_Model_Quote_Item */
    $item->setMyCustomData( 'OK' );
    $item->setOriginalCustomPrice( 777 );
    $item->save();
}

and I want to read these data again in my Order/Pdf/Invoice.php:

foreach ($invoice->getAllItems() as $item){
    Mage::log($item->getOrderItem()->getData()); // Here I wish to see my data set previously
}

The problem is I can only see the price update, but I can not see my MyCustomData field. So I suppose that my custom data stored just as protected _data array don't transfer from quoteItem to orderItem.

How to make it work? Should I rather use some another way to set my custom data?
I thought about product options way, but I don't want to affect all the products, only particular ones.

Thanks.

Best Answer

If you attributes are set up correctly on both the order_item and the quote_item then you maybe missing the xml that copies the attribute value when you save and order.

<fieldsets>
    <sales_convert_quote_item>
        <your_attribute_code>
            <to_order_item>*</to_order_item>
        </your_attribute_code>
    </sales_convert_quote_item>
    <sales_convert_order_item>
        <your_attribute_code>
            <to_quote_item>*</to_quote_item>
        </your_attribute_code>
    </sales_convert_order_item>
</fieldsets>

This will copy the attribute value from quote item to order item when saving the order and also from order item to quote item when recreating a quote from an order via the admin or customer section.

Related Topic