2

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?

1 Answer 1

1

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.
2
  • aha! $item->getProduct()->getCost() worked, did not know cost didn't get passed over to the order, thank you!
    – AJK
    Commented Oct 6, 2016 at 17:54
  • 1
    It should be stored against the order item separately because the cost of product (item) could change when you are pulling this data. that's the reason the data of an order related to pricing, name and all that info should be stored against order item at the time of placing order.
    – Dharam
    Commented Aug 18, 2021 at 15:22

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.