How to Conditionally Restrict Payment Methods in Magento 2
Magento 2 provides many in-built payment methods like Paypal, Authorize.net, Check / Money Order, Bank transfer, etc. Sometimes as per client requirements and client’s business nature, you have to conditionally restrict payment methods in Magento 2.
For example, the store owner would like to allow free shipping to only paid orders. In that case, you have to hide other offline payment methods if the customer selects free shipping as a shipping method. Another scenario is the store owner would like to allow offline payment methods to their B2B customers only. In that case, you have to hide all offline payment methods for the general customer group. There are many such situations where your client wants you to conditionally restrict payment methods in Magento 2. In this post, we will see how to conditionally restrict payment methods based on shipping method selection and customer group.
Getting Started: Development
Step 1: Create di.xml file under app/code/Codextblog/Restrictpayment/etc
directory to declare after plugin. Open that file and add below content.
<?xml version="1.0"?> <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd"> <type name="Magento\Payment\Model\MethodList"> <plugin name="hide_payment_methods" type="Codextblog\Restrictpayment\Plugin\Payment\Model\MethodList" sortOrder="1"/> </type> </config>
Restrict Payment method by customer group
Step 2: Create MethodList.php file under app/code/Codextblog/Restrictpayment/Plugin/Payment/Model
directory to define after plugin. Open that file and add below content.
<?php namespace Codextblog\Restrictpayment\Plugin\Payment\Model; use Magento\Quote\Api\Data\CartInterface; use Magento\Customer\Model\Session; /** * Class MethodList * Codextblog\Restrictpayment\Plugin\Payment\Model */ class MethodList { /** * @var Session */ protected $customerSession; /** * MethodList constructor. * * @param Session $customerSession */ public function __construct( Session $customerSession ) { $this->customerSession = $customerSession; } /** * @param \Magento\Payment\Model\MethodList $subject * @param $availableMethods * @param CartInterface|null $quote */ public function afterGetAvailableMethods( \Magento\Payment\Model\MethodList $subject, $availableMethods, CartInterface $quote = null ) { $customerGroupId = 2; if ($this->customerSession->isLoggedIn() && $this->customerSession->getCustomerData()->getGroupId() == $customerGroupId) { $allowedPaymentMethodCode = ['paypal_express']; foreach ($availableMethods as $key => $method) { if (!in_array($method->getCode(), $allowedPaymentMethodCode)) unset($availableMethods[$key]); } } return $availableMethods; } }
In above example, we have allowed only Paypal payment method to ‘wholesale’ customer group (Id: 2) and restricted all other payment methods.
Restrict Payment method by shipping method selection
Step 2: Create MethodList.php file under app/code/Codextblog/Restrictpayment/Plugin/Payment/Model
directory to define after plugin. Open that file and add below content.
<?php namespace Codextblog\Restrictpayment\Plugin\Payment\Model; use Magento\Quote\Api\Data\CartInterface; /** * Class MethodList * Codextblog\Restrictpayment\Plugin\Payment\Model */ class MethodList { /** * @param \Magento\Payment\Model\MethodList $subject * @param $availableMethods * @param CartInterface|null $quote */ public function afterGetAvailableMethods( \Magento\Payment\Model\MethodList $subject, $availableMethods, CartInterface $quote = null ) { $allowedMethodCode = ['paypal_express','authorizenet_directpost']; $selectedShippingMethod = $this->getShippingMethod($quote); foreach ($availableMethods as $key => $method) { if ($selectedShippingMethod == 'freeshipping' && !in_array($method->getCode(), $allowedMethodCode)) unset($availableMethods[$key]); } return $availableMethods; } }
In Above example, we have restricted all offline payment methods when customer select Free shipping as a shipping method.
Leave a Comment
(0 Comments)
Useful Magento 2 Articles
Author Info
Chirag
Connect With Me