6

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?

flukee
  • 121
  • 1
  • 3
  • 12

2 Answers2

23
    $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

Community
  • 1
  • 1
Umer Ahmed Khan
  • 246
  • 2
  • 4
1

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'];
        }
    }
}
TaganPablo
  • 359
  • 1
  • 4
  • still not working.. i replaced the code in public function with yours and it still wont display anything. i am a noob in php, i have to say too – flukee Mar 27 '13 at 10:26
  • Ah, in static block you should use block type = catalog/artist – TaganPablo Mar 27 '13 at 19:05
  • here is what i got sofar: Artist.php contains the following: `public function getAllArtists() { $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) { $artist[] = $value['label']; } } } }` – flukee Mar 29 '13 at 09:52
  • the artist.phtml contains `
      getAllArtists() as $artist): ?>
    `
    – flukee Mar 29 '13 at 09:54
  • and in the static bock: `{{block type="catalog/artist" template="catalog/product/artist.phtml"}}` – flukee Mar 29 '13 at 09:58
  • ok, after another try i got "syntax error, unexpected T_PUBLIC error" so i edited the "public" in public function out and now i get this error: Class 'Mage_Catalog_Block_Artist' not found – flukee Mar 29 '13 at 10:06
  • ok - you also probably lost "return $artist;" at the end of function – TaganPablo Mar 29 '13 at 12:31
  • as for Class 'Mage_Catalog_Block_Artist' - you can just place your file at app/code/local/Mage/Catalog/Block/ – TaganPablo Mar 29 '13 at 12:32
  • `class Mage_Catalog_Block_Artist { public function getAllArtists() { $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) { $artist[] = $value['label']; } } } return $artist; } }` – TaganPablo Mar 29 '13 at 12:33
  • ok, using the code above in Artist.php, it still nothing shows up. also, my firebug tells me that the static block doesn't get called. maybe that has something to do with it – flukee Mar 29 '13 at 13:26
  • if you had an error - Class 'Mage_Catalog_Block_Artist' not found - previously - it means that this block was called. – TaganPablo Mar 29 '13 at 14:39
  • well then something must went wrong somewhere else, but where? maybe could you please explain how your code works? – flukee Mar 29 '13 at 16:28