2

I would like to call an observer called 'salesQuoteSaveAfter' in magento 2 but it is not working could you please guide me or suggest me whether i did mistake.

File Paths:

1) app/code/Ewall/CustomPrice/Observer/SalesQuoteSaveAfterCustom.php

namespace Ewall\CustomPrice\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;

class SalesQuoteSaveAfterCustom
{   
    protected $checkoutSession;

        public function __construct(\Magento\Checkout\Model\Session $checkoutSession)
        {
        $this->checkoutSession = $checkoutSession;
        }
    public function invoke($observer)
    {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info('invoke function is calling');

        $quote = $observer->getEvent()->getQuote();

        /* @var $quote \Magento\Quote\Model\Quote */

        if($quote->getIsCheckoutCart())
        {
        $this->checkoutSession->getQuoteId($quote->getId());
        } 
     }
}

2 ) app/code/Ewall/CustomPrice/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_save_after">
<observer name="Ewall_CustomPrice_Custom" instance="Ewall\CustomPrice\Observer\SalesQuoteSaveAfterCustom" />
</event>

could you please help me. thanks

0

2 Answers 2

1

Try following way:

namespace Ewall\CustomPrice\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\RequestInterface;

class SalesQuoteSaveAfterCustom implements ObserverInterface
{
    protected $checkoutSession;

    public function __construct(\Magento\Checkout\Model\Session $checkoutSession)
    {
        $this->checkoutSession = $checkoutSession;
    }
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $logger->info('invoke function is calling');

        $quote = $observer->getQuote();

        /* @var $quote \Magento\Quote\Model\Quote */

        if($quote->getIsCheckoutCart())
        {
            $this->checkoutSession->getQuoteId($quote->getId());
        }
    }
}
2
  • Thank you Rana ji its calling now but i unable to get quote details like id..etc how can i get ? . Jan 25, 2017 at 5:11
  • Check updated answer.
    – Sohel Rana
    Jan 25, 2017 at 5:44
0

I think you should use events.xml in etc directory Instead of frontend/events.xml I don't know the exact reason why sales events are behaving like this may be some Magento expert can give the exact reason. I know this because I had similar problem some time ago

And If you want to use this event only for front-end then you can put events.xml in etc > webapi_rest > events.xml

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.