1

registration.php

 <?php
    \Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Hafiz_Sms',
    __DIR__
    );

etc/events.xml

<?xml version="1.0"?>
<configxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<eventname="sales_order_place_after">
<observername="place_order_after" instance="Hafiz\Sms\Observer\Orderplaceafter" />
</event>
</config>

etc/module.xml

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Hafiz_Sms" setup_version="2.1.1"></module>
</config>

Observer/Orderplaceafter.php

<?php
namespace Hafiz\Sms\Observer;
use Magento\Framework\Event\ObserverInterface;
use Psr\Log\LoggerInterface;
class Orderplaceafter implements ObserverInterface
{
protected $logger;
public function __construct(LoggerInterface$logger) {
$this->logger = $logger;
}
public function execute(\Magento\Framework\Event\Observer $observer){
  $order = $observer->getEvent()->getOrder();
  echo $orderId = $order->getId();
//  $comment = $this->getRequest()->getParam('comment');
  print_r("Catched event succssfully !"); exit;
}
}

But it's not working. How to debug in this event?. Kindly suggest me where i am wrong?

  • 2
    Not sure if it's just a typo in the question but <eventname should be <event name and <observername should be <observer name. That could be your problem if that's how it is in the module. – rain2o Apr 11 at 14:30
  • Hi Hafiz Arslan.are you completed debugging? – MSA Apr 15 at 15:45
1

The event will be defined at etc/events.xml Nor etc/event.xml.Seems file name is wrong.

events.xmlcode will be like.Also, the Syntax of declare event is wrong. Please used below code:

<?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 name="place_order_after" instance="Hafiz\Sms\Observer\Orderplaceafter" />
    </event>
</config>

Follow this article for how to implement event at module https://devdocs.magento.com/guides/v2.2/extension-dev-guide/events-and-observers.html.

Forgetting purpose, don't use print_r, die and exit. Use Magento loggger for print for debugging Magento 2: Replacement for Mage::log method?

  • it redirect checkout/cart page . not show print_r("Catched event succssfully !"); exit; this one? – Hafiz Arslan Apr 11 at 14:56
1

Yes, it is an interesting question. Many people may know about sales_order_place_after event.

If you use this event, you can't use print_r() and exit() function.

Otherwise, kindly use checkout_onepage_controller_success_action event. It will a helpful lot

  • checkout_onepage_controller_success_action is this event order place event? – Hafiz Arslan Apr 12 at 15:19
  • This event use for after order placed – MSA Apr 13 at 16:06
0

You declared event name and observer name as observername and eventname.

<?xml version="1.0"?>
<configxmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
<event name="sales_order_place_after">
<observer name="place_order_after" instance="Hafiz\Sms\Observer\Orderplaceafter" />
</event>
</config>

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.