Magento – Get Customer ID from Session Model

magento2

I've a helper and in the __construct() something like:

public function __construct(
        Context $context,
        \Magento\Customer\Model\Session $customerSession
    )
    {
        parent::__construct($context);

        $this->_customerSession = $customerSession;

    }

he problem is when I can the $_customerSession object data it's empty, even though I've logged in.

It seems the session is instantiated at a later point in the process, because if I call the customer\Session object in a page Model or block it works fine. In fact, the whole store works as expected. I can see the logged in welcome message at the top right bar, add products to cart, login.

Thecode above is dispatched in the event

catalog_product_get_final_price

How do I make sure the session is loaded or how do I know when it's loaded?

Best Answer

I've run into the problem too. The problem occurs when cache is enabled.

Please see https://github.com/magento/magento2/issues/2010

Block with Cachable=false not rendered on product view page