3

I'm trying to get custom customer attribute options value scoped on current store from customer session on Magento 2.0 EE.

Now I'm getting only the options ids:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$customerSession = $objectManager->get('Magento\Customer\Model\Session');
$customerRepository = $objectManager
    ->get('Magento\Customer\Api\CustomerRepositoryInterface');
$customer = $customerRepository->getById($customerSession->getCustomerId());
$attrValue = $customer->getCustomAttribute('attribute_code')->getValue();

var_dump($attrValue) is string(id1,id2,id3)

How can I get frontend text value of these options.

Daniel Widdis
  • 8,424
  • 13
  • 41
  • 63
ViSuaL
  • 281
  • 2
  • 4
  • 16

1 Answers1

2

I found a solution, I'm not sure is a good practice... :

        $objectManager = \Magento\Framework\App\ObjectManager::getInstance();
        $customerSession = $objectManager->get('Magento\Customer\Model\Session');
        $customerRepository = $objectManager
            ->get('Magento\Customer\Api\CustomerRepositoryInterface');
        $customer = $customerRepository->getById($customerSession->getCustomerId());

        $model = $objectManager->create('Magento\Eav\Model\ResourceModel\Entity\Attribute\Option\Collection');
        $model->setIdFilter(explode(',',$customer->getCustomAttribute('attribute_code')->getValue()))
            ->setStoreFilter();

        var_dump($model->toOptionArray());
ViSuaL
  • 281
  • 2
  • 4
  • 16