restrict_payment_methods restrict_payment_methods

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="MagentoPaymentModelMethodList">
        <plugin name="hide_payment_methods" type="CodextblogRestrictpaymentPluginPaymentModelMethodList" 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 CodextblogRestrictpaymentPluginPaymentModel;

use MagentoQuoteApiDataCartInterface;
use MagentoCustomerModelSession;

/**
 * Class MethodList
 * CodextblogRestrictpaymentPluginPaymentModel
 */
class MethodList
{
    
	/**
     * @var Session
     */
    protected $customerSession;

    /**
     * MethodList constructor.
     *
     * @param Session $customerSession
     */
    public function __construct(
        Session $customerSession
    ) {
        $this->customerSession = $customerSession;
    }

    /**
     * @param MagentoPaymentModelMethodList $subject
     * @param $availableMethods
     * @param CartInterface|null                $quote
     */
    public function afterGetAvailableMethods(
        MagentoPaymentModelMethodList $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 CodextblogRestrictpaymentPluginPaymentModel;

use MagentoQuoteApiDataCartInterface;

/**
 * Class MethodList
 * CodextblogRestrictpaymentPluginPaymentModel
 */
class MethodList
{
    
	/**
     * @param MagentoPaymentModelMethodList $subject
     * @param $availableMethods
     * @param CartInterface|null                $quote
     */
    public function afterGetAvailableMethods(
        MagentoPaymentModelMethodList $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.

Written by

Chirag

Chirag is a Founder & eCommerce Technology Leader at MageArmy. He leads the eCommerce development team and drives the company’s technology vision, helping businesses build, scale, and optimize high-performing online stores. With years of experience in eCommerce development, he has worked with businesses across industries to solve complex technical challenges and create scalable digital commerce solutions. He loves sharing practical insights about eCommerce development, Magento, Adobe Commerce, and the latest trends in digital commerce to help online businesses grow and succeed.