24

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?

21

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;
}
  • 1
    Nicely done!!!. – philwinkle May 16 '13 at 21:15
  • 6
    A warning for future googlers: manually delete your cache folder before testing this. I spent many hours testing many variations on this answer, clearing indexes and caches via backend. Whatever I did, custom attributes did not get saved to db, changing the quote_item's title however worked fine. Untill I cleared the cache manually by deleting /var/cache... Crazy! – Maurice May 26 '14 at 16:06
  • I want to this custom attribute in backend panel as well next to sku in sales order detail page. Kindly suggest , how to acheive ? – inrsaurabh Jun 17 '17 at 7:01
21

This is done by combining your knowledge of observers and config.xml. Config.xml will manage the provision of a custom attribute definition on the quote item, and an observer will handle saving the product attribute to the quote when added to a quote.

From there, you use config.xml to call the fieldset definition, which will handle the conversion from quote_item to order_item.

Full disclosure: The below content is from Atwix. Link below the answer.

At first, you should add custom attribute to sales->quote->item->product_attributes node:

<sales>
    <quote>
        <item>
            <product_attributes>
                <custom_attribute />
            </product_attributes>
        </item>
    </quote>
</sales>

This makes attribute accessible when we will be copying it from the product to quote item – which is our next step. For this task the observer is used, and the event will be called sales_quote_item_set_product:

<sales_quote_item_set_product>
    <observers>
        <yourmodule_customattribute>
            <class>yourmodule_customattribute/observer</class>
            <method>salesQuoteItemSetCustomAttribute</method>
        </yourmodule_customattribute>
    </observers>
</sales_quote_item_set_product>

Observer:

public function salesQuoteItemSetCustomAttribute($observer)
{
    $quoteItem = $observer->getQuoteItem();
    $product = $observer->getProduct();
    $quoteItem->setCustomAttribute($product->getCustomAttribute());
}

The last thing we need to care about – it is converting attribute from quote_item to order_item. And this can be done with XML:

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

Source: Atwix (who is full of win): http://www.atwix.com/magento/custom-product-attribute-quote-order-item/

  • 2
    +1. I feel like I +1'd myself. When I was about to submit something veeeery similar, a message appeared: 'this question has a new answer'. so +1 because you type fast :) – Marius May 16 '13 at 21:15
  • Hahaha - it's Atwix, really. – philwinkle May 16 '13 at 21:16
  • @philwinkle, The above works well but assume your custom attribute is product points depending on qty. If prod A has 50 points and 2 qty, then total points should be 50*2=100. How do I set value 100 instead of 50 in quote item ? If the attribute value gets updated, how do I save that updated value and not the one set from admin – Slimshadddyyy Jan 9 '16 at 11:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.