PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, August 23, 2022

[FIXED] How to get row total of each product in an order on Magento 2?

 August 23, 2022     magento, magento2, php     No comments   

Issue

I'm trying to get the subtotal of each row/line item in the success page but I'm not sure what to use.
I've tried getRowTotal() and getRowTotalInclTax() but both are showing up blank.
I'm able to get the whole cart subtotal but want I need is the individual product subtotal.

Here's part of the code I'm using:

// Get order details
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($block->getOrderId());

// Get Each Product Details
$items = $order->getAllItems();

foreach($items as $i) {
  $product = $objectManager->create('Magento\Catalog\Model\Product')->load($i->getProductId());

  echo $product->getName();
  echo $product->getSku();
  echo $product->getRowTotal(); // This is where its just coming back blank/null
}

Solution

There you are loading the product entity: The product entity cannot contain the information of the order.
You must use the Order Item entity that you already use to have the total row

Something like this :

// Get order details
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create('Magento\Sales\Model\Order')->loadByIncrementId($block->getOrderId());

// Get Each Product Details
$items = $order->getAllItems();

foreach($items as $i) {
  echo $i->getProduct()->getName();
  echo $i->getProduct()->getSku();
  echo $i->getRowTotal(); 
}


Answered By - Walid Fitouri
Answer Checked By - Pedro (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing