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 – Vaibhav Ahalpara Dec 21 '17 at 11:22
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 '17 at 11:42
  • if product do not have assign any attribute value it will return "No" – Vaibhav Ahalpara Dec 21 '17 at 11:53
  • how can I check that attribute are assign or not ? – Piyush Dec 21 '17 at 12:11
  • open product in admin and check that product do have assign attribute value – Vaibhav Ahalpara Dec 21 '17 at 12:14
  • I checked it and the attribute value is assigned – Piyush Dec 21 '17 at 12:29

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.