0

I'm trying to access logged in customer data in an "after" plugin.

The plugin is working, but I need some logic based on a custom customer attribute.

I try to do this but I get an error

Fatal error: Uncaught Error: Call to a member function get() on null in /Users/name/file/project/vendor/magento/framework/ObjectManager/Factory/AbstractFactory.php on line 170

I've tried using create instead of get for the object manager, but I get the same result.

I have a helper function that I use to get this data in some phtml files, but it doesn't seem to work in this class.

Do I have to get this data from somewhere else?

Thanks

namespace <company>\EmailCopy\Plugin;

use Magento\Framework\App\ObjectManager;
use Magento\Sales\Model\Order\Email\Container\OrderIdentity;
use Psr\Log\LoggerInterface;

class SendOrderCopy
{

   private $logger;
   private $objectManager;

   public function __construct(
       LoggerInterface $logger,
       ObjectManager $objectManager

   )
   {
       $this->logger = $logger;
       $this->objectManager = $objectManager;
   }

   public function afterGetEmailCopyTo(OrderIdentity $identity, $emailList)
   {
       $customer = $this->objectManager::getInstance()->get('Magento\Customer\Model\Session')->getCustomer();
       $customer = $customer->getData();
       var_dump($customer);
       $emailList[] = "<email>";
       $emailList[] = "<email";
       return $emailList;
   }
}

1 Answer 1

0

The point of this was to be able to get a custom attribute every time an email was sent, and CC a local sales rep based on that custom code.

This was the final function to get the customer data

  public function afterGetEmailCopyTo(OrderIdentity $identity, $emailList)
    {
        $customerEmail = $identity->getCustomerEmail();
        $customer = $this->_customerRepository->get($customerEmail);
        $territoryCode = $customer->getCustomAttribute('territory_code')->getValue();
        switch ($territoryCode) {
            ...
        }
    }
}

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.