1

I have been trying to resolve this issue for 2 days but cannot seem to find the issue why the observers won't trigger. I hope some good soul out there might spot the issue and help me out on this on.

Background: I want to write an observers which are triggered when an item is added to the cart and when the order was confirmed/checked out. The observers will ultimately update some customField in Jira and get some files into the magento database as well. But currently they should just write two files into the /tmp directory.

etc/webapi_rest/events.xml

<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="jira-confirmation-update" instance="one\two\Observer\JiraConfirmationUpdate" />
    </event>
    <event name="checkout_cart_add_product_complete">
        <observer name="jira-get-attachments" instance="one\two\Observer\JiraGetAttachments" />
    </event>
</config>

Observer/JiraConfirmationUpdate.php

<?php
namespace one\two\Observer;

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

class JiraConfirmationUpdate implements ObserverInterface
{
  public function __construct()
  {

  }

  public function execute(Observer $observer)
  {

  $fp = fopen('/tmp/confirm.txt', 'w');
  fwrite($fp, 'order confirmed!');
  fclose($fp);

  }
}

?>

Observer/JiraGetAttachments.php

<?php

namespace one\two\Observer;

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

class JiraGetAttachments implements ObserverInterface
{
  public function __construct()
  {

  }

  public function execute(Observer $observer)
  {

  $fp = fopen('/tmp/add2cart.txt', 'w');
  fwrite($fp, 'getting attachments!');
  fclose($fp);
  }
}

?>

just on a side note, when i run php JiraGetAttachments.php in the terminal i get the error

PHP Fatal error:  Interface 'Magento\Framework\Event\ObserverInterface' not found in /var/www/html/app/code/one/two/Observer/JiraConfirmationUpdate.php on line 7

is that the an usual behaviour?

EDIT for some reason after enabling the module I can not checkout anymore, disabling the module will make it possible to checkout again...

2 Answers 2

0

An example of how to get magento2 Observer working can be found in the following files inside the vendor folder.

magentofolder\vendor\magento\module-catalog\etc\frontend\events.xml
magentofolder\vendor\magento\module-catalog\Observer

If my answer helped you, you can accept the answer so it can help others also.

2
  • 1
    Thanks for the information I will take a look at those observers
    – jinnn
    Mar 16, 2020 at 11:37
  • @jinnn You are welcome, If my answer helped you, you can accept the answer so it can help others also. Mar 16, 2020 at 12:59
0

You can use this code step by step make sure module name must be in caps

app\code\One\Two\registration.php

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

app\code\One\Two\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="One_Two" setup_version="1.0.0">
    </module>
</config>

app\code\One\Two\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_after">
        <observer name="jira-confirmation-update" instance="One\Two\Observer\JiraConfirmationUpdate" />
    </event>
</config>

app\code\One\Two\Observer\JiraConfirmationUpdate.php

<?php
namespace One\Two\Observer;

class JiraConfirmationUpdate implements \Magento\Framework\Event\ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/templog.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info("Here");
    }
}
3
  • Thank you for your reply, but unfortunately this doesn't work for me
    – jinnn
    Mar 16, 2020 at 11:47
  • what is error ? Mar 16, 2020 at 11:53
  • when i manually try to check out as a customer i get "Something went wrong with your request. Please try again later." If i try to checkout again i get "Current customer does not have an active cart.". It should be mentioned that I'm stuck at the last step and cannot finish the order. The "templog.log" file wasn't created so i can only guess that the observer didn't trigger
    – jinnn
    Mar 16, 2020 at 12:36

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.