cancel
Showing results for 
Search instead for 
Did you mean: 

Magento 2 - Disable Payment Method for Certain Customer Groups by using Observer

SOLVED

Magento 2 - Disable Payment Method for Certain Customer Groups by using Observer

Source : https://www.magedelight.com/resources/how-to-disable-payment-method-for-certain-customer-groups-in-m... Stright opposite i just enable certain customer group,

 

app/code/Gta/EnablePaymentMethod/registration.php

<?php
\Magento\Framework\Component\ComponentRegistrar::register(
    \Magento\Framework\Component\ComponentRegistrar::MODULE,
    'Gta_EnablePaymentMethod',
    __DIR__
);

app/code/Gta/EnablePaymentMethod/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="enable_payment_customer_group" instance="Gta\EnablePaymentMethod\Observer\PaymentMethodEnable" />
    </event>
</config>

app/code/Gta/EnablePaymentMethod/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="Gta_EnablePaymentMethod" setup_version="1.0.0" schema_version="1.0.0"/>
</config>

app/code/Gta/EnablePaymentMethod/Observer/PaymentMethodEnable.php

<?php
namespace Gta\EnablePaymentMethod\Observer;
use Magento\Framework\Event\Observer;
use Magento\Framework\Event\ObserverInterface;
class PaymentMethodEnable implements ObserverInterface {
    protected $_customerSession;
    public function __construct(
       \Magento\Customer\Model\Session $customerSession
    ) {
       $this->_customerSession = $customerSession;
    }
    public function execute(Observer $observer) {
       $payment_method_code = $observer->getEvent()->getMethodInstance()->getCode();
       if ($payment_method_code == 'paypal_express') {
           $result = $observer->getEvent()->getResult();
           if ($this->_customerSession->isLoggedIn()) {
               $customerGroupId = $this->_customerSession->getCustomer()->getGroupId();
               if ($customerGroupId == 9) {
                   $result->setData('is_available', true);
               }
           }
       }
    }
}

Anything else i forget to do?

1 ACCEPTED SOLUTION

Accepted Solutions

Re: Magento 2 - Disable Payment Method for Certain Customer Groups by using Observer

Solution:

 

public function execute(Observer $observer) {
   $payment_method_code = $observer->getEvent()->getMethodInstance()->getCode();
   if ($payment_method_code == 'paypal_express') {
       $result = $observer->getEvent()->getResult();
       $result->setData('is_available', false);
       if ($this->_customerSession->isLoggedIn()) {
           $customerGroupId = $this->_customerSession->getCustomer()->getGroupId();
           if ($customerGroupId == 9) {
               $result->setData('is_available', true);
           }
       }
   }
}

View solution in original post

1 REPLY 1

Re: Magento 2 - Disable Payment Method for Certain Customer Groups by using Observer

Solution:

 

public function execute(Observer $observer) {
   $payment_method_code = $observer->getEvent()->getMethodInstance()->getCode();
   if ($payment_method_code == 'paypal_express') {
       $result = $observer->getEvent()->getResult();
       $result->setData('is_available', false);
       if ($this->_customerSession->isLoggedIn()) {
           $customerGroupId = $this->_customerSession->getCustomer()->getGroupId();
           if ($customerGroupId == 9) {
               $result->setData('is_available', true);
           }
       }
   }
}