I am using Magento 2.3

I have created a custom attribute "Test model"(id: test_model).Attribute is Multiple select.Please see below code:

  $_productCollection = $block->getLoadedProductCollection();
   foreach ($_productCollection as $_product): ?>
     <?php $attributes = $_product->getAttributes();
        foreach($attributes as $a){ 
        <?php $name = $a->getName(); ?>
            <?php if(preg_match("/_model/", $name)){ 
                if($a != ''){ ?>
                    <p><?php echo $a->getName(); ?></p>
                <?php } ?>
            <?php } ?>
        <?php } ?>

     <?php endforeach; ?>

First I have called all product attributes after that I have added condition to display only the attribute which have "_model" in its name. Output for this code is as below:

enter image description here

Now I want to display all selected value of this attribute as well, So How can I achieve this?

Here attribute is displayed four times because current category contains 4 products. So is there any other way to display the attribute and value only one time?

NOTE: Remember that I want to show only those values of this attribute which are selected in product not all value

Your Answer

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

Browse other questions tagged or ask your own question.