How to Get Custom Customer Attribute Value in Magento 2
Magento 2 stores being the preferred choice for the majority of the E-commerce business of various types, developers have to be ready to implement any functionalities out of the blue!
One such thing that I faced was in a B2B Magento 2 store development. The B2B registration status value was required to send the registration acknowledgment or rejection Email to the customers. For that, I wanted to get the value of custom customer attribute “Account Status”. Hence, I came up with the below solution.
In other cases where you would want to get custom customer attribute value in Magento 2 stores is integration with 3rd party APIs. The APIs would return the customer attributes value which is not the part of the default Magento 2 and you’d want to use them further and require these custom attributes values. Another solution that becomes essential in this context is get customer data from attribute value in Magento 2. This approach provides the necessary tools to efficiently extract customer data linked to specific attribute values, particularly when sourced from external APIs.
Method to get custom customer attribute value in Magento 2:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
<?php namespace [Vendor]\[Module]\Helper; use Magento\Framework\App\Helper\Context; use Magento\Customer\Api\CustomerRepositoryInterface; class Data extends AbstractHelper { protected $customerRepository; public function __construct( Context $context, CustomerRepositoryInterface $customerRepository) { $this->customerRepository = $customerRepository; parent::__construct($context); } public function getAttributeValue($customerId) { $customer = $this->customerRepository->getById($customerId); return $customer->getCustomAttribute('custom_attribute_code'); } } |
Any doubts on the solution? I can help you with them. All you have to do is just mention them in the Comments section below and I’ll be there.
Feel free to share the post with fellow developers on social media.
Sanjay Jethva
Sanjay is the co-founder and CTO of Meetanshi with hands-on expertise with Magento since 2011. He specializes in complex development, integrations, extensions, and customizations. Sanjay is one the top 50 contributor to the Magento community and is recognized by Adobe.
His passion for Magento 2 and Shopify solutions has made him a trusted source for businesses seeking to optimize their online stores. He loves sharing technical solutions related to Magento 2 & Shopify.
10 Comments
Hi,
Can we use customer attribute to checkout page instead of first name middle name and last name
is there any way to do that ?
Hey Akash,
The first name and last name of the customer is mandatory.
Hey Sanjay, Is it possible that it will return custom customer attribute value only after we update the value from admin after the code? Because when I’m using this code and try to get my custom attribute value it returns value only after I update that value from the admin even it contains a value before I apply the code.
Hello Karmdip,
No, if the value is there in the custom attribute then you can get it using the above code.
Please check your implementation again.
Thank You
you forget the word ‘function’ in the method getAttributeValue
Hello Luciano,
Thanks for drawing my attention.
I’ve updated the blog
Thank You.
How can we use this code to add custom customer attributes in pdf invoice?
Hello Himanshu,
Simply create an object of a helper class and call the function in your pdf class like:
use [Vendor]\[Module]\Helper\Data;
protected $data;
public function __construct(
............ ,
Data $data
)
{
$this->data = $data;
parent::__construct(......);
}
$this->data->getAttributeValue($customerId);
Thank you
you need to use ->getValue(); ast last.
you have error in your code.
Hello,
In our code, the function will return all data of custom customer attribute.
Thanks.