0

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!

2 Answers 2

2

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!

2
  • 1
    Thanks @Kishan. I'm getting the image now using block.
    – MagentoDev
    Commented Nov 18, 2019 at 10:47
  • You're welcome @Siva Commented Nov 18, 2019 at 10:49
1

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

5
  • Thanks. I have tried as you said but still its not working. What am I missing?
    – MagentoDev
    Commented Nov 18, 2019 at 10:15
  • 1
    Answer updated, please check now Commented Nov 18, 2019 at 10:15
  • Its working thanks.
    – MagentoDev
    Commented Nov 18, 2019 at 10:20
  • If I want to show the customer image using customer Id, How can I proceed?
    – MagentoDev
    Commented Nov 18, 2019 at 10:20
  • I have added a function for getting customer ID: public function getCustomerId() { return $this->_customerSession->getCustomer()->getId(); } and now I have <img src='<?php echo $this->helper("SK\ProfilePic\Helper\Data")->getProfilePicById($customerData); ?>' alt="Demo">
    – MagentoDev
    Commented Nov 18, 2019 at 10:22

Your Answer

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

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