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?