1

I want to create Plugin for observer SalesOrderAfterPlaceObserver, I created a di.xml file and added below coded,

<type name="Magento\SalesRule\Observer\SalesOrderAfterPlaceObserver">
    <plugin name="NameSpace_SalesRule::SalesOrderAfterPlaceObserver" type=" NameSpace\SalesRule\Plugin\Observer\SalesOrderAfterPlaceObserverPlugin" sortOrder="1"/>
</type>

And Created SalesOrderAfterPlaceObserverPlugin.php plugin File,

<?php

namespace NameSpace\SalesRule\Plugin\Observer;

use Magento\SalesRule\Observer\SalesOrderAfterPlaceObserver;
use Magento\Framework\Event\Observer as EventObserver;

class SalesOrderAfterPlaceObserverPlugin
{
    /**
     * @param SalesOrderAfterPlaceObserver $subject
     * @param                              $result
     * @param EventObserver                $observer
     */
    public function afterExecute(SalesOrderAfterPlaceObserver $subject, $result, EventObserver $observer)
    {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info('Your text message');

    }
}

But My Plugin is not calling.

Does have any idea?

  • There is a space in the type declaration attribute. Is this a typo here or is it also in your source code? – Quisse Jun 22 '18 at 9:11
1

You can try this updated code:

di.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
   <type name="Magento\SalesRule\Observer\SalesOrderAfterPlaceObserver">
      <plugin name="NameSpace_SalesRule::SalesOrderAfterPlaceObserver" type="NameSpace\SalesRule\Plugin\Observer\SalesOrderAfterPlaceObserverPlugin" sortOrder="1"/>
  </type>
</config>

SalesOrderAfterPlaceObserverPlugin.php

<?php
namespace NameSpace\SalesRule\Plugin\Observer;

use Magento\SalesRule\Observer\SalesOrderAfterPlaceObserver;
use Magento\Framework\Event\Observer as EventObserver;

class SalesOrderAfterPlaceObserverPlugin
{
    /**
     * @param SalesOrderAfterPlaceObserver $subject
     * @param                              $result
     * @param EventObserver                $observer
     */
    public function afterExecute(SalesOrderAfterPlaceObserver $subject, $result, EventObserver $observer)
    {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info('Your text message');

    }
}

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.