2

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

$block->getCustomer()->getGender() 

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()

5
  • what's wrong with $block->getCustomer()->getGender()? you don't have to use the objectmanager to get the customer. use the customersession Commented Jan 22, 2019 at 7:28
  • $block->getCustomer()->getGender() gives me integer value Commented Jan 22, 2019 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 Commented Jan 22, 2019 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 Commented Jan 22, 2019 at 7:33
  • look at the lower part of the code in your answer Commented Jan 22, 2019 at 7:34

2 Answers 2

1

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; if($this->customerSession->isLoggedIn()){ $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'; } } 
7
  • use the session to get the customer Commented Jan 22, 2019 at 7:33
  • Updated answer :) Commented Jan 22, 2019 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. Commented Jan 22, 2019 at 7:48
  • 1
    @PhilippSander Yes Exactly, remove customer Factory Commented Jan 22, 2019 at 9:58
  • @RohanHapani also, no need to use getResource() as it is deprecated, you can use getAttribute() directly on $customer Commented Jan 22, 2019 at 10:03
1

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; 
2
  • Dependency Injection is more recommended than object manager way Commented Jan 22, 2019 at 11:56
  • @RintoGeorge, Yes definitely we should use DI instead of object manager. Commented Jan 22, 2019 at 11:57

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.