How to Conditionally Restrict Payment Methods in Magento 2

restrict_payment_methods

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.

Want to ask a question or leave a comment?

Leave a Comment

(0 Comments)

All the comments are goes into moderation before approval. Irrelevant comment with links directly goes to spam. Your email address will not be published.

Was this post helpful? Please support Us!

Follow us on twitter or Like us on facebook.

 


To Avoid Spam Downloads, We Want Your Email

We will send you download link right
away. Please submit form below.
SEND ME DOWNLOAD LINK
Close

Increase Your
Magento 2
Knowledge

Get Weekly Tutorial
to your Inbox
Subscribe Me
close-link
Subscribe Here