Magento 2 – Troubleshooting Simple Model Observer Issues

event-observerfrontendmagento2model

May be "clarification" is one of the reasons, I am posting a query about Magento 2 Observer here.

As I am trying to execute an event observer on sales_quote_collect_totals_after event but it doesn't seem to run at all, when I do hard-refresh the Checkout/Cart page.

I have searched many online posts and tutorials, but every post seems to have different code and it goes a bit confusing at times, so my final question is how to get observer working with only observer class file and events xml ?

Learning/Custom/Model/Observer:

<?php

namespace Learning\Custom\Model;

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

class Observer implements ObserverInterface {
    public function execute(Observer $observer) {
        $event = $observer->getEvent();
        $quote = $event->getQuote();
        Mage::log(print_r(Zend_Debug::dump($quote->getData()), true),NULL, 'developer.log');
        exit;
    }
    /*public function execute(\Magento\Framework\Event\Observer $observer) {
        rm_state()->controllerSet($observer['controller_action']);
    }*/
}

etc/frontend/events.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../../lib/internal/Magento/Framework/Event/etc/events.xsd">
        <event name="sales_quote_collect_totals_after">
        <observer name="learning_custom_event_obs" instance="Learning\Custom\Model\Observer" />
    </event>
</config>

etc/module.xml:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="../../../../../lib/internal/Magento/Framework/Module/etc/module.xsd">
    <module name="Learning_Custom" setup_version="1.0.0">
        <sequence><module name="Magento_Core"/></sequence>
        <sequence><module name="Magento_Store"/></sequence>
    </module>
</config>

Best Answer

I followed the below link and put the observer class file inside the "Observer" directory and now it works, the code and paths as below:

http://blog.salesids.com/fr/observer-un-evenement-dans-magento-2/

Learning/Custom/Observer/GetQuote:

<?php

namespace Learning\Custom\Observer;

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

class GetQuote implements ObserverInterface {
    public function execute(Observer $observer) {
        $event = $observer->getEvent();
        $quote = $event->getQuote();
            $myfile = fopen("var/log/debug.log", "a+") or die("Unable to open file!");
fwrite($myfile, print_r($quote->getData(),true));
fclose($myfile);
    }
}

etc/frontend/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_quote_collect_totals_after">
        <observer name="learning_custom_event_obs" instance="Learning\Custom\Observer\GetQuote" />
    </event>
</config>

This did the magic, thanks everybody for your replies !!!.

Related Topic