4

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>
9

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 !!!.

  • On Which action it will trigger? – Priya Ponnusamy Dec 1 '17 at 8:18
  • Whenever totals are calculated, i.e. after adding/editing product in cart and after going to checkout. – Vicky Dev Dec 1 '17 at 20:47
2

Kindly refer the Below module which they implemented the event sales_quote_collect_totals_after as you required to do refer their

1.webapi_soap/events.xml

OR

2.webapi_rest/events.xml

3.Observer

Magento_Msrp which resides in the Vendor/magento/module-msrp

1

Try to mention the method name in the file 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\Model\Observer" method="eventLogging"/>
    </event>
</config>

and then declare the method name in the Observer file :

<?php

namespace Learning\Custom\Model;

class Observer {
    public function __construct(
        \Psr\Log\LoggerInterface $logger
    ) {
        $this->logger = $logger;
    }
    public function eventLogging(\Magento\Framework\Object $observer) {
        $event = $observer->getEvent();
        $quote = $event->getQuote();

        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/developer.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info($quote->getData(),true); 
    }
}

Also, Mage::log is no longer used to print logs in magento 2. I have mentioned the alternative in my answer.

Hope this helps.

  • Hi manashvi problem is in XMLSchema because lattest magento use <config xmlns:xsi="w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd"> – Pratik Dec 4 '15 at 12:01
  • May be "urn.." thing could be issue too, but big issue in my module was I created Observer file inside "Model" directory, which is not the flow allowed in Magento 2, I put it in the "Observer" directory and changed the filename and it's working now. I will update in an answer here soon. – Vicky Dev Dec 4 '15 at 12:12
  • Yes urn might be the issue but the Observer needs to be placed inside the Model directory only. – Manashvi Birla Dec 4 '15 at 12:27
  • @Manashvibirla I need another help form you, when I put your code to print the quote data to log file, the product adding to cart fails giving message We can't add this item to your shopping cart right now.. Can you give another method for logging data or may be help me fix this ? – Vicky Dev Dec 4 '15 at 12:34
  • check this out magento.stackexchange.com/questions/92434/… – Manashvi Birla Dec 4 '15 at 12:36
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

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.