How to disable payment method for certain customer groups in Magento 2. you might often need to disable some payment methods for some customer groups to provide a particular payment method for VIP customers. when such time arrives, you would want to do it the right way.
Magento 2 Default functionality Customer groups determine which discounts are available and the tax class that is associated with the group. The default customer groups are General, Not Logged In, and Wholesale.
Also Like this: Payment Restrictions Extension for Magento 2
How to disable a payment method for a customer group in Magento 2, you’ll first need to create a simple Module.
Here we are using Dolphin as Vendor name and MyModule as the name of a module. You can change this according to your Vendor and Module name.
Create registration.php file in the app/code/Dolphin/MyModule folder with the following code.
<?php \Magento\Framework\Component\ComponentRegistrar::register( \Magento\Framework\Component\ComponentRegistrar::MODULE, 'Dolphin_MyModule', __DIR__ );
Create a module.xml file in the app/code/Dolphin/MyModule/etc folder with the following code.
<?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="Dolphin_MyModule" setup_version="1.0.0"></module> </config>
Create an events.xml file in the folder app/code/Dolphin/MyModule/etc and use event payment_method_is_active which will be called Payment method active.
<?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="dolphin_payment_disable" instance="Dolphin\Mymodule\Observer\PaymentMethodDisable" /> </event> </config>
Here you declare an observer named PaymentMethodDisable that will handle the event payment_method_is_active which Magento dispatches every time payment method is active.
Now we need to create an Observer as defined in the events.xml file
Read this: Get All Payment Methods in Magento 2
Now, we need to create the Observer PaymentMethodDisable.php file in the app/code/Dolphin/MyModule/Observer with the following code.
<?php namespace Dolphin\MyModule\Observer; use Magento\Framework\Event\Observer; use Magento\Framework\Event\ObserverInterface; class PaymentMethodDisable implements ObserverInterface { /** * @var \Magento\Customer\Model\Session */ protected $_customerSession; /** * @param \Magento\Customer\Model\Session $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); } } } } }
Session will extend core class Magento\Framework\Session\SessionManager
to handle the session.
Get the customer details- $this->_customerSession->getCustomer()->getId(); will give you current customer id, You can also get customer’s other details $customerData->getData().
Now setup the module.To setup module first time, we execute below command:
[dt_highlight color="" text_color="" bg_color=""]php bin/magento s:up[/dt_highlight] [dt_highlight color="" text_color="" bg_color=""]php bin/magento s:s:d -f[/dt_highlight] [dt_highlight color="" text_color="" bg_color=""]php bin/magento c:c[/dt_highlight]
I hope this blog is easy to understand about how to disable payment method for certain customer groups in Magento 2.
if you are facing any issues during implementation, use the comment section below!
Happy Coding!
Click one of our contacts below to chat on WhatsApp