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;
    }
}
  • Can you please share the code of how you are loading $order? – Dhara Bhatti Feb 15 at 12:22
  • Please, check my updated code. – bikash.kaushik Feb 15 at 12:37
  • You need to use $item->getPrice() method to get actual price. – Dhara Bhatti Feb 15 at 12:41
  • 1
    I got it by using $item->getOriginalPrice() method. – bikash.kaushik Feb 18 at 6:47
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.

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.