2

I want to get the color and size of ordered product on sales_order_save_after

i have write this code

$product = Mage::getModel('catalog/product')->load($item->getProductId());
$color = $product->getAttributeText('color');

config.xml event

<events>
    <sales_order_save_after> 
        <observers>
            <Piyush_orderexport_order_save_after>
                <type>singleton</type>
                <class>Piyush_OrderExport_Model_Observer</class>
                <method>exportOrder</method>
            </Piyush_orderexport_order_save_after>
        </observers>
    </sales_order_save_after>
</events>

but no value found

1
  • for simple product or configure product Dec 21, 2017 at 11:22

1 Answer 1

3

Assuming that color and size are product attributes.

to get product attribute you can use below code

$_product = Mage::getModel('catalog/product')->load($item->getProductId());
$size = $_product->getResource()->getAttribute('size')->getFrontend()->getValue($_product);
$color =  $_product->getResource()->getAttribute('color')->getFrontend()->getValue($_product);

for config product use below code

$order = $observer->getEvent()->getOrder();

foreach( $order->getAllVisibleItems() as $item ) {
    $options = $item->getProductOptions();
    $options = $options['attributes_info'];
}

it will give you all product option used for order and option value

output will look

Array
(
    [0] => Array
        (
            [label] => Color
            [value] => White
        )

    [1] => Array
        (
            [label] => Size
            [value] => XS
        )

)
9
  • above code giving value <Item_Color>No</Item_Color>
    – Piyush
    Dec 21, 2017 at 11:42
  • if product do not have assign any attribute value it will return "No" Dec 21, 2017 at 11:53
  • how can I check that attribute are assign or not ?
    – Piyush
    Dec 21, 2017 at 12:11
  • open product in admin and check that product do have assign attribute value Dec 21, 2017 at 12:14
  • I checked it and the attribute value is assigned
    – Piyush
    Dec 21, 2017 at 12:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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