Trouble in getting in customer data using block class

All we need is an easy explanation of the problem, so here it is.

I’m having trouble in getting the customer details using block class. I want to show the customer name after he/she logged in using block class. I can do it via object manager but not through block.

Below are my code:

Block file:

<?php 

namespace vendor\module\Block;

use Magento\Customer\Model\Session;

class Testing extends \Magento\Framework\View\Element\Template {

    public function __construct(
        Session $customerSession,
        \Magento\Framework\View\Element\Template\Context $context
    )
    {
        parent::__construct($context);
        $this->_customerSession = $customerSession;

    }
    public function getCustomerName(){
        $this->_customerSession->getCustomer()->getName();
    }
}

In template file:

<?php echo $block->getCustomerName(); ?>

In XML:

<referenceContainer name="content">
     <block class="vendor\module\Block\Testing" name="customer.session.data" template="Magento_Theme::html/test.phtml" cacheable="false" />
 </referenceContainer>

When I try to load the customer name, its not working.

Any help will be appreciated!

How to solve :

I know you bored from this bug, So we are here to help you! Take a deep breath and look at the explanation of your problem. We have many solutions to this problem, But we recommend you to use the first method because it is tested & true method that will 100% work for you.

Method 1

You can use this in your Block file

<?php 

namespace vendor\module\Block;

use Magento\Customer\Model\Session;

class Testing extends \Magento\Framework\View\Element\Template {

    public function __construct(
        Session $customerSession,
        \Magento\Framework\View\Element\Template\Context $context
    )
    {
        parent::__construct($context);
        $this->_customerSession = $customerSession;
    }

    public function getCustomerName(){
        return $this->_customerSession->getCustomer()->getName();
    }


    public function getCustomerId(){
        return $this->_customerSession->getCustomer()->getId();
    }
}

In template file you can direct access your Block file’s function like this

<?php echo $block->getCustomerName(); ?>
<?php $customerId =  $block->getCustomerId(); ?>
<img src='<?php echo $this->helper("SK\ProfilePic\Helper\Data")->getProfilePicById($customerId); ?>' alt="Demo">

Because you have used this block class for this template in your XML file here

<block class="vendor\module\Block\Testing" name="customer.session.data" template="Magento_Theme::html/test.phtml" cacheable="false" />

Hope this will help!

Method 2

You are doing it correctly. You just need to change

this:

public function getCustomerName(){
    $this->_customerSession->getCustomer()->getName();
}

to:

public function getCustomerName(){
    return $this->_customerSession->getCustomer()->getName();
}

You forgot return

Note: Use and implement method 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply