0

I am trying to fetch a custom attribute value of a customer in magento 2 but I am unable to fetch it

I am using \Magento\Customer\Api\CustomerRepositoryInterface class to fetch the custom customer attribute of a customer

I have tried this

       $customers = $this->customerRepositoryInterface->getById(2);
       print_r($customers->getCustomAttributes('enable_alerts'));

This returns

Array (
    [enable_alerts] => Magento\Framework\Api\AttributeValue Object
        (
            [_data:protected] => Array
                (
                    [attribute_code] => enable_alerts
                    [value] => 1
                )

        )

)

But I need only value of enable_alerts. What I am doing wrong here Is there any other way of fetching customers attribute value I have tried multiple codes and searched every thing!

1 Answer 1

0

Try This :-

print_r($customers->getCustomAttributes('enable_alerts')->getValue());
7
  • Is there a way to filter the customer by custom attributes values ??
    – Ghulam.M
    Jun 19, 2019 at 7:37
  • yes you can use addAttributeToFilter to filter data Jun 19, 2019 at 7:38
  • Sorry to ask is there a way I can update current custom attribute value?? :)
    – Ghulam.M
    Jun 19, 2019 at 11:48
  • yes you can set Jun 19, 2019 at 11:53
  • could you please share how
    – Ghulam.M
    Jun 19, 2019 at 11:54

Your Answer

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

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