1

Before 2.2 I made a small plugin to sync email subscription with our provider like the following and everything is working in ect/frontend/events.xml

I created my observer like this :

<event name="newsletter_subscriber_save_before">
    <observer name="VendorName_newsletter_subscriber" instance="VendorName\ModuleName\Observer\Newsletter" />
</event>

and in my VendorName\ModuleName\Observer\Newsletter.php

public function execute(Observer $observer)
{
    $event = $observer->getEvent();
    $customer = $event->getSubscriber();
    $customerEmail = $customer->getSubscriberEmail();
    $customerName = $customer->getFirstname();
    $customerLastname = $customer->getLastname();
    $customerId = $customer->getId();
    $checkSubscriber = $this->_subscriber->loadByEmail($customerEmail);
    if ($checkSubscriber->isSubscribed()) {
        // Customer is subscribed
        //sync with Sendinblue
        ....
    }
    $this->messageManager->addSuccessMessage(__('Welcome back beloved customer %1 !', $customer->getCustomer()));
}

everything was working as expected but now I got a "null" on my $customer variable.

I didn't find anything on dev-doc about changes. Has there been an update I've missed?

Thanks

1

Try below code. It will help you to get customer object.

protected $_customerSession;

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

$customer = $this->_customerSession->getCustomer();

OR - To get Customer using $observer

$customer = $observer->getEvent()->getCustomer();
  • Thanks but it didn't work also... I got a Call to a member function getSubscriberEmail() on null error, so as I started... – Mitchum Feb 7 '18 at 15:14
  • did you try both options? – Abhishek Panchal Feb 7 '18 at 15:15
  • YEs, got it ot work right now $customer = $this->_customerSession->getCustomer(); $customerEmail = $customer->getEmail(); $customerName = $customer->getFirstname(); $customerLastname = $customer->getLastname(); $customerId = $customer->getId(); $checkSubscriber = $this->subscriber->loadByEmail($customerEmail); Thanks – Mitchum Feb 7 '18 at 15:30

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.