How to Get Product Cost from Order Items in Magento

magento-1.8PHPsales-order

I'm trying to get the cost of each product on an order summed together into a single value, not sure what I'm doing wrong but it's not returning any values.

I should note that I'm able to pull other item details from the order such as prices, skus, etc.

// I'm passing $order info via observer

$itemCollection = $order->getItemsCollection();

foreach($itemCollection as $item) {             
    // Get Product Costs
    $productCost += $item->getCost();
}

$productCost is returning null, any ideas why?

Best Answer

How to get Product Cost from Order Items

You can't.

Costs are not copied over to the order item. So you have two options:

  1. Copy it over to the order item, there are plenty of explanations how to copy a product attribute into the order item, on MageOverflow too!
  2. You just grab it from the product: $item->getProduct()->getCost(), downside is, if the product is deleted, it doesn't work anymore

More reasons to copy it over (thanks to @Dharam!)

  • if the value changes over time you get the current value, not the one of the past, when the order was made.