Home    Articles

 

How to get values of a multiple-select attribute for a product in magento

 

I faced this problem many times, and somehow I managed to solve it, until recently when I needed lots of attributes displayed on the product listing page.

This is not a very easy task, so you need to read this tutorial. Getting the code without knowing what is happening will not help you. Read it, it will only take you 10 min.

I will show you how to extract one attribute that is a multiple-select one, so you can do it for any attribute.

First step is to get the id of the multiple-select attribute.
Do not confuse the attribute id with the values of the attribute.
Let’s call our multiple-select attribute “series”.
$seriesAttributeId will be an integer (ex:142).

<?php
$seriesAttributeId = Mage::getResourceModel("eav/entity_attribute")->getIdByCode("catalog_product","series");
?>

In the 2′nd step we need to get all the values of the “series” attribute that are related to the product:
$read will be the instance of the Zend Database class that magento uses for queries.

<?php
$read = Mage::getSingleton('core/resource')->getConnection('core_read');
$select = $read->select();
$select->from("catalog_product_entity_varchar")
       ->where("attribute_id = ?", $seriesAttributeId)
       ->where("entity_id IN (?)", $_product->getId());
$result = $read->query($select);
$result = $result->fetch();
//print_r the $result will look like this:
/*
Array
(
    [value_id] => 212
    [entity_type_id] => 4
    [attribute_id] => 142
    [store_id] => 0
    [entity_id] => 9
    [value] => 3,4
)
*/
 
//we build a simple array with the selected options
$attributeOptionIds = array();
$attributeOptionIds = array_merge($attributeOptionIds, explode(',', $result['value']));
array_unique($attributeOptionIds);
//$attributeOptionsIds
/*
Array
(
    [0] => 3
    [1] => 4
)
*/
?>

We are interested in this field from the array [value] => 3,4.
This will tell us that there are 2 selected options of the attribute for this product with id = 9;
Now we need to extract the string value of the attributes options with id 3 and 4.

Building another query to get the options value for this product:

<?php
$select = $read->select();
if(!empty($attributeOptionIds[0]))
		{
		 $select->from("eav_attribute_option_value")
                        ->where("option_id IN (". implode(",", $attributeOptionIds).")")
                        ->group("value");
 
		 $result = $read->query($select);
		 $result = $result->fetchAll(); 
		}
		else
		{
		  $result = array();	
		}
// $result will look like this:
/*
Array
(
    [0] => Array
        (
            [value_id] => 118
            [option_id] => 4
            [store_id] => 0
            [value] => 100 Days
        )
 
    [1] => Array
        (
            [value_id] => 116
            [option_id] => 3
            [store_id] => 0
            [value] => Retro
        )
 
)
*/	
?>

Now we have the values for the selected options of the “series” attribute which is a multiple-select type.

The values are 100 Days and Retro.

Using a foreach loop you can display them.

That’s all. Enjoy.

by