Magento: list all values of a single attribute

flukee picture flukee · Mar 19, 2013 · Viewed 19.4k times · Source

I'm trying to list up all the existing values of a newly created attribute in magento 1.7.0.2. (and make them clickable links, so that on click they list up all of the items with the specific attribute value, but that's not a priority right now)

The attribute code is "artist"

So far i created the file Artist.php in app/code/core/Mage/Catalog/Block/ with the following code:

public function getAllArtists()
{
$product = Mage::getModel('catalog/product');
$attributes = Mage::getResourceModel('eav/entity_attribute_collection')
    ->setEntityTypeFilter($product->getResource()->getTypeId())
    ->addFieldToFilter('attribute_code', 'artist');
$attribute = $attributes->getFirstItem()->setEntity($product->getResource());
$artists = $attribute->getSource()->getAllOptions(false);
  return $artists;  
}

and the file artist.phtml in /app/design/frontend/default/template-name/template/catalog/product with this code:

 <ul id="artist_list">
  <?php foreach ($this->getAllArtists() as $artist): ?> 
  <li><a href="<?php Mage::getURL() ?>catalogsearch/advanced/result/?&artist;[]=<?php echo $artist['value'] ?>&search;="><?php echo $artist['label'] ?></a></li>
  <?php endforeach; ?>
 </ul>

which i then call in a static block with

{{block type="core/template" template="catalog/product/artist.phtml"}}

but nothing appears...

i used the code from this thread: http://www.magentocommerce.com/boards/viewthread/19982/P0/

the attribute is set "Visible on Product View Page on Front-end" and i call the attribute value of each item in the ../template/product/view.phtml with

<?php echo $_product->getData('artist') ?> 

and it correctly displays the value.

any ideas?

Answer

Umer Ahmed Khan picture Umer Ahmed Khan · Oct 24, 2013
    $name='whatever_your_attribute_name';
    $attributeInfo = Mage::getResourceModel('eav/entity_attribute_collection')->setCodeFilter($name)->getFirstItem();
    $attributeId = $attributeInfo->getAttributeId();
    $attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
    $attributeOptions = $attribute ->getSource()->getAllOptions(false); 
    print_r($attributeOptions);

Ref: Magento - get all attribute value