4

Is it possible to choose payment method to certain customer groups? In Magento 1 there are some codes to add payment modules, like that any ways in Magento 2 ? without using plugins?

1

2 Answers 2

7

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

app/code/Pits/PaymentMethod/etc/
app/code/Pits/PaymentMethod/Observer/

Step 2) Declare your module

app/code/Pits/PaymentMethod/etc/module.xml.

Paste the following code in the above file.

    <?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="Pits_PaymentMethod" setup_version="2.0.0" /></config>

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

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

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

app/code/Pits/PaymentMethod/etc/events.xml

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" />
    </event>
</config>

Step 7)Lets create observer file

app/code/Pits/PaymentMethod/Observer/DisabledPgByCustomergroup.php

Paste the below code in it.

<?php
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();
        $this->_logger->info($method_instance->getCode());
        /* 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);
            }
        }
        /*else{
        if($method_instance->getCode() =='purchaseorder'){
        $result->setData('is_available', true);

        }
        }*/
    }
}

Step 8)Run this in command line

php bin/magento setup:upgrade
1
  • This answer is not related at all to the original question. Commented May 28, 2020 at 0:58
4

For this, you need a create module which will fire an observer on event payment_method_is_active .

And on this observer,you can disabled any payment method basis of Customer group .

<?php
namespace Devamitbera\DisabledPgByCustomergroup\Observer;

use Magento\Framework\Event\ObserverInterface;
use Magento\Framework\App\Request\DataPersistorInterface;
use Magento\Framework\App\ObjectManager;

class ObserverforDisabledPaymentgateway implements ObserverInterface
{
    public function __construct() {}

    /**
     *
     * @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() =='YOUR_CUSTOMER_GROUP_ID'  ){
          /* Disable All payment gateway  exclude Your payment Gateway*/
          if($method_instance->getCode() !='YOUR_PAYMENT_METHOD_CODE'){
               $result->isAvailable = false;
          }
      }

    }

}

Note:

Here , i have Disabled all payment payment gateways excluding Your gateways when a particular customer group have order placing

10
  • Amit, Thanks for the answer, But my module not working as I expected, can you check here drive.google.com/file/d/0B0NGKxAic15YUV8wQmwtazgzU1E/… Commented Oct 31, 2016 at 11:13
  • okay. but it not good idea to
    – Amit Bera
    Commented Oct 31, 2016 at 11:15
  • Yea I know but that's very difficult to write/read entire module code/structure in comment, so please.... Commented Oct 31, 2016 at 13:33
  • Amit, any word, please? Commented Nov 15, 2016 at 18:47
  • why rewrite controller ? it is an observer
    – Amit Bera
    Commented Nov 16, 2016 at 18:45

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.