How to Disable Payment Method Programmatically in Magento 2

How to Disable Payment Method Programmatically in Magento 2

Hello Magento Folks?,

What’s Up! Welcome to the Magento Tutorial blog. Today will help you How to Disable Payment Method Programmatically in Magento 2. Also, go through the previously placed article How to Install Magento SUPEE 11346 [With or Without SSH]

Introduction:

The online stores are providing many methods for payments at checkout. In providing many payment method options many times the store is screwed up with managing all payment methods and the costing is not affordable by the store owners. Always there should be stability in between offering the payment methods and maintaining customer experience in your store.

To overcome these problems the store owner can disable payment methods programmatically in Magento 2. One can also restrict the payment method on the base of customer groups, shipping parameters, product/order attributes, etc. Let’s Do It?

Steps to Disable Payment Method Programmatically in Magento 2:

Step 1: add events.xml in the following path

app\code\Vendor\Extension\etc\events.xml

Then add following 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="disable_payment" instance="Vendor\Extension\Observer\Paymentmethoddisable" />
    </event>
</config>

Step 2: Now add Paymentmethoddisable.php in following path

app\code\Vendor\Extension\Observer\Paymentmethoddisable.php

Then add following code

<?php

namespace Vendor\Extension\Observer;

use Magento\Framework\Event\ObserverInterface;


class Paymentmethoddisable implements ObserverInterface
{
    public function execute(\Magento\Framework\Event\Observer $observer)
    {

        if($observer->getEvent()->getMethodInstance()->getCode()=="checkmo"){  // Here you can replace required payment method code
            $checkResult = $observer->getEvent()->getResult();
            $checkResult->setData('is_available', false); 
        }
    }
}

Conclusion:

Hence, after the implementation of the given above steps, you will be easily able to How to Disable Payment Method Programmatically in Magento 2. If any difficulties then comment down in the comment section below. Don’t forget to share the article with your friends.

Happy Coding! 

Previous Article

How to Write SEO Friendly Content (The Guide for Beginners )

Next Article

How to Implement WebP Images Extension in Magento 2?

Write a Comment

Leave a Comment

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

Get Connect With Us

Subscribe to our email newsletter to get the latest posts delivered right to your email.
Pure inspiration, zero spam ✨