0

I am making this observer class to check if a visitor is logged in.

use Magento\Framework\Event\ObserverInterface;
use Magento\Customer\Model\Session;

class CheckLoginObserver implements ObserverInterface
{
  /**
   * @var \Magento\Customer\Model\Session
   */
  protected $customerSession;

  public function execute(\Magento\Framework\Event\Observer $observer)
  {
    //use $customerSession variable here
    ...
  }

The goal is to use the $customerSession class in the execute function. What's the right way to do that?

1

It depends. Which event are You using for that observer? There is possibility that You can use customer session from Observer. Of course You can check it by look what contain $observer (by using xdebug).
If observer doesnt contain that, You have to use __construct function to load customerSession.

| improve this answer | |

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.