1

In Magento 2, how can I get order item actual price? I tried using below way but it returns 0, although the code is working with Magento 1.9

use Magento\Framework\Event\ObserverInterface;
class SendOrderTxn implements ObserverInterface
{
    public function execute(
        \Magento\Framework\Event\Observer $observer
    ) {
        $order = $observer->getEvent()->getOrder();
        foreach ($order->getAllVisibleItems() as $item) {
            $orgamount = $item->getProduct()->getPrice();
            /* here $orgamount is 0 */
        }

        return $this;
    }
}
4
  • Can you please share the code of how you are loading $order? Feb 15, 2019 at 12:22
  • Please, check my updated code. Feb 15, 2019 at 12:37
  • You need to use $item->getPrice() method to get actual price. Feb 15, 2019 at 12:41
  • 1
    I got it by using $item->getOriginalPrice() method. Feb 18, 2019 at 6:47

1 Answer 1

1

Try following code.

<?php
$orderid = 2;

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Api\Data\OrderInterface')->load($orderid);

//Loop through each item and fetch data
foreach ($order->getAllItems() as $item)
{
   //fetch whole item information
   print_r($item->getData());

   //Or fetch specific item information
   echo $item->getId();
   echo $item->getProductType();
   echo $item->getQtyOrdered();
   echo $item->getPrice(); 

}
?>

I hope this will help you.

1
  • This does not work on giftcard product.
    – devhs
    May 27, 2021 at 12:08

Your Answer

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

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