1

How to restrict a selected payment method in Magento 2?

enter image description here

Event: 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="bg_customer_group_payment_filters" instance="\Bg\PaymentRestriction\Observer\PaymentMethodAvailable" />
    </event>
</config>

Observer: Bg\PaymentRestriction\Observer\PaymentMethodAvailable.php

<?php

namespace Bg\PaymentRestriction\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;
use Bg\PaymentRestriction\Helper\Data as DataHelper;

class PaymentMethodAvailable implements ObserverInterface
{
    /**
     * @var \Magento\Customer\Model\Session
     */
    private $customerSession;
    /**
     * @var \Bg\PaymentRestriction\Helper\Data
     */
    private $dataHelper;

    /**
     * @var \Magento\Customer\Api\GroupRepositoryInterface
     */
    private $groupRepository;

    public function __construct(
        \Psr\Log\LoggerInterface $logger,
        DataHelper $dataHelper
    ) {
        $this->_logger = $logger;
        $this->dataHelper = $dataHelper;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $result          = $observer->getEvent()->getResult();
        $method_instance = $observer->getEvent()->getMethodInstance();
        $quote           = $observer->getEvent()->getQuote();
        $this->_logger->info($method_instance->getCode());
        $activeRule = $this->dataHelper->getPaymentMethod();
        /* Enable All payment gateway  exclude Your payment Gateway*/
        if ($method_instance->getCode() == 'checkmo') {
            $result->setData('is_available', false);
        } else {
            $result->setData('is_available', true);
        }
    }
}

I got a value like this in frontend.

enter image description here

9
  • Have you get selected method in the observer file? Commented Nov 18, 2019 at 11:23
  • Any system log or exception log? Because, this code is working. Commented Nov 18, 2019 at 11:46
  • it may work for single payment method but i was r run it on for loop its not disable a payment method Commented Nov 18, 2019 at 11:49
  • Can you tell how are you looping it while multiple payments
    – Piyush
    Commented Nov 18, 2019 at 11:59
  • No need to use the else part as by default it will show other payment methods. You only need to set is_available false for the methods which you dont want to show. Commented Nov 18, 2019 at 12:01

2 Answers 2

1

Use this code:

<?php

namespace Bg\PaymentRestriction\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;
use Bg\PaymentRestriction\Helper\Data as DataHelper;

class PaymentMethodAvailable implements ObserverInterface
{
    /**
     * @var \Magento\Customer\Model\Session
     */
    private $customerSession;
    /**
     * @var \Bg\PaymentRestriction\Helper\Data
     */
    private $dataHelper;

    /**
     * @var \Magento\Customer\Api\GroupRepositoryInterface
     */
    private $groupRepository;

    public function __construct(
        \Psr\Log\LoggerInterface $logger,
        DataHelper $dataHelper
    ) {
        $this->_logger = $logger;
        $this->dataHelper = $dataHelper;
    }

    public function execute(\Magento\Framework\Event\Observer $observer)
    {
        $result          = $observer->getEvent()->getResult();
        $method_instance = $observer->getEvent()->getMethodInstance();
        $quote           = $observer->getEvent()->getQuote();
        $this->_logger->info($method_instance->getCode());
        $activeRule = $this->dataHelper->getPaymentMethod();
        $disable_payment_methods = ['checkmo', 'purchaseorder', 'banktransfer']; // Get Method from your Restrictions
        /* Enable All payment gateway  exclude Your payment Gateway*/
        if (in_array($method_instance->getCode(), $disable_payment_methods)) {
            $result->setData('is_available', false);
        } else {
            $result->setData('is_available', true);
        }
    }
}
1

Try to do with or condition. Like this below way :

if ($method_instance->getCode() == 'checkmo' || $method_instance->getCode() == "purchaseorder") {
            $result->setData('is_available', false);
        }

Hope, It will helpful for you.

1
  • 1
    for array, you can use in_array() function. Commented Nov 18, 2019 at 12:18

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.