3

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="payment_method_is_active">
        <observer name="ComapnyName_ModuleNames_DisabledCcByPincode" instance="ComapnyName\ModuleName\Model\Observer\DisabledCcByPincode" />
    </event>
</config>

File Path : /app/code/CompanyName/ModuleName/Model/Observer/DisabledCcByPincode.phpDisabledCcByPincode.php

<?php
namespace CompanyName\ModuleName\Model\Observer;

use \Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;
use Magento\Checkout\Model\Session;

class DisabledCcByPincode implements ObserverInterface
{
    protected $checkoutSession;

    public function __construct(
        \Psr\Log\LoggerInterface $logger,
        Session $checkoutSession
    )
    {
        $this->_logger = $logger;
        $this->checkoutSession = $checkoutSession;
    }
    /**
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(Observer $observer)
    {
        $result          = $observer->getEvent()->getResult();
        $method_instance = $observer->getEvent()->getMethodInstance();
        $quote           = $observer->getEvent()->getQuote();
        //$this->_logger->info($method_instance->getCode());
        /* Disable All payment gateway  exclude Your payment Gateway*/

        var_dump($quote); //This Value coming NULL

        $postcode = $quote->getShippingAddress()->getPostcode();

    }


}

I am not getting quote or any data except $resultvalue. I would like to get quote data in above observer event.

Any help appreciated.

1
  • did you find any solution for this? Jan 31, 2020 at 7:12

4 Answers 4

8

I have fixed an issue by following way,

<?php
namespace CompanyName\ModuleName\Model\Observer;

use \Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;

class DisabledCcByPincode implements ObserverInterface
{
    protected $_cart;

    public function __construct(
        \Psr\Log\LoggerInterface $logger,
        \Magento\Checkout\Model\Cart $_cart
    )
    {
        $this->_logger = $logger;
        $this->_cart = $_cart;
    }
    /**
     *
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
     */
    public function execute(Observer $observer)
    {
        $result          = $observer->getEvent()->getResult();
        $method_instance = $observer->getEvent()->getMethodInstance();
        $quote           = $this->_cart->getQuote(); // Updated Code

        $postcode = $quote->getShippingAddress()->getPostcode();

    }


}
2
0

Try this below code

$quote = $observer->getQuote();

OR

<?php
namespace Marissen\Lease\Observer;

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

use \Magento\Checkout\Model\Session as CheckoutSession;

class AddtoCart implements ObserverInterface {

    /** @var CheckoutSession */
    protected $checkoutSession;

    /**
     * @param CheckoutSession $checkoutSession
     */
    public function __construct(CheckoutSession $checkoutSession) {
        $this->checkoutSession = $checkoutSession;
    }

    public function execute(Observer $observer) {
        /** @var \Magento\Catalog\Model\Product\Interceptor $product */
        $product = $observer->getProduct();

        /** @var \Magento\Quote\Model\Quote  */
        $quote = $this->checkoutSession->getQuote();
    }
}

Source from get cart/quote in Observer

2
  • I had already tried this with no success. Jul 21, 2017 at 5:32
  • Checkoutsession should be workable.or else you need to find your mistake.
    – Vino
    Jul 21, 2017 at 6:00
0

Vishwas Soni,

Please change the code

$postcode = $quote->getShippingAddress()->getPostcode();

to

if($quote && null !== $quote){
  $postcode = $quote->getShippingAddress()->getPostcode();
}

Donot use checkout session,session bcoz magento checkout magento using rest api,So you cannot Magento\Checkout\Model\Session at rest api area

1
  • 1
    Your answer seems to be irrelevant for me. Could you please read my question again and help me if you have any solution. Jul 21, 2017 at 5:31
0

this class app/code/Magento/Payment/Model/Method/AbstractMethod.php is having this dispatch event

in this event magento passes three parameter

[
  'result' => $checkResult,
  'method_instance' => $this,
  'quote' => $quote
]

So in your code you can get quote by below code

public function execute(Observer $observer)
{
    $result          = $observer->getEvent()->getResult();
    $method_instance = $observer->getEvent()->getMethodInstance();
    $quote           = $observer->getQuote(); // You will get Quote by this code

    $postcode = $quote->getShippingAddress()->getPostcode();

}
1
  • But somehow quote value seems to be NULL here. That's what I want to know, why quote is getting NULL here? Jul 22, 2017 at 4:45

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.