7

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?

3
  • I am running Magento 2.1, and the other answers I have seen for this question seem to only work for Magento 2.0
    – opqr549
    Commented Jul 21, 2016 at 20:54
  • 1
    You can read more: magento.stackexchange.com/questions/116914/… Commented Jul 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?
    – opqr549
    Commented Jul 22, 2016 at 17:07

2 Answers 2

6

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.

7
  • 1
    This 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.
    – opqr549
    Commented Jul 22, 2016 at 15:00
  • @opqr549 did you every resolve this? I'm having the same issue.
    – jtate
    Commented Oct 24, 2016 at 12:56
  • My getGrandTotal() everytime returns NULL if order made as guest Commented Jan 19, 2017 at 15:34
  • 1
    noobie question but where do put this php to get it to work?
    – Ahdee
    Commented Jun 4, 2017 at 4:34
  • 1
    If 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. Commented Nov 10, 2020 at 15:14
0
//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;?>

Not the answer you're looking for? Browse other questions tagged or ask your own question.