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?
Magento Stack Exchange is a question and answer site for users of the Magento e-Commerce platform. It only takes a minute to sign up.
Sign up to join this communityI'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?
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();
}
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.