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.

3 Answers 3

4

I was experiencing the same issue as you were having. I had to disable paymentmethods for cash on delivery also.

What eventually fixed it was: Move the events.xml file from frontend scope to global scope.

So move events from Vendor_Modulename/etc/frontend/events.xml to: Vendor_Modulename/etc/events.xml. This has something to do with a REST request wich is a different scope than frontend.

I hope this helps

Greetings,

Menno van Muilwijk

1

Move Your app/code/Vendor/Module/etc/frontend/events.xml file TO app/code/Vendor/Module/etc/events.xml

It is working for me.

0

If you're trying to hide your payment method when the cashondelivery one is active, I would use the \Magento\Payment\Model\Method\AbstractMethod::isAvailable() method:

// your payment method

public function __construct(
    $allTheArgsOfAPaymentMethod,
    \Magento\OfflinePayments\Model\CashondeliveryFactory $cashOnDeliveryMethodFactory
) {
    parent::__construct(/* necessary args */);
    $this->cashOnDeliveryMethodFactory = $cashOnDeliveryMethodFactory;
}

public function isAvailable(\Magento\Quote\Api\Data\CartInterface $quote = null)
{
    return !$this->cashOnDeliveryMethodFactory->create()->isAvailable($quote);
}

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.