0

I am trying to observe an event when the order is placed but is not working for me. I tried with "sales_order_place_after" and "checkout_submit_all_after" events but nothing happens. The "test.log" file is not generated.

My "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="send_order_tracy" instance="Tracy\ShippingMethod\Observer\SendOrderToTracy"/>
</event>
</config>

My observer class:

<?php

namespace Tracy\ShippingMethod\Observer;

class SendOrderToTracy implements \Magento\Framework\Event\ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
       $writer = new \Zend\Log\Writer\Stream(BP.'/var/log/test.log'); // creates new log file named test.log in var/log folder
       $logger = new \Zend\Log\Logger();
       $logger->addWriter($writer);
       $logger->info("oe repinga plaiciƩ la orden"); // this will be print in test.log file
    }
}

I tried with the first event and later with the other one, changing just the event name. Is that correct? Do I need to compile or something like that? I am just flushing cache.

0

Try the @Rakesh response for this link:

<?php
namespace Tracy\ShippingMethod\Observer;

class SendOrderToTracy implements \Magento\Framework\Event\ObserverInterface
{
    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;
    }
}
  • Thanks @Ylgen, I will try. But, I am wondering, where this print shouls occurs? I mean, where this function print_r writes? – Daniel Silva Oct 16 '18 at 20:18
  • Try to add content to comment field before placing an order. – Ylgen Guxholli Oct 16 '18 at 20:23
  • sorry, I am not getting yout idea. Comments where? (I don´t see any field to add a comment), but more important, add comment for what? – Daniel Silva Oct 16 '18 at 20:41

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.