Magento: list all values of a single attribute
Asked Answered
S

2

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?

Spleeny answered 19/3, 2013 at 17:26 Comment(0)
P
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

Plessor answered 24/10, 2013 at 11:37 Comment(1)
Thanks, it is helpful to load model by id here if attribute values were set from admin panelRotherham
C
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'];
        }
    }
}
Chippewa answered 19/3, 2013 at 20:25 Comment(12)
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 tooSpleeny
Ah, in static block you should use block type = catalog/artistChippewa
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']; } } } }Spleeny
the artist.phtml contains <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>Spleeny
and in the static bock: {{block type="catalog/artist" template="catalog/product/artist.phtml"}}Spleeny
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 foundSpleeny
ok - you also probably lost "return $artist;" at the end of functionChippewa
as for Class 'Mage_Catalog_Block_Artist' - you can just place your file at app/code/local/Mage/Catalog/Block/Chippewa
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; } }Chippewa
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 itSpleeny
if you had an error - Class 'Mage_Catalog_Block_Artist' not found - previously - it means that this block was called.Chippewa
well then something must went wrong somewhere else, but where? maybe could you please explain how your code works?Spleeny

© 2022 - 2024 — McMap. All rights reserved.