0

On the product page (within the "column main" area) I have created a phtml file. In this I want to echo all the chosen options of a custom multiselect attribute.

What I have:

  1. current product (via \Magento\Framework\App\ObjectManager::getInstance())

  2. name of my custom attribute (xyz)

  3. for example 3 selected options out of 5 (e.g. "red", "green" and "black")

What I need:

  • The three values of the selected options

I tried

<?php $objectManager = \Magento\Framework\App\ObjectManager::getInstance();?>
<?php $currentProduct = $objectManager->get('Magento\Framework\Registry')->registry('current_product'); //get current product ?>
<?php echo $currentProduct->getCustomAttribute('xyz')->getValue(); ?>
<?php echo $currentProduct->getAttributeText('xyz'); ?>

But there is no output.

I found a lot of possible solutions using ->getResource() but I have come to learn this method is deprecated as from 2.3.

2

Silly me, it was a cache problem...

Might be useful for others:

<?php echo $currentProduct->getCustomAttribute('xyz')->getValue(); ?>

delivers the IDs of all multiselect options (e.g. "13,15,16")

<?php echo $currentProduct->getAttributeText('xyz'); ?>

returns an array with all the values needed ( Array ( [0] => Green [1] => Red [2] => Black - use print_r to get this).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.