0

I have created an app with the Amazon API allowing me to create orders to be fulfilled by Amazon. I want to capture my customers order information (product SKU's, address, name, etc.) when their payment is accepted and send it to Amazon. What is the best way to grab this information? Magento version 2.1.

  • Where you want capture the customer information ? Mean checkout page ? Before place order button ? – Sourav Jul 23 '16 at 2:54
0

Here's a simple way to display what you need. You'll need to plug what you need into your api. You can get the session variables by instantiating a model class using the singleton method.

$items = Mage::getSingleton('checkout/session')->getQuote()->getAllItems();

foreach($items as $item) {
    echo 'ID: '.$item->getProductId().'<br />';
    echo 'Name: '.$item->getName().'<br />';
    echo 'Sku: '.$item->getSku().'<br />';
    echo 'Quantity: '.$item->getQty().'<br />';
    echo 'Price: '.$item->getPrice().'<br />';
    echo "<br />";
}
  • Is this only for Magento 1? – opqr549 Jul 22 '16 at 19:43
  • 2
    This is a Magento 1 answer OP is asking for Magento 2 – Raphael at Digital Pianism Jul 22 '16 at 20:01
  • edited 1 hour ago <<< he didn't specify before the edit. @RaphaelatDigitalPianism – thismethod Jul 22 '16 at 20:57
  • In magento2 Mage :: getModel or getSingleton is not using. These are magento1 factory methods. – Sourav Jul 23 '16 at 2:50
0

Use the \Magento\Checkout\Model\Session::getLastRealOrder() method.

I assume that you must be using a 3rd party payment gateway and after the payment is made it will redirect back to a url you specified which will hit a controller.

<?php
namespace Custom\Module\Controller\Response;

use Magento\Framework\App\Action\Context;
use Magento\Checkout\Model\Session;

class Response extends \Magento\Framework\App\Action\Action
{
  protected $_session;

  public function __construct(Context $context, Session $session){
     $this->_session = $session;
     return parent::__construct($context);
  }
  public function execute(){
    /*this method should be hit and you can have the logic to check the status of the payment */ 
    /*if the payment is successful you can get the order as below*/
     $order = $this->_session->getLastRealOrder(); 

  }

}

From the order object you can access the info you need and send it to Amazon.

  • Getting an error message: 1 exception(s): Exception #0 (Exception): Recoverable Error: Argument 1 passed to Magento\Framework\App\Action\Response::__construct() must be an instance of Magento\Framework\App\Action\Context, none given, called in /chroot/home/ikonopsc/ikonops.com/html/vendor/magento/module-checkout/view/frontend/templates/success.phtml on line 30 and defined in /chroot/home/ikonopsc/ikonops.com/html/vendor/magento/module-checkout/view/frontend/templates/success.phtml on line 16 – opqr549 Jul 25 '16 at 17:24
  • Sorry there was a syntax error in line 5. it should be use Magento\Checkout\Model\Session; It has been corrected with the current edit. After editing delete var/generate folder and clear cache and check. – Lakshin Karunaratne Jul 26 '16 at 7:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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