Magento2: Disable Payment Method Programmatically

Payment Method Magento 2

We developers often need to disable specific payment methods in certain scenarios. However, we cannot disable those payment methods from the configuration because our website needs them in a different scenario.

We will check how we can achieve such functionality to enable/disable payment methods in different situations. We will use the event payment_method_is_active dispatched by the Magento_Payment module.

Disable Payment Method Programmatically In Magento 2

Create an event.xml file to define the observer.

<?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="enable_disable_cod_payment" instance="SbDevBlog\Payment\Observer\HandlePaymentMethods" />
    </event>
</config>

Create an observer class HandlePaymentMethods defined in the event.xml

<?php
<code role="textbox" aria-multiline="true" aria-label="Code" class="block-editor-rich-text__editable rich-text" style="white-space: pre-wrap; min-width: 1px;" contenteditable="true">/**
 * @copyright Copyright (c) sbdevblog (http://www.sbdevblog.com)
 */</code>
namespace SbDevBlog\Payment\Observer;

use Magento\Framework\Event\ObserverInterface;

class HandlePaymentMethods implements ObserverInterface
{
    /**
     * Handle payment_method_is_active event
     *
     * @param \Magento\Framework\Event\Observer $observer
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
       if($observer->getEvent()->getMethodInstance()->getCode()=="payment_method_code"){
            $checkResult = $observer->getEvent()->getResult();
             // pass true to enable & false to disable payment method
              $checkResult->setData('is_available', false);
            return;
        }
    }
}

I have disabled Cash On Delivery Payment Method in the example given below.

<?php
/**
 * @copyright Copyright (c) sbdevblog (http://www.sbdevblog.com)
 */

namespace SbDevBlog\Payment\Observer;

use Magento\Framework\Event\ObserverInterface;

class HandlePaymentMethods implements ObserverInterface
{
    /**
     * Handle payment_method_is_active event
     *
     * @param \Magento\Framework\Event\Observer $observer
     */
    public function execute(\Magento\Framework\Event\Observer $observer)
    {
       if($observer->getEvent()->getMethodInstance()->getCode()=="cashondelivery"){
             $checkResult = $observer->getEvent()->getResult();
             // pass true to enable & false to disable payment method
              $checkResult->setData('is_available', false);
            return;
        }
    }
}

That’s it. It is easy to Disable Payment Method Programmatically In Magento 2.

Thanks for reading the post; please subscribe and share with your connections. Also, please use the comment box for your valuable suggestions.

You can go ahead and visit the post to disable the shipping method programmatically.

Download Source Code

Note: Please verify the code of this blog and the relevant git repository before using it in production.
sb dev blog adobe commece Magento 2

🙂 HAPPY CODING 🙂

One thought on “Magento2: Disable Payment Method Programmatically

Leave a Reply

Your email address will not be published. Required fields are marked *