Successful e-commerce store demands to provide better user experience to grow their sales by offering and managing various payment methods at checkout page according to the needs of the specific customer as every customer has different demands. For example, the merchant which offers the cash on delivery for the local region may not be offered to international customers; In some cases, payment gateway requires certain taxes and fees for the different part which cannot be borne by the merchant.TO solve those issues, it has become vital for Magento store owners to optimize their payment methods according to different customer groups
To enable the payment process between customers and Magento store, we can restrict the payment methods by different customers groups by following the steps below.
1. Create an events.xml in : /app/code/Magemonkeys/PaymentMethod/etc
1 2 3 4 5 6 |
<?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="Magemonkeys_PaymentMethod_DisabledPgByCustomergroup" instance="Magemonkeys\PaymentMethod\Observer\DisabledPgByCustomergroup" /> </event> </config> |
2. Create an DisabledPgByCustomergroup.php in : /app/code/Magemonkeys/PaymentMethod/Observer
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
<?php namespace Magemonkeys\PaymentMethod\Observer; use Magento\Framework\Event\ObserverInterface; use Magento\Framework\App\Request\DataPersistorInterface; use Magento\Framework\App\ObjectManager; class DisabledPgByCustomergroup implements ObserverInterface { public function execute(\Magento\Framework\Event\Observer $observer) { $helper = \Magento\Framework\App\ObjectManager::getInstance()->get('Magemonkeys\PaymentMethod\Helper\Data'); if($helper->isEnable()){ $customer_grp = $helper->isActiveGroup(); $result = $observer->getEvent()->getResult(); $method_instance = $observer->getEvent()->getMethodInstance(); $quote = $observer->getEvent()->getQuote(); if (null !== $quote && $quote->getCustomerGroupId() != 'CUSTOMER_GROUP_ID') { if ($method_instance->getCode() == 'PAYMENT_METHOD_CODE') { $result->setData('is_available', false); } } } } } ?> |
By following the above steps, you can restrict the payment methods in Magento 2 store.
If you have any doubt regarding the above method, feel free to ask me the comment section.
I would be happy to help you.
[crayon-63e08e2517144890927196/] Using above fucntion Images can be imported directly from...
Override view block using di.xml and add the below code...
You can check a list of called layout XML for...
Follow the below steps to install and set up PWA...
If you want to remove all leading zero's from order,...
Let our Magento expert connect to discuss your requirement.
We offer Magento
certified developers.
Our Magento clientele
is 500+.
We sign NDA for the
security of your projects.
We’ve performed 100+
Magento migration projects.
Free quotation
on your project.
Three months warranty on
code developed by us.