Magento Product Attributes – Add to Quote Item and Order Item

attributesconfigurationproduct

What is the correct way in Magento to have a product attribute be automatically persisted to quote an item and then finally to order item?

Is it as simple as a little config XML or is it a manual process looking at before saving events and so forth?

Best Answer

one way would be to use an observer and a converter.

the observer would be to get the attribute from the product to the quote (using an attribute called 'test'), and the converter gets the attribute from the quote to the order.

in your config:

<global>
    <fieldsets>
        <sales_convert_quote_item>
            <test>
                <to_order_item>*</to_order_item>
            </test>
        </sales_convert_quote_item>
    </fieldsets>

    <sales>
        <quote>
            <item>
                <product_attributes>
                    <test />
                </product_attributes>
            </item>
        </quote>
    </sales>

    <events>
        <sales_quote_item_set_product>
            <observers>
                <YOUR_MODULE>
                    <class>YOUR_MODULE/observer</class>
                    <method>setTestAttribute</method>
                </YOUR_MODULE>
            </observers>
        </sales_quote_item_set_product>
    </events>
</global>

in your observer:

public function setTestAttribute(Varien_Event_Observer $observer) {

    $item = $observer->getQuoteItem();
    $product = $observer->getProduct();
    $item->setTest($product->getTest());
    return $this;
}