Question

I am using Magento 2.2.8 Enterprise Edition

I have a custom address attribute area a dropdown attribute with multiple options.

Now when I am getting value of attribute by using this code, I am getting ID

$address->getCustomAttribute('area')->getValue();

I just want to get Label of the selected area option, can anyone tell me How can I get it?

Was it helpful?

Solution

Load eavConfig via dependency Injection.

protected $eavConfig;
public function __construct(
    \Magento\Eav\Model\Config $eavConfig,
)
{
    $this->eavConfig = $eavConfig;
}

Than you can get the label like this.

$attribute = $this->eavConfig->getAttribute('customer_address', "attribute_code");
$optionlabel = $attribute->getSource()->getOptionText($attributeValue);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top