Magento 2 Disable Payment Method in php Using payment_method_is_active Observer

I recently came across a situation where I needed to be able to turn off payment method on checkout based on customer information on a Magneto 2 site.

The best solution seemed to be to tie into the payment_method_is_active event. I left the payment method turned on in the admin for ease of use and clarity to the store owner and had my observer turn off the method based on criteria.

The code structure you need is pretty simple
app/code/Evermore/Payment is the root of my extension.

As with all extension, add the registration.php file

registration.php

<?php

\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Evermore_Payment',
__DIR__
);

Then add the module.xml file.

etc/module.xml

<?xml version="1.0"?>
 
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Evermore_Payment" setup_version="0.0.1">
    </module>
</config>

Now an events.xml file to add the observer call.

etc/events.xml

<?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="Evermore_Payment_Method_Is_Active" instance="Evermore\Payment\Observer\DisablePaymentMethods" />
    </event>
</config>

Now the structure is in place to call your Observer.  Finally add the Observer file.

Observer/DisablePaymentMethods.php

<?php

/**
 * Disables Payment Method of PO if the customer doesn't have approved billing terms.
 *
 */

namespace Evermore\Payment\Observer;

use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
use Psr\Log\LoggerInterface;

class DisablePaymentMethods implements ObserverInterface {
  protected $_customerInterface;
  protected $_logger;

  public function __construct(
    \Magento\Customer\Api\CustomerRepositoryInterface $customerInterface,
    LoggerInterface $logger
  ) {
    $this->_customerInterface = $customerInterface;
    $this->_logger = $logger;
  }

  /**
   * @param Observer $observer
   *
   * @return void
   */
  public function execute(Observer $observer) {
    //Replace this code with your own checks.  This code is checking a customer attribute.  If they are not approved for billing terms, the purchaseorder method is turned off.
    $result = $observer->getEvent()->getResult();
    $method_instance = $observer->getEvent()->getMethodInstance()->getCode();
    $quote = $observer->getEvent()->getQuote();
    if($method_instance == 'purchaseorder') {
      $customer = $this->_customerInterface->getById($quote->getCustomer()->getId());
      if($customer->getCustomAttribute('approved_billing_terms')->getValue() == 0) {
        $result->setData('is_available', false);
      }
    }
  }
}

Put this code in your extension, run the standard code

php bin/magento setup:upgrade
php bin/magento setup:di:compile

And you’re done.

 

Leave a Reply

Your email address will not be published. Required fields are marked *