Skip to main content
added 20 characters in body
Source Link
Karthick N
  • 366
  • 1
  • 3
  • 9

Model observer file <?php namespace Exp\Test\Model\Observer; use Magento\Framework\Event\ObserverInterface; use Exception;

 <?php
    namespace Exp\Test\Model\Observer;
    use Magento\Framework\Event\ObserverInterface;
    use Exception;

class PaymentActive implements ObserverInterface {
    
    public function __construct (
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        $this->_logger = $logger;
        $this->scopeConfig = $scopeConfig;
        $this->storeManager = $storeManager;
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $this->testlog = $logger;
    }
    public function execute(\Magento\Framework\Event\Observer $observer) {
    $event           = $observer->getEvent();
        $method          = $event->getMethodInstance();
        $result          = $event->getResult();
        $currencyCode    = $this->storeManager->getStore()->getCurrentCurrencyCode();
        $this->testlog->info($method->getCode());
        if($method->getCode() == 'cashondelivery'){
              $result->setData( 'is_available', false);
        }
    }
}

Model observer file <?php namespace Exp\Test\Model\Observer; use Magento\Framework\Event\ObserverInterface; use Exception;

class PaymentActive implements ObserverInterface {
    
    public function __construct (
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        $this->_logger = $logger;
        $this->scopeConfig = $scopeConfig;
        $this->storeManager = $storeManager;
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $this->testlog = $logger;
    }
    public function execute(\Magento\Framework\Event\Observer $observer) {
    $event           = $observer->getEvent();
        $method          = $event->getMethodInstance();
        $result          = $event->getResult();
        $currencyCode    = $this->storeManager->getStore()->getCurrentCurrencyCode();
        $this->testlog->info($method->getCode());
        if($method->getCode() == 'cashondelivery'){
              $result->setData( 'is_available', false);
        }
    }
}

Model observer file

 <?php
    namespace Exp\Test\Model\Observer;
    use Magento\Framework\Event\ObserverInterface;
    use Exception;

class PaymentActive implements ObserverInterface {
    
    public function __construct (
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        $this->_logger = $logger;
        $this->scopeConfig = $scopeConfig;
        $this->storeManager = $storeManager;
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $this->testlog = $logger;
    }
    public function execute(\Magento\Framework\Event\Observer $observer) {
    $event           = $observer->getEvent();
        $method          = $event->getMethodInstance();
        $result          = $event->getResult();
        $currencyCode    = $this->storeManager->getStore()->getCurrentCurrencyCode();
        $this->testlog->info($method->getCode());
        if($method->getCode() == 'cashondelivery'){
              $result->setData( 'is_available', false);
        }
    }
}
Source Link
Karthick N
  • 366
  • 1
  • 3
  • 9

Payment method is active observer result not working in magento 2

Actually I have implemented the custom payment method in magento 2 like this article http://webhintsexplorer.com/custom-payment-method-magento-2/. Its working fine properly. I tried to hide my custom payment method using observer 'payment_method_is_active'. These are my observer code

<?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='Exp\Test\Model\Observer\PaymentActive' instance='Exp\Test\Model\Observer\PaymentActive' />
  </event>
</config>

Model observer file <?php namespace Exp\Test\Model\Observer; use Magento\Framework\Event\ObserverInterface; use Exception;

class PaymentActive implements ObserverInterface {
    
    public function __construct (
        \Psr\Log\LoggerInterface $logger,
        \Magento\Framework\App\Config\ScopeConfigInterface $scopeConfig,
        \Magento\Store\Model\StoreManagerInterface $storeManager
    ) {
        $this->_logger = $logger;
        $this->scopeConfig = $scopeConfig;
        $this->storeManager = $storeManager;
        $writer = new \Zend\Log\Writer\Stream(BP . '/var/log/test.log');
        $logger = new \Zend\Log\Logger();
        $logger->addWriter($writer);
        $this->testlog = $logger;
    }
    public function execute(\Magento\Framework\Event\Observer $observer) {
    $event           = $observer->getEvent();
        $method          = $event->getMethodInstance();
        $result          = $event->getResult();
        $currencyCode    = $this->storeManager->getStore()->getCurrentCurrencyCode();
        $this->testlog->info($method->getCode());
        if($method->getCode() == 'cashondelivery'){
              $result->setData( 'is_available', false);
        }
    }
}

The observer are triggering in checkout page when I log the payment method code using the code

$this->testlog->info($method->getCode());

in execute method. I got the result

2016-04-22T08:25:19+00:00 INFO (6): cashondelivery
2016-04-22T08:25:19+00:00 INFO (6): checkmo
2016-04-22T08:25:19+00:00 INFO (6): paypal_express_bml
2016-04-22T08:25:19+00:00 INFO (6): payflow_express
2016-04-22T08:25:19+00:00 INFO (6): paypal_express
2016-04-22T08:25:19+00:00 INFO (6): payflow_express_bml

I can't get my custom payment method code in the log. Please suggest is I am missing any settings in custom payment method model or any where?. Then I tried to hide the core payment method cash on delivery using the method code 'cashondelivery' in the code.

if($method->getCode() == 'cashondelivery'){

That is also not working. Any help is much appreciated.