7

This question already has an answer here:

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?

marked as duplicate by 7ochem, Murtuza Zabuawala, Khoa TruongDinh, brentwpeterson, Keyul Shah Feb 3 '17 at 16:43

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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 Jul 21 '16 at 20:54
  • 1
    You can read more: magento.stackexchange.com/questions/116914/… – Khoa TruongDinh Jul 22 '16 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 Jul 22 '16 at 17:07
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.

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