0

I want to intercept sales_order_place_before event, so I created a new module named app\code\Giulio\CheckoutExtention, created aregistration.php` file where I put this code below:

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

a module.xml inside app\code\Giulio\CheckoutExtention\etcwhere I have declared my module like following:

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd" >
    <module name="Giulio_CheckoutExtension" setup_version="0.0.1"/>
</config>

and then always inside \etc folder, an events.xmlwhere i declared my Observer like this.

<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="set_checkout_if_no_different_categories" instance="Giulio\CheckoutExtension\Observer\SetCheckout" />
    </event>
</config>

Then inside \Giulio\CheckoutExtension\Observer a SetCheckout.php file where I extended Monolog\Logger class for loggin some messages and implemented ObserverInterface like this:

<?php

namespace Giulio\CheckoutExtension\Observer;

use Magento\Framework\Event\Observer;
use Monolog\Logger;

class SetCheckout extends Logger implements \Magento\Framework\Event\ObserverInterface {



    public function execute(Observer $observer) {
        $order = $observer->getEvent()->getOrder();
        $order->setOrderId("CIAO");
        $this->warning("Ho creato un Log con l'observer");
    }
}

after executing

rm -rf var/di/* var/generation/* var/cache/* var/log/* var/page_cache/*

php bin/magento cache:clean

php bin/magento cache:flush

php bin/magento setup:upgrade

php bin/magento setup:di:compile

php bin/magento indexer:reindex

and tried to accessing Homepage for my store, the page wont load and it will be left blank.

Any suggestion?

Thanks you all anticipately!

3
  • Please check your log files. also, let me know which server you are using? Commented May 18, 2018 at 13:27
  • @NikunjVadariya im using XAMPP, i fixed this, the problem was the implementation of Monolog\Logger, i changed it passing at __constructor() the \Psr\Log\LoggerInterface $logger. Commented May 18, 2018 at 13:35
  • if my answer help in your concern then there is green right tick mark , hit that tickmark for right ansewer that will help future readers. Commented May 18, 2018 at 13:45

1 Answer 1

0

Replace your code with below the code. it will resolve your issue.

<?php

namespace Giulio\CheckoutExtension\Observer;

use Magento\Framework\Event\Observer;

class SetCheckout implements \Magento\Framework\Event\ObserverInterface {
    public $_logger;
    public function __construct(
        \Psr\Log\LoggerInterface $logger

    ) {
        $this->_logger = $logger;        
    }

    public function execute(Observer $observer) {
        $order = $observer->getEvent()->getOrder();
        $order->setOrderId("CIAO");
        $this->_logger->warning("Ho creato un Log con l'observer");
    }
}

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.