In this technical blog, I will explain you how to disable payment method for certain customer groups in Magento 2.
In e-commerce store, payment method system is one of the most important cores of Magento 2 store. It’s affect on your store system for increase your sales, profit, etc. Sometimes, store owner wants to restrict specific payment methods based on the customer groups. For example, VIP customer groups can able to use CCAvenue payment methods and other payment methods can’t able to access.
So, let’s now we will learn how to disable payment method for certain customer groups.
For create simple module, you can click here.
You may like this also :
So, let’s now we will learn how to disable payment method for certain customer groups
For create simple module, you can click here.
1) First of all, create registration.php at app/code/RH/DisablePaymentMethod/
<?php
/**
* Created By : Rohan Hapani
*/
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'RH_DisablePaymentMethod',
__DIR__
);
2) create module.xml at app/code/RH/DisablePaymentMethod/etc/
<?xml version="1.0"?>
<!--
/**
* Created By : Rohan Hapani
*/
-->
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="RH_DisablePaymentMethod" setup_version="1.0.0" schema_version="1.0.0" />
</config>
3) create events.xml at app/code/RH/DisablePaymentMethod/etc/
<?xml version="1.0"?>
<!--
/**
* Created By : Rohan Hapani
*/
-->
<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="rh_payment_disable" instance="RH\DisablePaymentMethod\Observer\PaymentMethodDisable" />
</event>
</config>
4) create PaymentMethodDisable.php at app/code/RH/DisablePaymentMethod/Observer/
<?php
/**
* Created By : Rohan Hapani
*/
namespace RH\DisablePaymentMethod\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);
}
}
}
}
}
Now, all things of module are ready. So, it’s time for setup module. To setup module first time, we need to execute this below command :
php bin/magento s:up
php bin/magento s:s:d or php bin/magento s:s:d -f
php bin/magento c:c
With the above code, you can set restrictions which you want in the payment methods for certain customer groups in Magento 2 store.
I hope this blog is easy to understand about how to disable payment method for certain customer groups in Magento 2. In case, I missed anything or need to add some information, always feel free to leave a comment in this blog, I’ll get back with proper solution