0

I have taken the reference from this link

Created the variable protected $_customerSession;

Done the injection \Magento\Customer\Model\Session $customerSession,

But in my Function in my block class it always returns as false

var_dump($this->_customerSession->isLoggedIn());

Then I tried checking whats Happening in $_SESSION

[_session_validator_data] => Array (
    [remote_addr] => ::1
    [http_via] => 
    )
[_session_hosts] => Array (
    [localhost] => 1
    )
[default] => Array (
    [_form_key] => k9RrgTSkjcgnJ1iZ
    )
[customer_base] => Array (
    [customer_group_id] => 0
    [customer_id] => 
    )
[checkout] =>  Array ( )
[catalog] =>  Array ( )
[reports] => Array (
    [product_index_viewed_count] => 1
    )
[message] =>  Array ( )
[review] =>  Array ( )

My block is extending Template class

use Magento\Framework\View\Element\Template as Template;

My construct function looks like this

public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Vendor\Module\Model\Faq $faq,
        \Vendor\Module\Helper\Data $dataHelper,
        \Magento\Customer\Model\Url $customerUrl,
        \Magento\Framework\Registry $registry,
        \Magento\Customer\Model\Session $customerSession,
        array $data = []
        ) {
            $this->faq = $faq;
            $this->_registry = $registry;
            $this->_dataHelper = $dataHelper;
            $this->_customerUrl = $customerUrl;
            $this->_customerSession = $customerSession;
            parent::__construct($context, $data);
            $this->setTabTitle();
    }

Just to add My php file in my block is custom file in my custom module.

0

can you try this

public function __construct(
    \Magento\Framework\View\Element\Template\Context $context,
    \Vendor\Module\Model\Faq $faq,
    \Vendor\Module\Helper\Data $dataHelper,
    \Magento\Customer\Model\Url $customerUrl,
    \Magento\Framework\Registry $registry,
    \Magento\Customer\Model\Session $customerSession,
    array $data = []
    ) {
        $this->_isScopePrivate = true;
        $this->faq = $faq;
        $this->_registry = $registry;
        $this->_dataHelper = $dataHelper;
        $this->_customerUrl = $customerUrl;
        $this->_customerSession = $customerSession;
        parent::__construct($context, $data);
        $this->setTabTitle();
}
  • Tried but not working – Gagan Aug 1 '18 at 5:24
0

Construct code -

public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Vendor\Module\Model\Faq $faq,
        \Vendor\Module\Helper\Data $dataHelper,
        \Magento\Customer\Model\Url $customerUrl,
        \Magento\Framework\Registry $registry,
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\App\Request\Http $request,
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository,
        \Magento\Customer\Model\SessionFactory $_customerSession,
        array $data = []
        ) {
            $this->_isScopePrivate = true;
            $this->faq = $faq;
            $this->_registry = $registry;
            $this->_dataHelper = $dataHelper;
            $this->_customerUrl = $customerUrl;
            $this->_customerSession = $customerSession;
            $this->request = $request;
            $this->customerRepository = $customerRepository;
            $this->customerSession = $_customerSession;
            parent::__construct($context, $data);
            $this->setTabTitle();
    }

Function code -

$customer = $this->customerSession->create();
        var_dump($customer->isLoggedIn());

This is what worked for me. I will be now Removing unnecessary code.

This is the answer which helped from Prashant

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.