How-to-get-the-items-data-from-the-order-into-the-Magento-2-success-page

How to Get Items from the Order into the Magento 2 Success Page?

Collecting all the items from order into the success page is not a big deal if you know the right process out of it. In today’s tutorial, we will help you with the options to fetch various types of information from order into the success page. Let’s begin with the tutorial:

In order to fetch order increment ID, follow the below snippet:

$this->orderId = $lastOrder->getIncrementId();

But while loading the order object, you are using order entity ID in place of increment ID so the line should be changed as:

$order = $this->_orderFactory->create()->load($this->orderId);
With order increment id
$order = $this->_orderFactory->create()->loadByIncrementId($this->orderId);
In getvisibleitems

Follow the below-mentioned code to get order details on your Success Page:

<?php
$lid = $this->getOrderId();
echo  “Order ID:”.$lid.”<br/>”;
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$order = $objectManager->create(‘Magento\Sales\Model\Order’)->load($lid);
$totall = $order->getGrandTotal();
echo “Order Total:”.$totall.”<br/>”;
$shippingAddress = $order->getShippingAddress();
echo “Telephone No:”.$shippingAddress->getTelephone().”<br/>”;
echo “postcode”.$shippingAddress->getPostcode().”<br/>”;
$items = $order->getAllItems();
foreach($items as $i):
$_product =
$objectManager->create(‘Magento\Catalog\Model\Product’)->load($i->getProductId())->getSku();
echo “product sku:”.$_product.”<br/>”;
endforeach;
?>

It is recommended to avoid using the object manager directly, so prefer building a block and then sending details from there. Keep reading our knowledge base to learn more about the functioning of Magento 2 and manage your eCommerce store better. If there is any query, feel free to reach us out!

Tags