0

I built a custom module which has a custom observer OrderSaveAfter.php. How can I call any function inside the custom observer from another custom code ?

  • When You Want To call Observer @Jsparo30 – Learing_Coder Jul 12 '17 at 4:10
  • let me know whether u would like to save any data in sales_order table? – Nagaraju K Jul 12 '17 at 4:40
  • please let me know u r comments whether it is working or not ? – Nagaraju K Jul 12 '17 at 8:00
  • @PranayK, for a special case I built custom php code under Magento root, so i need to call observer from it. – Jsparo30 Jul 12 '17 at 11:50
0

you can use an event called checkout_onepage_controller_success_action

Please follow the below steps

Step 1: app/code/Ewall/Paytrade/etc/frontend/events.xml

<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="Ewall_Paytrade_After_Place_Order_Success" instance="Namespace\Module\Observer\AfterOrder"  />
       </event>
    </config>

Step 2:

<?php
 namespace Ewall\Paytrade\Observer;
 use Magento\Framework\Event\ObserverInterface;
 use Magento\Framework\App\RequestInterface;
 use Magento\Sales\Api\OrderRepositoryInterface;
 use Magento\Customer\Model\Session;
class PlaceOrderSuccess implements ObserverInterface
{ 

    protected $orderRepository;
    protected $_session;

    public function __construct(  
    OrderRepositoryInterface $OrderRepositoryInterface,
    \Magento\Customer\Model\Session $session
    ) {
        $this->orderRepository = $OrderRepositoryInterface;
        $this->_session = $session;

    }

    public function execute(\Magento\Framework\Event\Observer $observer) 
    {

           $writer = new \Zend\Log\Writer\Stream(BP.'/var/log/TradeorderSuccess.log');
           $logger = new \Zend\Log\Logger();
           $logger->addWriter($writer);            

           $order_ids = $observer->getEvent()->getOrderIds()[0];
           $order = $this->orderRepository->get($order_ids);           
           $order_id = $order->getIncrementId();               

          foreach ($order->getAllVisibleItems() as $item)
          {
                $logger->info("xxxxxxxxxxxxxxxxxx===>".$item->getName());
           } 

          $order->save();
          $logger->info("saved");
    }


}

Step 3: after did the changes use di.compile and clear cache & pagecache and and check the same.

  • Thank you for your answer, But how can i call a specific custom observer ? – Jsparo30 Jul 12 '17 at 11:56

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.