Magento – Output all product attribute values on catalog page

catalogtemplatetheme

On the catalog grid page, I want to output all filterable attribute values for each product. For example: If the price is filterable in the layered navigation, the attribute value associated with the product would be output (£0.00 – £999.99, £20,000 and above, etc).

I have the start of something but can't work out how to filter by product only and grab the value:

            $_productId = $_product->getProductId();
            $product = Mage::getModel('catalog/product')->load($productId);
            $attributes = $product->getAttributes();
             foreach ($attributes as $attribute) {
                echo $attribute->getAttributeCode();

              }

This just seems to output every attribute code on every product.

This is how I think it should be represented in terrible pseudocode:

get allProductAttributes
foreach allProductAttributes as attribute
    if attribute->isFilterable
        echo attribute->getAttributeValue
    endif
endforeach

Thanks

Best Answer

First idea : Change all the attributes, so "Used in Product Listing" is the same as "Use In Layered Navigation", then you have only the attributes in the product which are filterable.

Second idea: Write an observer and change the attributes which are selected.

$collection = Mage::getResourceModel('catalog/product_attribute_collection');
$collection
   ->setItemObjectClass('catalog/resource_eav_attribute')
   ->setAttributeSetFilter($setIds)
   ->addStoreLabel(Mage::app()->getStore()->getId())
   ->setOrder('position', 'ASC')
   ->addIsFilterableFilter();

This way you get the filterable attributes. And with the following code, you get the ones from the product listing:

$collection = Mage::getResourceModel('catalog/product_attribute_collection');
$collection
   ->setItemObjectClass('catalog/resource_eav_attribute')
   ->setAttributeSetFilter($setIds)
   ->addStoreLabel(Mage::app()->getStore()->getId())
   ->setOrder('position', 'ASC')
   ->addIsFilterableFilter();
   ->addFieldToFilter('additional_table.used_in_product_listing', array('gt' => 0));

Then you can loop over the ones from the losting, remove them all with $collection->removeAttributeToSelect() and afterwards you add all the filterable attributes.

But I recommend the first idea, then you don't have to code.