1

I have created new extension to get data after successful order in magento 2.

I have created below files.

Narola\Eye4frauds\etc\module.xml
Narola\Eye4frauds\registration.php
Narola\Eye4frauds\etc\events.xml
Narola\Eye4frauds\Observer\Checkout\OnepageControllerSuccessAction.php

Here is my events.xml

   <?xml version="1.0"?>  
      <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
          <event name="sales_order_place_after">
              <observer instance="Narola\Eye4frauds\Observer\Checkout\OnepageControllerSuccessAction"  name="Narola_Eye4frauds_observer_checkout_onepagecontrollersuccessaction_sales_order_place_after"/>
          </event>
      </config>

here is my OnepageControllerSuccessAction.php file

namespace Narola\Eye4frauds\Observer\Checkout;
  class OnepageControllerSuccessAction implements 
   \Magento\Framework\Event\ObserverInterface
  {
public function __construct(\Magento\Sales\Model\Order $order)
{
    $this->order = $order;
}
public function execute(\Magento\Framework\Event\Observer $observer)
{
    $orderids = $observer->getEvent()->getOrderIds();

    echo "<pre>";       var_dump($orderids);        echo "</pre>";      die;

    try {
        foreach ($orderids as $orderid) {
            $order = $this->order->load($orderid);                    
            $order->hold();
            $order->save();
        }
    } catch (\Magento\Framework\Exception\LocalizedException $e) {
        // Error logic
    } catch (\Exception $e) {
        // Generic error logic
    }
  }
}

here I have printed var_dump($orderids); but it does not display. This file is not called. Did I make any mistake?

1
  • Hello mask are you sure your module is enabled? You should use different event to according my experience. Wait a bit.
    – P S
    Jan 17, 2018 at 6:32

2 Answers 2

0

I tried the same way , you can check here, but ended using checkout_onepage_controller_success_action:

<?xml version="1.0"?>
<!--
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
-->

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
   <event name="checkout_onepage_controller_success_action">
       <observer name="process_gateway_redirect" instance="Magento\SamplePaymentGateway\Observer\ProcessGatewayRedirect" />
   </event>
</config>

Use above in you events.xml. Your observer should be :

<?php
/**
 * Copyright © 2016 Magento. All rights reserved.
 * See COPYING.txt for license details.
 */
namespace Magento\SamplePaymentGateway\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Store\Model\StoreManagerInterface;

class ProcessGatewayRedirect implements ObserverInterface
{
    /**
     * @param Observer $observer
     * @return void
     */
    private $storeManager;
    protected $_checkoutSession;
    protected $customerSession

    public function __construct(
        StoreManagerInterface $storeManager,
        \Magento\Checkout\Model\Session $checkoutSession,
        \Magento\Customer\Model\Session $customerSession

    ) {
        $this->storeManager = $storeManager;
        $this->_checkoutSession = $checkoutSession;
        $this->customerSession = $customerSession;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    { 
        $orderId = $observer->getEvent()->getOrderIds();
        $base_url = $this->storeManager->getStore()->getBaseUrl();
        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $order = $objectManager->create('\Magento\Sales\Model\Order')->load($orderId[0]);
        $payment = $order->getPayment();
        echo 'lkjlkj';

        if($customerSession->isLoggedIn()) {
           echo   $customerSession->getCustomer()->getId()."<br/>";  // get Customer Id
           echo   $customerSession->getCustomer()->getName()."<br/>";  // get  Full Name
           echo   $customerSession->getCustomer()->getEmail()."<br/>"; // get Email Name
           echo   $customerSession->getCustomer()->getGroupId()."<br/>";  // get Customer Group Id
           }  
           echo $customerId;
            $method = $payment->getMethodInstance();
            $methodTitle = $method->getTitle();
            $qid = $order->getQuoteId();
            $order_data= $order->getData();
            $status = $this->_checkoutSession->getLastOrderStatus();
            $increment_id = $order_data['increment_id'];
            $redirect = $objectManager->get('\Magento\Framework\App\Response\Http');
            $redirect->setRedirect($base_url.'paym/redirect/index/id/'.$increment_id.'');
        return;
    }
}

I'm sure this works for you because I'm using it as well.

1
  • Thansk you. now it's working.
    – TheMask
    Jan 17, 2018 at 7:09
0

You could also use the checkout_submit_all_after event, which directly brings you the relevant order and quote objects. That way you don't have to load from the database.

It is dispatched at the end of the order placement process though, so make sure to not do too heavy handed stuff.

Your Answer

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

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