Magento – Get Configurable Product Price from Simple Product in Order

configurable-productevent-observersimple-product

We are writing synchronization of Magento and MAS90 ERP system. We are transferring Orders from Magento to ERP system. We have added observer for order_save_after.

Each of Magento Simple product has UPC code that needed to add item in ERP system.
Simple products are automatically added to Magento Order based on configurable item, but problem is that simple product's real price, quantity, tax amount…are stored in configurable product, but UPC is stored in simple product.
For resolving this problem, we decide to get simple products from Magento Order ( to get right UPC ).

Our question is, how to get RIGHT price, quantity, tax amount of automatically added simple product. ( How in Magento order are linked configurable product and automatically added simple product. )

Here is snippet of Code that is getting products.

    foreach ($order->getAllItems() as $orderItem){
        $tmpMAS90OrderItem = NULL; 
        if($orderItem->getProductType() == 'simple'){
            $tmpMAS90OrderItem = $orderItem -> getUpc();
        }   
        if($tmpMAS90OrderItem != NULL) { 
            $setChildValuesArray = array(
                'ItemCode'            => $tmpMAS90OrderItem,
                'UnitPrice'           => $orderItem -> getPrice(), // THIS IS WRONG PRICE, right price is in configurable item
                'QuantityOrdered'     => $orderItem -> getQtyToInvoice() 
            );
            $querySO->setChildFieldValues($setChildValuesArray,$childSequence); //child sequence ( line sequence )
            $childSequence++;
             $linesTaxAmountSum += $orderItem->getTaxAmount(); // THIS IS WRONG TAX AMOUNT, right amount is in configurable item
        }
    }

Best Answer

In this case you need to use call parent item's row().As per magento when a configurable product is ordered,then there are two rows has been saved at sales_flat_order_item.One is configurable product data product_id and order item prices and another is simple products details.So need data fetch data from Configurable products.

So need to change

'UnitPrice'=> $orderItem->getParentItem()?$orderItem->getParentItem()-> getPrice():$orderItem-> getPrice(),

From

UnitPrice'           => $orderItem -> getPrice(),

Also To:

$linesTaxAmountSum += $orderItem->getParentItem()?$orderItem->getParentItem()->getTaxAmount():$orderItem->getTaxAmount();

From:

$linesTaxAmountSum += $orderItem->getTaxAmount(); 
Related Topic