2

This is my code. The event is not getting triggerd after I place a new order.

app/code/Company/RalSurcharge/etc/frontend/events.xml (i also tried app/code/Company/RalSurcharge/etc/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_before">
        <observer name="company_ralsurcharge_sales_order_place_before" instance="Company\RalSurcharge\Observer\RalSurcharge2" />
    </event>
</config>

app/code/Company/RalSurcharge/Observer/RalSurcharge2.php

<?php
namespace Company\RalSurcharge\Observer;

use \Magento\Framework\Event\ObserverInterface;
use \Magento\Framework\Event\Observer;

class RalSurcharge2 implements ObserverInterface
{   
    public function execute(Observer $observer)
    {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/observer.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info("Hello from sales_order_place_before observer");
    }



}

2 Answers 2

3

Avoid digits or any special symbols in file names or Class names.

For the fast and easy creation of basic files, you can use https://mage2gen.com/ tool. It'll generate all needed files automatically for you so that you do not have to worry about the files and it's structure.

3

I figured it out. The problem is the number in the filename. I changed it from RalSurcharge2 to RalSurchargeTwo, now it works...

If it still not works, then it can be because the file events.xml is in the wrong folder. I wonder why it ever worked for me, normally it has to be in MODULE/etc/webapi_rest

https://magento.stackexchange.com/a/189439/66769

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.