I created custom payment method (off-line).


And i created customer attribute(On Account Payment Method Applicable) with yes/no.

this field visible in admin side. by default this field will be selected as yes.

Admin user can select no for some of customers.

So based on the customer this payment should be available for front end checkout. Any one have some idea how to do this?

2 Answers 2


In your payment method, you can put logic into the isAvailable method to decide wether or not it is a sutible method for example:

public function isAvailable(CartInterface $quote = null)
        $customerRepository = $objectManager
        $customer = $customerRepository->getById(1);
        $cattrValue = $customer->getCustomAttribute('my_custom_attribute');

        if ($cattrValue !== true) {
            return false; 
        return true;

The details of the customer attribute and using it has been copied from this question for more details: Magento2 How to get custom attribute and its value of a customer?


Let’s create a module. Step 1) Create folders like this.


Step 2) Declare your module


Paste the following code in the above file.

    <?xml version="1.0"?>
    <config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    <module name="Pits_PaymentMethod" setup_version="2.0.0" /></config>

Step 4 ) Create registration file. app/code/Pits/PaymentMethod/registration.php Paste the below code.


Step 5)Run the below command in the command line

php bin/magento module:enable Pits_PaymentMethod

 php bin/magento setup:upgrade

Step 6)Lets create events.xml file


Paste the below code in it.

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="payment_method_is_active">
        <observer name="Pits_PaymentMethod_DisabledPgByCustomergroup" instance="Pits\PaymentMethod\Observer\DisabledPgByCustomergroup" />

Step 7)Lets create observer file


namespace Pits\PaymentMethod\Observer;
use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;
class DisabledPgByCustomergroup implements ObserverInterface
    public function __construct(\Psr\Log\LoggerInterface $logger)
        $this->_logger = $logger;
     * @param \Magento\Framework\Event\Observer $observer
     * @return void
    public function execute(\Magento\Framework\Event\Observer $observer)
        $result          = $observer->getEvent()->getResult();
        $method_instance = $observer->getEvent()->getMethodInstance();
        $quote           = $observer->getEvent()->getQuote();
        /* If Cusomer  group is match then work */
        if (null !== $quote && $quote->getCustomerGroupId() != 4) {
            /* Disable All payment gateway  exclude Your payment Gateway*/
            if ($method_instance->getCode() == 'purchaseorder') {
                $result->setData('is_available', false);
        if($method_instance->getCode() =='purchaseorder'){
        $result->setData('is_available', true);


Step 8)Run this in command line

php bin/magento setup:upgrade
  • I tried this but unfortunately it is not working for me. $paymentMethod = $observer->getEvent()->getMethodInstance()->getCode(); $logger->info('Payment Method: '.$paymentMethod); if ($paymentMethod == "cashondelivery") { $logger->info('Disable Cash on delivery '); $checkResult = $observer->getEvent()->getResult(); $checkResult->setData('is_available', true); } Commented Dec 23, 2020 at 9:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.