0

I'm trying to get custom customer attribute value (for attribute code abn_no) without loading the customer but not able to find any way to get it.

Is there any way to get custom customer attribute value without loading the customer?

3
  • from where you want to get the attribute value, Observer, or any custom file where customers object is available..? Commented Mar 23, 2022 at 11:26
  • i want it in my custom file. if the customer is login then i want to get attribute value of that customer, but i dont want to load the entire customer. Commented Mar 24, 2022 at 3:53
  • Have you got solution ?
    – Msquare
    Commented Mar 31, 2022 at 13:27

2 Answers 2

1

You can retrieve customer custom attributes from the customer session; there's no need to load the customer every time in Magento. Please check the code below.

protected $sessionFactory;

public function __construct(
    \Magento\Customer\Model\SessionFactory $sessionFactory,
) {
    $this->sessionFactory = $sessionFactory;
}

public function getCustomerAttribute()
{
    $customer = $this->sessionFactory->create();
    return $customer->getCustomerData()->getCustomAttribute('<Your Attribute Code>')->getValue();
}
1

This is not correct method but you have asked the question I answered it.

1st method:

You can load the attribute's value by attribute's code/Id(static) with customer id(dynamic get from session). For this you have to run raw SQL for the respected attribute table like "customer_entity_varchar". Note: Attribute Id can change respected to deploy environment.

2nd Method: This method is bit more correct one, you can set value in session of customer after login as you said.

Your Answer

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

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