1

I have made a custom link in My account section of customers. I want to get the customer's id with the click of button in another custom page (can\Test\Account). The customer id is not changing.

For example: Rahul logs in and goes to his My account section and clicks on a custom link then his customer id say 16 is appearing on can\Test\Account but if another customer with id of 18 comes log in and clicks on link in My account section then also 16 is appearing.

BLOCK code:

<?php
namespace SimplifiedMagento\Custom2\Block;
class Details extends \Magento\Framework\View\Element\Template
{
    protected $_customerSession;

    public function __construct(
        \Magento\Framework\View\Element\Template\Context $context,
        \Magento\Customer\Model\SessionFactory $customerSessionFactory
       // array $data = []
    ) {

        $this->_customerSession = $customerSessionFactory;
    parent::__construct($context/*, $data*/);
    }

    public function getDetails() {
        $obj = $this->_customerSession->create();
        if($obj->isLoggedIn()){
            echo $obj->getId();
           }
    }
}
?>

phtml:

<?php $block->getDetails();?>

Layout:

<?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">
    <body>
        <referenceBlock name="customer_account_navigation">
            <block class="Magento\Framework\View\Element\Html\Link\Current" name="demo-link">
                <arguments>
                    <argument name="path" xsi:type="string">can/Test/Account</argument>
                    <argument name="label" xsi:type="string">Employee details</argument>
                </arguments>
            </block>
        </referenceBlock>
    </body>
</page>
5
  • Please add the Layout XML code of your module in your question. – Kishor Thummar Dec 16 '19 at 12:11
  • Hi, @Ankur I just checked your block code and logged the customer id and I am getting every time different customer id those customers logged. I think maybe a problem in layout xml so you can share it in the above question. – Praveen Chelumalla Dec 16 '19 at 12:21
  • ok ..... i am sharing – Ankur Anand Dec 16 '19 at 12:38
  • shared.......please look for it...... – Ankur Anand Dec 16 '19 at 12:42
  • 1
    Try with set cacheable="false" in your xml. – Rohan Hapani Dec 16 '19 at 13:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.