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

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!

  • 1
    Thanks @Kishan. I'm getting the image now using block. – Siva Nov 18 at 10:47
  • You're welcome @Siva – Kishan Savaliya Nov 18 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

  • Thanks. I have tried as you said but still its not working. What am I missing? – Siva Nov 18 at 10:15
  • 1
    Answer updated, please check now – Shoaib Munir Nov 18 at 10:15
  • Its working thanks. – Siva Nov 18 at 10:20
  • If I want to show the customer image using customer Id, How can I proceed? – Siva Nov 18 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"> – Siva Nov 18 at 10:22

Your Answer

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

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