6

I have added custom field on checkout page.

I want to get all the post data, in Checkout success observer,

Here is my observer code

<?php
/** 
 Author : Disha
 **/
 namespace Neo\OrderComment\Observer;

 use Magento\Framework\Event\ObserverInterface;
 use Magento\Framework\App\RequestInterface;

 class Orderneo implements \Magento\Framework\Event\ObserverInterface
 {
    /** @var \Psr\Log\LoggerInterface **/
    protected $_logger;
    /** @var \Magento\Sales\Model\Order\Status\HistoryFactory **/
    protected $_historyFactory;
    /** @var \Magento\Sales\Model\OrderFactory **/
    protected $_orderFactory;
    /**
     * @param \Psr\Log\LoggerInterface
     * @param \Magento\Sales\Model\Order\Status\HistoryFactory
     * @param \Magento\Sales\Model\OrderFactory
     **/
    public function __construct(
        \Psr\Log\LoggerInterface $logger,
        \Magento\Sales\Model\Order\Status\HistoryFactory $historyFactory,
        \Magento\Sales\Model\OrderFactory $orderFactory){
        $this->_logger = $logger;
        $this->_historyFactory = $historyFactory;
        $this->_orderFactory = $orderFactory;
    }
    /**
     * Add order comment, to the last placed order
     * @param \Magento\Framework\Event\Observer
     **/
    public function execute(\Magento\Framework\Event\Observer $observer)
    {   
        // Get order id, of last placed order
        $orderIds = $observer->getEvent()->getOrderIds();
        foreach($orderIds as $orderId){
            $order = $this->_orderFactory->create()->load($orderId);
        }
        echo "<pre>"; print_r($this->getRequest()->getPost());
        exit;
    }
}

But I am getting this error, as unknown method getRequest(),

Fatal error:  Uncaught Error: Call to undefined method Neo\OrderComment\Observer\Orderneo::getRequest() in /var/www/html/magento2/app/code/Neo/OrderComment/Observer/Orderneo.php:42
Stack trace:
#0 /var/www/html/magento2/vendor/magento/framework/Event/Invoker/InvokerDefault.php(73): Neo\OrderComment\Observer\Orderneo->execute(Object(Magento\Framework\Event\Observer))
#1 /var/www/html/magento2/vendor/magento/framework/Event/Invoker/InvokerDefault.php(61): Magento\Framework\Event\Invoker\InvokerDefault->_callObserverMethod(Object(Neo\OrderComment\Observer\Orderneo), Object(Magento\Framework\Event\Observer))
#2 /var/www/html/magento2/vendor/magento/framework/Event/Manager.php(66): Magento\Framework\Event\Invoker\InvokerDefault->dispatch(Array, Object(Magento\Framework\Event\Observer))
#3 /var/www/html/magento2/var/generation/Magento/Framework/Event/Manager/Proxy.php(95): Magento\Framework\Event\Manager->dispatch('checkout_onepag...', Array)
#4 /var/www/html/magento2/vendor/magento/module-checkout/Controller/Onepage/Success.php in /var/www/html/magento2/app/code/Neo/OrderComment/Observer/Orderneo.php on line 42
4
  • As an additional solution you can add the request to your class through dependency injection and access it via singleton Commented Mar 30, 2017 at 8:36
  • Checkout success observer, are u taking for which event?From where you have posted the data?
    – Amit Bera
    Commented Mar 30, 2017 at 8:52
  • @RakeshJesadiya already added this as an answer Commented Mar 30, 2017 at 8:54
  • @AmitBera I am using checkout_onepage_controller_success_action this event Commented Mar 30, 2017 at 8:57

2 Answers 2

14
protected $_request;

public function __construct(
    \Magento\Framework\App\RequestInterface $request,
    \Psr\Log\LoggerInterface $logger,
    \Magento\Sales\Model\Order\Status\HistoryFactory $historyFactory,
    \Magento\Sales\Model\OrderFactory $orderFactory
) { 
    $this->_request = $request;
    $this->_logger = $logger;
    $this->_historyFactory = $historyFactory;
    $this->_orderFactory = $orderFactory;
}

Inside function get post data as below way:

$this->_request->getPost();

Don't forget to clear var/generation folder.

7
  • I am getting this error, after implementing yours Commented Mar 30, 2017 at 8:45
  • Argument 4 passed to Neo\OrderComment\Observer\Orderneo::__construct() must be an instance of Magento\Framework\App\Action\Context, none given Commented Mar 30, 2017 at 8:45
  • I have updated my answer and remove var/generation folder before check. Commented Mar 30, 2017 at 8:51
  • okay thnks @Rakesh, I will check. Commented Mar 30, 2017 at 8:53
  • let me know if you have issue after updated answer. Commented Mar 30, 2017 at 9:04
4

I'm pretty sure you need to replace:

$this->getRequest()->getPost()

With:

$observer->getRequest()->getPost()

But it may depend on the event you're observing IIRC.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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