1

I am using Magento 2.3

I want to display values of a particular attribute in category page. For ex. I have an attribute called 'test' and I have 10 values in this attribute. I have selected different values of this attribute in different products.

Now I want to display values of "test" attribute which are selected in product(not all values) and I want to display list of attribute values in category page instead of product listing.

So How can I achieve this?

2
  • looks like you could improve your question: I understand your attribute is a product attribute that is multiselect? Also, it is not clear whether your attribute's selected values are different for a same product but in a different category? As it stands you use the word test too many times and I wonder if this helps us to understand your question or us to be lost with it.. Commented Dec 27, 2018 at 9:23
  • you are right my question is complex and because of that I am not able to describe it clearly. So I will try to improve it Commented Dec 27, 2018 at 10:10

2 Answers 2

1

I have done something similar. I had an multi-select attribute named availability with possible options Collect At Store and Home Delivery. On listing page I had to show icons of these values were assigned for any particular product.

This is how I got it working in list.phtml

Inside <?php foreach ($_productCollection as $_product): ?> I have added following code:

<?php

if ($_product && ($attr = $_product->getResource()->getAttribute('availability')) && $attr->usesSource())
    {
    if ($ids = explode(',', $_product->getData('availability')))
        {
        if (!empty($ids))
            {
            foreach($ids as $id)
                {
                $deliveryOption = $attr->getSource()->getOptionText($id); ?>
                            <?php
                if ($deliveryOption == 'Collect At Store')
                    { ?> 
                                <!-- show Collect At Store icon -->
                            <?php
                    } ?>
                             <?php
                if ($deliveryOption == 'Home Delivery')
                    { ?> 
                                <!-- show Collect At Store icon -->
                            <?php
                    } ?>

                        <?php
                }
            }
        }
    }

?>

You can follow this example to show your attribute values.

1

To display the attribute value in product list page, add the below code in app/design/frontend/YOUR_VENDOR/YOUR_THEME/Magento_Catalog/templates/product/list.phtml

<?php echo$_item->getTest(); ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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