0

I want to get customer id in block but the customer session returns null.

from controller, i could get customer id.

logger in block works well so the customer id is executed.

vendor/Block/Config.php

class Config extends \Magento\Framework\View\Element\Template                                                                                                                                       
{
    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\Session $customerSession
    ) {
        $this->session = $customerSession;
        $this->logger = $logger;

        parent::__construct($context);
    }

    /**
     */
    public function getCustomerId(): int
    {
        $this->logger->debug('this log is ok');
        return $this->session->getCustomer()->getId(); // return null
    }

from view, <?=$block->getCustomerId();?>

this code returns 500 because the type hinting of getCustomerId() is :int and return null.

this->session->getCustomer returns null

this->session->isLoggedIn also returns null

i saw almost same, other question in stackexchange, they do not fit my ploblem.

like this Magento2 error on injecting customer session in block

ofcourse i did

setup:upgrade

setup:di:compile

cache:celan

1
  • logger in block works well so the customer id is executed. -> logger in block works well so the getCustomerId() is executed.
    – grandan
    Oct 23, 2020 at 1:54

2 Answers 2

0

You cannot get a Customer session if your page is at under full-page cache.

Use Magento\Framework\App\Http\Context in this case. See https://devdocs.magento.com/guides/v2.4/extension-dev-guide/cache/page-caching/public-content.html#configure-page-variations

Define a new Magento\Framework\App\Http\Context variable using a plugin on \Magento\Framework\App\Http\Context:getVaryString and set customer id to new varible. and after that get customer id from context

0

Create a plugin class on Magento\Framework\App\Action\AbstractAction.

app/code/Vendorename/Modulename/etc

di.xml

<?xml version="1.0" ?>

<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">

    <type name="Magento\Framework\App\Action\AbstractAction">
        <plugin name="get_customr_id"  disabled="false" sortOrder="20" type="Vendorename\Modulename\Plugin\Framework\App\Action\AbstractAction"/>
    </type>

    </type>
</config>

app/code/Vendorename/Modulename/Plugin/Framework/App/Action

AbstractAction.php

<?php

declare(strict_types=1);

namespace Vendorename\Modulename\Plugin\Framework\App\Action;

use Magento\Customer\Model\Context as CustomerContext;
use Magento\Framework\Exception\NoSuchEntityException;

class AbstractAction
{

    private $customerRepository;
    private $customerSession;
    private $httpContext;

    public function __construct(
        \Magento\Customer\Model\Session $customerSession,
        \Magento\Framework\App\Http\Context $httpContext,
        \Magento\Customer\Api\CustomerRepositoryInterface $customerRepository
    ) {
        $this->customerSession = $customerSession;
        $this->httpContext = $httpContext;
        $this->customerRepository = $customerRepository;
    }

    public function aroundDispatch(
        \Magento\Framework\App\ActionInterface $subject,
        callable $proceed,
        \Magento\Framework\App\RequestInterface $request
    ) {

        $customerId = $this->customerSession->getCustomerId();
        $this->httpContext->setValue(
            'current_cust_id',
            $customerId,
            0
        );

        return $proceed($request);
    }
}

After that adding above code ,add this \Magento\Framework\App\Http\Context on your block class and get this customer id value by $this->httpContext->getValue('current_cust_id')

private $httpContext;

public function __construct(
    .........................................
    \Magento\Framework\App\Http\Context $httpContext,
    ............................................
) {
    .........................................
    $this->httpContext = $httpContext;
    .........................................
}

.........................................


$cust_id = $this->httpContext->getValue('current_cust_id'); 
echo "Customer Id ".$cust_id;

I Hope This Helps You.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.