Site icon MageComp Blog

How to Restrict Payment Method Based on Shipping selection in Magento 2

How to restrict payment method based on the shipping method in Magento 2

Magento 2 is never failed to meet your Ecommerce store requirements they have tons of preloaded options built-in while customizations can be covered with coding. Considering international businesses many time, it happens that you have to restrict some payment or shipping options for different countries. Because not shipping and payment charges are the same for all locations, depending on distance and amount of transactions it may vary. Adding those charges to the product price is not a solution nor can be bared by store owners. At that time all you need to do is restrict payment methods based on the shipping method.
For example, if your customer selects the “Flat Rate” shipping method, you don’t want your customer to select the “Cash on delivery” payment method and hide it from checkout. There is no such in Magento 2 backend configurations, however, you can try 3rd party Magento Extensions such as Shipping & Payment Restrictions for Customer Groups in Magento 2. But if you want to do it on your own, you have to code for it.
So, we are back with another blog using which you can restrict any payment method based on your customer shipping method selections in Magento 2. Just follow the below steps and you are done.
First, you need to create “di.xml” file inside your extension folder.
app\code\Vendor\Extension\etc\di.xml

<?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="restrict_payment_on_shippingmethod" type="Vendor\Extension\Plugin\Model\MethodList" sortOrder="10"/>
    </type>
</config>

	
    	
	

After that you need to create “MethodList.php” file inside your extension folder and add below code inside that file.
app\code\Vendor\Extension\Plugin\Model\MethodList.php

namespace Vendor\Extension\Plugin\Model;
 
class MethodList
{
	public function afterGetAvailableMethods(
        \Magento\Payment\Model\MethodList $subject,
    	$availableMethods,
    	\Magento\Quote\Api\Data\CartInterface $quote = null
	) {
    	$shippingMethod = $this->getShippingMethodFromQuote($quote);
    	foreach ($availableMethods as $key => $method) {
        	// Here we will hide CashonDeliver method while customer select FlateRate Shipping Method
            if(($method->getCode() == 'cashondelivery') && ($shippingMethod == 'flatrate_flatrate')) {
                unset($availableMethods[$key]);
        	}
    	}
 
    	return $availableMethods;
	}
 
	/**
 	* @param \Magento\Quote\Api\Data\CartInterface $quote
 	* @return string
 	*/
	private function getShippingMethodFromQuote($quote)
	{
    	if($quote) {
        	return $quote->getShippingAddress()->getShippingMethod();
    	}
 
    	return '';
	}
}

That’s it. You are free to manipulate this code according to your need to restrict and hide multiple payment methods based on your customer’s shipping method selection. Also, check out Shipping and Payment Method per Customer Group Extension which allows the store owner to manage shipping and payment method for the specific customer groups.

Lastly, if you found this blog helpful, don’t forget to share it with your colleagues and Magento Friends and Let us know if you are facing any issue while implementing this code.
Happy Coding!

Exit mobile version