I want to get customer gender value like Male or Female in magento2? How to get it in \Magento\Customer\Block\Account\Dashboard\Info block,

I am doing something like this


I referred this link How to get customer gender value with text in magento2?

I don't want to use ObjectManager just for gender, there must be something via this method $block->getCustomer()->getGender()

  • what's wrong with $block->getCustomer()->getGender()? you don't have to use the objectmanager to get the customer. use the customersession – Philipp Sander Jan 22 at 7:28
  • $block->getCustomer()->getGender() gives me integer value – summu Jan 22 at 7:29
  • but the answer describes how to get the text-value. I don't get your problem. just don't use the objectmanager to get the customer. that's bad practice – Philipp Sander Jan 22 at 7:31
  • this $block->getCustomer()->getGender() gives me integrer value, Obviously i dont want to show integer on UI to user,i want text, like male or female – summu Jan 22 at 7:33
  • look at the lower part of the code in your answer – Philipp Sander Jan 22 at 7:34

Use this below code in your block :

protected $customerSession;

public function __construct(
    \Magento\Customer\Model\Session $customerSession
) {
    $this->customerSession = $customerSession;

public function yourFunction()
    //$customerId = 12;
        $customer = $this->customerSession->getCustomer();
        $customerId = $customer->getId();
        $genderText = $customer->getAttribute('gender')->getSource()->getOptionText($customer->getData('gender'));
        echo $genderText;
    } else{
        echo 'customer is not logged in';
  • use the session to get the customer – Philipp Sander Jan 22 at 7:33
  • Updated answer :) – Rohan Hapani Jan 22 at 7:36
  • 1
    no.... why not use the customer? why are oyu loading it again? that's the whole point of using the session. throw away the factory. you don't need it. – Philipp Sander Jan 22 at 7:48
  • 1
    @PhilippSander Yes Exactly, remove customer Factory – summu Jan 22 at 9:58
  • 1
    +1 for using DI – Rinto George Jan 22 at 11:54

You can try the below solution :

$customerId = 7; //customer's id you want to load
$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customer = $objectManager->create('Magento\Customer\Model\Customer')->load($customerId);

//Get gender value like 1,2,3
$genderValue = $customer->getCustomAttributeText('gender')->getValue();

//Get gender text like male, female
$genderText = $customer->getResource()->getAttribute('gender')->getSource()->getOptionText($customer->getData('gender'));

echo $genderText;
  • Dependency Injection is more recommended than object manager way – Rinto George Jan 22 at 11:56
  • @RintoGeorge, Yes definitely we should use DI instead of object manager. – Amit Naraniwal Jan 22 at 11:57

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.