How to Disable Payment Method for Certain Customer Groups

How to Disable Payment Method for Certain Customer Groups in Magento 2?

If you’re starting an eCommerce store, you might often need to disable some payment methods for some customer groups.

For instance, you might want to provide a particular payment method (i.e. CCAvenue) for VIP customers.

So, when such time arrives, you would want to do it the right way.

And today, we’re going to show you exactly how to disable a payment method for certain customer groups in a Magento 2 based eCommerce store.

Steps to Disable a Payment Method for Certain Customer Groups

In order to learn how to disable a payment method for a customer group in Magento 2, you’ll first need to create a simple Module.

Now, we have already created an easy-to-follow tutorial on how to create a simple module in Magento 2. So you can refer to that.

Once you’ve created a simple module, it’s time to go through the process of disabling a payment method step-by-step.

Step 1:

First of all, we will create a registration.php file at app/code/MD/DisablePaymentMethod/

Now, copy the following code in the file.

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'MD_DisablePaymentMethod',
    __DIR__
);

Step 2:

Next, we will create a module.xml file in app/code/MD/DisablePaymentMethod/etc/ directory.

Now, copy the below code in the file.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="MD_DisablePaymentMethod" setup_version="1.0.0" schema_version="1.0.0"/>
</config>

Step 3:

The next step is to create an events.xml file in the app/code/MD/DisablePaymentMethod/etc/ and copy the below 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="md_payment_disable" instance="MD\DisablePaymentMethod\Observer\PaymentMethodDisable" />
    </event>
</config>

Step 4:

Next, we will create a PaymentMethodDisable.php file in the app/code/MD/DisablePaymentMethod/Observer/

Now, copy the following code in the file.

<?php
namespace MD\DisablePaymentMethod\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class PaymentMethodDisable implements ObserverInterface {
    protected $_customerSession;
    public function __construct(
       \Magento\Customer\Model\Session $customerSession
    ) {
       $this->_customerSession = $customerSession;
    }
    public function execute(Observer $observer) {
       $payment_method_code = $observer->getEvent()->getMethodInstance()->getCode();
       if ($payment_method_code == 'payroll') {
           $result = $observer->getEvent()->getResult();
           if ($this->_customerSession->isLoggedIn()) {
               $customerGroupId = $this->_customerSession->getCustomer()->getGroupId();
               if ($customerGroupId == 1) {
                   $result->setData('is_available', false);
               }
           }
       }
    }
}

Now, we’re almost done here. The only thing that remains is setting up the module.

To set up the module, you need to execute the following commands:

php bin/magento s:up
php bin/magento s:s:d or php bin/magento s:s:d -f
php bin/magento c:c

And it’s done!

“Also Read: How to Restrict Some Payment Methods in Magento 2?

Concluding Thoughts…

With the above process, you can easily set custom restrictions that you want in the payment methods for certain customer groups.

We hope that you found this tutorial easy and helpful.

If you want more control over your customer group you can use our Magento 2 Customer Group Restrictions Extension. With it you can modify the visibility of products, catalog, CMS pages, product price, and payment and shipment methods for every customer group and set different catalog visibility restriction.

Check our Front-end and back-end demos to explore more exciting features of this extension.

Also, you can refer this Magento 2 Customer Group Restrictions Extension’s FAQ Page for most common questions and it’s answers.

However, if you ever need professional assistance, you can certainly contact us for help. And in case we missed anything, please do let us know in the comments below.


Also read:

How to Restrict Payment Methods by Attribute/Category in Magento 2

How to Restrict Some Payment Methods in Magento 2

How to Get the Payment Method Title of an Order in Magento 2

How to Create Custom Payment Method in Magento 2

How to Setup Cash on Delivery (COD) Payment Method in Magento 2

How to Get List of Payment Methods using Command Line in Magento 2

How to Disable Payment Method for Certain Customer Groups in Magento 2

Tags