ViSuaL
ViSuaL

Reputation: 281

Magento 2.0 - How to get Customer Multiselect Attribute Options Value

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.

Upvotes: 3

Views: 2292

Answers (1)

ViSuaL
ViSuaL

Reputation: 281

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());

Upvotes: 2

Related Questions