I tried to get the customer session
in my custom module, but it is getting null always when using <update handle="customer_update"/>
in the layout file.
If I comment <update handle="customer_update"/>
the customer session is getting.
And My code is below.
Block file
<?php
namespace Learning\CustomerAccLink\Block;
use Magento\Customer\Model\Session as Session;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Exception\NoSuchEntityException;
use Magento\Framework\View\Element\Template\Context;
class CustomerLink extends \Magento\Framework\View\Element\Template
{
/**
* Session object.
*
* @var Session
*/
private $customerSession;
/**
*
* Object initialization.
*
* @param Context $context
* @param Session $session
* @param array $data
*/
public function __construct(
Context $context,
Session $session,
array $data = []
){
$this->customerSession = $session;
parent::__construct($context, $data);
}
/**
* Get Customer Group ID
*
* @return int
* @throws LocalizedException
* @throws NoSuchEntityException
*/
public function getCustomerGPID(): int
{
return $this->customerSession->getCustomer()->getGroupId();
/*try {
}catch (LocalizedException|NoSuchEntityException $ex){
return 0;
}*/
}
}
Controller file
<?php
namespace Learning\CustomerAccLink\Controller\Index;
use Magento\Framework\App\Action\Context;
use Magento\Framework\View\Result\PageFactory;
class Index extends \Magento\Framework\App\Action\Action
{
/**
* @var PageFactory
*/
protected $resultPageFactory;
/**
* @param Context $context
* @param PageFactory $resultPageFactory
*/
public function __construct(
Context $context,
PageFactory $resultPageFactory
) {
$this->resultPageFactory = $resultPageFactory;
parent::__construct($context);
}
/**
* Default customer account page
*
* @return Page
*/
public function execute()
{
//echo "Hello";
return $this->resultPageFactory->create();
}
}
routes.xml file
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:App/etc/routes.xsd">
<router id="standard">
<route id="learning" frontName="learning">
<module name="Learning_CustomerAccLink" />
</route>
</router>
</config>
module.xml file
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Learning_CustomerAccLink" >
</module>
</config>
customer_account.xml file
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" layout="2columns-left"
xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd"
label="Customer My Account (All Pages)" design_abstraction="custom">
<body>
<attribute name="class" value="account"/>
<referenceContainer name="customer_account_navigation">
<block class="Magento\Framework\View\Element\Html\Link\Current"
name="customer-account-navigation-custom-link" after="-">
<arguments>
<argument name="label" xsi:type="string" translate="true">Customer Link</argument>
<argument name="path" xsi:type="string">learning</argument>
</arguments>
</block>
</referenceContainer>
</body>
</page>
learning_index_index.xml file
<?xml version="1.0"?>
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
<!-- <update handle="customer_account"/>-->
<body>
<referenceContainer name="content">
<block class="Learning\CustomerAccLink\Block\CustomerLink" name="custom_customer_link" template="customerLink.phtml" />
</referenceContainer>
<referenceBlock name="root">
<action method="setHeaderTitle">
<argument translate="true" name="title" xsi:type="string">Customer Link | Learning</argument>
</action>
</referenceBlock>
</body>
</page>
customerLink.xml file
<?php
/** @var $block Learning\CustomerAccLink\Block\CustomerLink */
?>
<?php echo $block->getCustomerGPID(); ?>
registration.php file
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Learning_CustomerAccLink',
__DIR__
);
Was there I missed any?
Thanks,