9

This question already has an answer here:

I followed this tutorial to add a custom attribute to Customer. Then in Controller LoginPost.php, I want to get the value of that attribute of a particular customer to do some checking. But I found it completely different from Magento 1.X to retrieve the value of a custom attribute.

Anyone can suggest some methods? Thanks.

marked as duplicate by KAndy, Sander Mangel, Keyul Shah, Marius Nov 22 '15 at 15:44

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • Maybe you misunderstood my question. The problem I'm facing is how to retrieve the value of a custom attribute, NOT creating a custom attribute. Thanks. – Ricky.C Nov 22 '15 at 13:45
  • Magento 1.X got $customerObj = Mage::getModel('customer/customer')->load($customer_id); $attr = $customerObj->getData('manager'); to retrieve the values. But in Magento2, I can't find the method to retrieve the values. – Ricky.C Nov 22 '15 at 13:46
  • 1
    Definitely not a duplicate. Did you read the questions guys ? – Sylvain Rayé May 27 '16 at 18:15
15

\Magento\Customer\Api\Data\CustomerInterface extends \Magento\Framework\Api\CustomAttributesDataInterface that have getCustomAttribute method. You can use it to get custom attribute. ex. for test:

$customerRepository = $objectManager
        ->get('Magento\Customer\Api\CustomerRepositoryInterface');
$customer = $customerRepository->getById(1);
$cattrValue = $customer->getCustomAttribute('my_custom_attribute');
  • @kandy.where can i find implementation of getCustomAttribute.in customer model there is no implementation for this. – sivakumar Nov 22 '15 at 14:35
  • 1
    see \Magento\Framework\Api\AbstractExtensibleObject – KAndy Nov 22 '15 at 14:46
  • 1
    $customer->getCustomAttribute('custom_id '); returns a Magento\Framework\Api\AttributeValue Object ( [_data:protected] => Array ( [attribute_code] => custom_id [value] => a234 ) ) .Then how can I obtain the value 'a234' since I can't access to \Magento\Framework\Api\AttributeValue, so I have no ideas how to do that. Thanks – Ricky.C Nov 23 '15 at 2:21
  • 5
    Finally, I found it by my guess. $customer->getCustomAttribute('custom_id ')->getValue(); – Ricky.C Nov 23 '15 at 3:04
  • 1
    Hi , Can you tell me how to save custom attribute..? – Elavarasan Mar 15 '16 at 16:32

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