0

Source : https://www.magedelight.com/resources/how-to-disable-payment-method-for-certain-customer-groups-in-magento-2/ 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?

3
1

If you want to disable PayPal for other groups you'd add this line first, when you are inside the PayPal condition

$result->setData('is_available', false);

So...

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);
           }
       }
   }
}
3
  • Thank you, its working – zus Oct 21 '20 at 9:40
  • After creating the module my live website returns " An error has happened during application run. See exception log for details. Could not write error message to log. Please use developer mode to see the message. " during running of the upgrade, setup, cache commands, how to install module without affecting my live website. – zus Oct 21 '20 at 9:53
  • 1
    We usually enable maintenance while deploying code – Raul Sanchez Oct 21 '20 at 12:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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