Skip to content

Not able to access Customer Session via custom module in Magento 2

I’m not able to access the customer session in my custom module. However, when I’m navigating my custom action to salesrep/index/account I’m able to get the customer session. But when I’m navigating my custom module via category click not able to access it.

Here is my code.
Controller Code

<?php
namespace ABCSolutionsCustomerControllerIndex;

use MagentoFrameworkAppActionContext;
use MagentoFrameworkAppConfigScopeConfigInterface;
use MagentoFrameworkAppResponseInterface;
use MagentoFrameworkViewResultPageFactory;
use PsrLogLoggerInterface as Logger;

class Accounts extends MagentoFrameworkAppActionAction
{

    /**
     * @var PageFactory
     */
    protected $resultPageFactory;

    /**
     * Logger.
     *
     * @var Logger
     */
    protected $logger;

    /**
     * Core store config
     * @var ScopeConfigInterface
     */
    protected $_scopeConfig;

    /**
     * @param Context $context
     * @param PageFactory $pageFactory
     * @param ScopeConfigInterface $scopeConfigInterface
     * @param Logger $loggerInterface
     */
    public function __construct(
        Context $context,
        PageFactory $pageFactory,
        ScopeConfigInterface $scopeConfigInterface,
        Logger $loggerInterface,
    ){
        $this->resultPageFactory = $pageFactory;
        $this->_scopeConfig = $scopeConfigInterface;
        $this->logger = $loggerInterface;
        parent::__construct($context);
    }

    /**
     * @return ResponseInterface|MagentoFrameworkControllerResultInterface|MagentoFrameworkViewResultPage
     */
    public function execute()
    {
        $page = $this->resultPageFactory->create();
        $page->getConfig()->getTitle()->set(__('Customers'));
        return  $page;
    }


}

Block Code:

<?php
namespace ABCSolutionsCustomerBlock;

use MagentoFrameworkViewElementTemplate;
use MagentoFrameworkViewElementTemplateContext;
use MagentoCustomerModelSession as CustomerSession;


    class Accounts extends Template
    {
    
        /**
         * @var CustomerSession
         */
        protected $customerSession; 
    
        /**
         * @param Context $context
         * @param CustomerSession $customerSession
         */
        public function __construct(
            Context $context,
            CustomerSession $customerSession
        ){
            $this->customerSession = $customerSession;
            parent::__construct($context);
        }
        public function getCustomerId()
        {
            if ($this->customerSession->isLoggedIn()) {
                return $this->customerSession->getCustomer()->getId();
            }
            return 0;
        }
        }

Layout code

<referenceContainer name="content">
            <block class="ABCSolutionsCustomerBlockAccounts" name="sales_rep_accounts" template="ABCSolutions_Customer::accounts.phtml" cacheable="false" />
        </referenceContainer>

Phtml code:

<?php /** @var $block ABCSolutionsCustomerBlockAccounts */ ?>
    $customerId = $block->getCustomerId();

Static Block:

{{block class="ABCSolutionsCustomerBlockAccounts" template="ABCSolutions_Customer::accounts.phtml"}}

And the Category:
For this category there are no products, So when we click on this the custom module will show.
enter image description here

In frontend website menu.
enter image description here

Any help on this?