TransWikia.com

Getting a customer custom attribute label not value

Magento Asked by Goose84 on October 2, 2021

I know I can do the following

    $customerId = 1;
    $customer = $this->customerRepositoryInterface->getById($customerId);
    $code = $customer->getCustomAttribute('attribute_code')->getValue();

But as the attribute is a type of "select" and not "text", it gives me the ID as the value and not the actual label

Therefore how do I get the label/frontend name of the option instead of the ID

One Answer

You might need to load the attribute using the AttributeRepositoryInterface::get($entityTypeCode, $attributeCode).

This will return an AttributeInterface on which you can call getOptions().

This will return an AttributeOptionInterface[]

Each AttributeOptionInterface exposes getLabel() and getValue().

What you are left with is a search problem.

Also as this is a custom attribute you(or who added the attribute) in a DataPatch or InstallScript, probably already specified the options for it. From that Option class you can extract your label based on your value.

Answered by vitoriodachef on October 2, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP