3

I have problem when I try to load multiselect values for product. I have:

$product->getSportTypes()

But these can give me only "sport type" Ids, not and names. How to get names?

3 Answers 3

5

I suppose you want to display the value of the multiselect attribute named sport_types in the frontend. To do so you need to follow below steps:

$sportTypes = explode(",",$_product->getResource()
->getAttribute('sport_types')->getFrontend()
->getValue($_product));

foreach($sportTypes as $key => $value){
        echo $value;
    }
1
  • My Magento version uses an extra space for delimiter, i.e. ", " instead of ",". You might need that too.
    – mimarcel
    Jul 8, 2015 at 17:25
4

Better use:

foreach($product->getAttributeText('sport_types') as $type) {
    echo $type;
}

Amit already mentioned this but I want to make sure, that everyone understands, that getAttributeText returns you the attribute name for selects and an array for multiselects.

1
  • Much cleaner solution than the others. Thanks Fabian! Jan 26, 2016 at 13:27
2

Magento:

$product->getSportTypes(); give array of option ids

And below give array of option

 $product->getAttributeText('sport_types');
$attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product','sport_types');
 $collection =Mage::getResourceModel('eav/entity_attribute_option_collection')
                ->setPositionOrder('asc')
                ->setAttributeFilter($attributeId)
                ->setStoreFilter(Mage::app()->getStore()->getId())
                ->load();
                echo "<pre>";
                print_r($collection->toOptionArray());
                echo "</pre>";

For getting all Selected option of shop_types attribute try below code

$Collection=Mage::getModel('catalog/product')->getCollection()->addFieldToSelect('sport_types'')->groupByAttribute('sport_types'');
5
  • These only works if only one value is selected, but if i select more, it will return false.
    – Milos
    Jul 7, 2014 at 11:27
  • then what you want???want list all option of this attribute
    – Amit Bera
    Jul 7, 2014 at 11:30
  • I want to get all names from sport types that are selected
    – Milos
    Jul 7, 2014 at 11:31
  • over all products or only this products
    – Amit Bera
    Jul 7, 2014 at 11:36
  • For current product, load sport types names as string
    – Milos
    Jul 7, 2014 at 11:45

Your Answer

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

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