Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Magento: list all values of a single attribute

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?

like image 398
flukee Avatar asked Mar 19 '13 17:03

flukee


2 Answers

    $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

like image 164
Umer Ahmed Khan Avatar answered Oct 30 '22 14:10

Umer Ahmed Khan


it will be just

$attribute = Mage::getModel('eav/config')->getAttribute(4,'artist');
if($attribute->usesSource()) {
    $options = $attribute->getSource()->getAllOptions(false);
    foreach($options as $key=>$value) {
        if(count($value)==2) {
            $artists[] = $value['label'];
        }
    }
}
like image 40
TaganPablo Avatar answered Oct 30 '22 12:10

TaganPablo