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

If you are trying to find out how to get the items from order into the Magento 2 success page. Let us learn how to use this option.

To fetch items from order into the success page

To get order Increment ID

$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

Code to get order details in your success.phtml file. It works fine in Magento 2.

<?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;

?>

Again using object manager in direct way is not recommended so just build block and send details from there and it becomes easy for you to retrieve the file.

To get order id use the following class

protected $_checkoutSession;

\Magento\Checkout\Model\Session $checkoutSession,

$this->_checkoutSession = $checkoutSession;

// Use this method to get ID

public function getRealOrderId()

{

$lastorderId = $this->_checkoutSession->getLastOrderId();

return $lastorderId;

}

// Use this in your phtml

<?php

$lid = $block->getRealOrderId();

$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;?>

Get basic Item information on your Order Success Page

$order = Mage::getSingleton(‘sales/order’)->load($orderId, ‘increment_id’);

$items = $order->getAllVisibleItems();

foreach($items as $item) {

$product = Mage::getModel(‘catalog/product’)->load($item->getProductId());

echo ‘”identifier”:”‘.$product->getSku().'”,';

echo ‘”amount”:”‘.$product->getPrice().'”,';

$order = Mage::getSingleton(‘sales/order’)->load($orderId, ‘increment_id’);

$items = $order->getAllItems();

foreach($items as $item) {

$product = Mage::getModel(‘catalog/product’)->load($item->getProductId());

if($product->getTypeId() == ‘simple’ ) {

echo ‘”identifier”:”‘.$product->getSku().'”,';

echo ‘”amount”:”‘.$product->getPrice().'”,';

}

}

How to display/portray product image for the ordered items on admin panel – order view

<?php /** @var \Magento\Sales\Block\Adminhtml\Order\View\Items\Renderer\DefaultRenderer $block */ ?>

<?php $_item = $block->getItem() ?>

<?php $block->setPriceDataObject($_item) ?>

<tr>

<?php $i = 0;

$columns = $block->getColumns();

$lastItemNumber = count($columns) ?>

<td>

<?php $product =  $_item->getProduct();?>

<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance();?>

<?php  $imagewidth=200;

$imageheight=200;

$imageHelper  = $objectManager->get(‘\Magento\Catalog\Helper\Image’);

$image_url = $imageHelper->init($product, ‘small_image’)->setImageFile($product->getSmallImage())->resize($imagewidth, $imageheight)->getUrl();

?>

<img src=”<?php echo $image_url;?>” />

</td>

<?php foreach ($columns as $columnName => $columnClass):?>

<?php $i++; ?>

<td class=”<?= /* @noEscape */ $columnClass ?><?= /* @noEscape */ ($i === $lastItemNumber ? ‘ last’ : ”) ?>”><?= /* @escapeNotVerified */ $block->getColumnHtml($_item, $columnName) ?></td>

<?php endforeach; ?>

</tr>

Recent Posts

Leave a Comment

quick inquiry in magentoguys
REQUEST A QUOTE x

captcha txt
Not readable? Change text. 100% Privacy - We Will Never Spam You!

Start typing and press Enter to search

Tips to Enhance the Speed of your Magento StoreHow to Make Magento eCommerce Store Faster?Let’s Know about the Common SEO MistakesCommon SEO mistakes of Most eCommerce Websites