2

I am avoiding using the object manager directly, but I cannot get this injected session to return a customer id?

...
use Magento\Customer\Model\Session;
...

class Summary extends Template
{

    protected $_session;

    public function __construct(
        Session $session,
        Quote $quote, 
        Template\Context $context, 
        array $data = [])
    {
        $this->_session = $session;
        $this->_quote = $quote;
        parent::__construct($context, $data);
    }
    var_dump($this->_session->getCustomerId());

    // Returns null.
  • have you check customer is logged in or not? – Rakesh Jesadiya Jun 29 '16 at 9:26
  • The customer is definitely logged in... it's me? – LM_Fielding Jun 29 '16 at 9:32
4

you can get current customer id using,

    protected $currentCustomer;

    public function __construct(
            \Magento\Framework\View\Element\Template\Context $context,
            \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer,
            array $data = []
        ) {
            $this->currentCustomer = $currentCustomer;           
            parent::__construct($context, $data);
        }

    public function getCustomerId(){
          return $this->currentCustomer->getCustomerId();
    } 
| improve this answer | |
  • Is this working? plz remove var and check. – Rakesh Jesadiya Jun 29 '16 at 10:13
  • No, that won't work. I had to inject my model with customer/session rather than using session in a block class. Really good idea though, thanks. – LM_Fielding Jun 29 '16 at 10:31

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.