1

I have a custom offline payment method that I want to show based on a customer attribute value. Eventually I want to check that value against the total and if it is less than the total then disable the method. But first thing is first, I can't seem to get the customer attribute. I am testing with group_id which in this case is 1. This is what I have:

etc > events.xml

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Event/etc/events.xsd">
    <event name="sales_order_save_before">
        <observer name="payroll_split_payment_before_save" instance="Vendor\Module\Observer\QuoteToOrderObserver" />
    </event>

    <event name="payment_method_is_active">
        <observer name="payroll_payment_method_is_active" instance="Vendor\Module\Observer\DisablePayment" />
    </event>
</config>

and my Observer:

<?php

namespace Vendor\Module\Observer;

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

class DisablePayment implements ObserverInterface
{
  protected $_customerRepositoryInterface;
  protected $_logger;

  public function __construct(
    // --> delete this line and replace with answer below \Magento\Customer\Api\CustomerRepositoryInterface $customerRepositoryInterface,
    LoggerInterface $logger
  ) {
    // --> delete this line and replace with answer below $this->_customerRepositoryInterface = $customerRepositoryInterface;
    $this->_logger = $logger;
  }

  /**
   * @param Observer $observer
   *
   * @return void
   */
  public function execute(
    \Magento\Framework\Event\Observer $observer
    )
  {

    $result = $observer->getEvent()->getResult();
    $method_instance = $observer->getEvent()->getMethodInstance()->getCode();
    $quote = $observer->getEvent()->getQuote();

    if($method_instance == 'payroll')
    { 
      $customer = $this->_customerRepositoryInterface->getById($quote->getCustomer()->getId());
      if($customer->getCustomAttribute('group_id')->getValue() == 1)
      {
        $result->setData('is_available', false);
      }
    }
  }
}

I get this error:

'Uncaught Error: Call to a member function getValue() on null in DisablePayment.php

What am I doing wrong?

1

You can use Magento\Customer\Model\Session $customerSession to get current customer group id for logged in customers.

You can use the below code in your DisablePayment.php file for the same.

namespace Vendor\Module\Observer;

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

class DisablePayment implements ObserverInterface
{
  protected $_customerSession;
  protected $_logger;

  public function __construct(
    \Magento\Customer\Model\Session $customerSession,
    LoggerInterface $logger
  ) {
    $this->_customerSession = $customerSession;
    $this->_logger = $logger;
  }

  /**
   * @param Observer $observer
   *
   * @return void
   */
  public function execute(Observer $observer)
  {

    $result = $observer->getEvent()->getResult();
    $method_instance = $observer->getEvent()->getMethodInstance()->getCode();
    $quote = $observer->getEvent()->getQuote();

    if($method_instance == 'payroll')
    { 
        $customerGroupId = $this->getGroupId();
        if($customerGroupId == 1)
        {
            $result->setData('is_available', false);
        }
    }
  }

  public function getGroupId(){
     if($this->_customerSession->isLoggedIn()):
            return $customerGroup=$this->_customerSession->getCustomer()->getGroupId();
     endif;
  }
}

Hope it helps!!!

| improve this answer | |
  • Hi, thanks for this! I will test and report back. I had stated above that I was using group_id as a test. Eventually I want to use a different customer attribute, it will be a custom one I create called payroll_limit. It will have a number in it that I will use as a check against the total to show or hide the method. Will your way still work? Thanks! – stanhook Sep 6 '19 at 15:52
  • You can check the data coming in $this->_customerSession->getCustomer()->getData(); I've answered as per you question. – Sumit Sep 6 '19 at 16:00
  • @Summit I will try it and report back. Thanks! – stanhook Sep 6 '19 at 16:17
  • 1
    I can confirm that the above code works and when I change it to $this->_customerSession->getCustomer()->getData('lastname') for example I can check it against that field. Thanks!! – stanhook Sep 6 '19 at 19:12
  • Hi, so I need to show a message that if that user ID (or whatever field) is not acceptable or a match that a message would show in the payment methods place. Would you know how to show a message if after to check the method wasn't available? Thanks! – stanhook Sep 9 '19 at 19:47

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.