How-to-Restrict-Payment-Methods-by-Attribute-or-Category

How to Restrict Payment Methods by Attribute/Category in Magento 2?

 

In this blog, we are going to teach you how to restrict payment methods by attribute or category in Magento 2.

It is highly essential to learn to restrict payment methods. As with this you can provide a seamless checkout experience to your customers and elevate the sales of specific products or categories.

This will lead to elevated customer satisfaction and effective management of payment methods.

Follow these steps and accurately restrict payment methods by attribute or category.

Step-by-step Process to Restrict Payment Methods by Attribute or Category

Take a look at \Magento\Payment\Model\MethodList. The constructor has an $additionalChecks argument allowing you to pass in additional custom checks to restrict specific payment methods. 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">

<!-- Specify a custom check where `your_custom_restriction` is a unique value -->

<type name="Magento\Payment\Model\MethodList">

<arguments>

<argument name="additionalChecks" xsi:type="array">

<item name="your_custom_restriction" xsi:type="string">your_custom_restriction</item>

</argument>

</arguments>

</type>

<!-- Here we specify the class responsible for handling the check specified in the previous block -->

<type name="Magento\Payment\Model\Checks\SpecificationFactory">

<arguments>

<argument name="mapping" xsi:type="array">

<item name="your_custom_restriction" xsi:type="object">Vendor\PaymentRestriction\Model\YourCustomRestriction</item>

</argument>

</arguments>

</type>

</config>

In etc/catalog_attributes.xml we also need to tell Magento to load our custom attribute value in the product object when we call getProduct() on the quote item.


<?xml version="1.0"?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Catalog:etc/catalog_attributes.xsd">

<group name="quote_item">

<attribute name="disable_some_method"/>

</group>

</config>

The class Model/YourCustomRestriction.php will make the decision to include/exclude the payment method. Our isApplicable() method should return true or false if you want to allow or block the payment method. In the below mentioned example, first, I checked if the payment is Braintree (if not, we immediately return true). If it is, I iterated over all items in the cart, and if one of the items has the attribute disable_some_method set to yes, we break from the loop and return true, which in turn returns false, preventing the payment method from being used.


<?php

declare(strict_types=1);

namespace Vendor\PaymentRestriction\Model;

use Magento\Payment\Model\Checks\SpecificationInterface;

use Magento\Payment\Model\MethodInterface;

use Magento\Quote\Model\Quote;

use PayPal\Braintree\Model\Ui\ConfigProvider;

 

class YourCustomRestriction implements SpecificationInterface

{

/**

* @param MethodInterface $paymentMethod

* @param Quote $quote

* @return bool

*/

public function isApplicable(MethodInterface $paymentMethod, Quote $quote)

{

if ($paymentMethod->getCode() === ConfigProvider::CODE && $this->hasRestrictedItems($quote)) {

return false;

}

return true;

}

 

/**

* @param Quote $quote

* @return bool

*/

private function hasRestrictedItems(Quote $quote)

{

$hasRestrictedItems = false;

/** @var \Magento\Quote\Api\Data\CartItemInterface $item */

foreach ($quote->getAllItems() as $item) {

if ($item->getProduct()->getDisableSomeMethod()) {

$hasRestrictedItems = true;

break;

}

}

return $hasRestrictedItems;

}

}

Conclusion

Just like that you can restrict payment methods by attribute or category in Magento 2.

To get 360-degree control over payment methods for your store, you can opt for our Magento 2 Payment Restrictions Extension.

Also, you can refer to this blog if you want to disable the payment method in your Magento 2 store programmatically.

Explore the highly advanced features we provide by checking out our front & back-end demos.Also, feel free to contact us anytime for any professional assistance required or if you are looking for extension customization services.

Tags