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