I need to get the order information (shipping details, item SKU's) in order to send it over to the Amazon API for easy FBA shipping. Ideally I will be able to capture this information upon a successful payment. How do you get this information?
-
I am running Magento 2.1, and the other answers I have seen for this question seem to only work for Magento 2.0– opqr549Jul 21, 2016 at 20:54
-
1You can read more: magento.stackexchange.com/questions/116914/…– Khoa TruongDinhJul 22, 2016 at 2:33
-
I am struggling to implement that solution effectively, my understanding of how to create custom Magento 2 modules isn't quite where it needs to be. Are there any good online resources (outside of official docs) to learn this process?– opqr549Jul 22, 2016 at 17:07
2 Answers
You can use below code to get details in success.phtml file. It will work for magentoce2.1 also.
<?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;
?>
Kindly note that I do not recommend using object manager like this directly. I insist please create your block and pass detail from there and you can retrieve in this file.
-
1This is partially working, I can successfully create the order object that contains all the correct method, however the only value that gets displayed is the order id.– opqr549Jul 22, 2016 at 15:00
-
-
My getGrandTotal() everytime returns NULL if order made as guest Jan 19, 2017 at 15:34
-
1
-
1If you warn not to do it this way, why not just show the way you DO recommend doing it? Why not just NOT show this and show the right way? Makes zero sense. Nov 10, 2020 at 15:14
//Use this Class to get order id
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;?>