I have created a custom attribute for a Product, with the code »gender«, as a dropdown list. I has, unsurprisingly, two possible values: »male« and »female« and the properties table looks like this:
I have created a custom block, extending Mage_Catalog_Block_Product_List
and added a method addFilterAttribute($key, $value)
, so that I can instantiate the block like that in the modules layout update:
<reference name="content">
<block type="mymodule/page_filtered" name="mymodule.men.content">
<action method="addFilterAttribute">
<key>gender</key>
<value>male</value>
</action>
</block>
</reference>
So as one might expect, I would like to create a list/grid of Products containing all the products for male customers. But since the value I pass to the block is the admin label for the corresponding value, I do not get any products listed.
Here is the code I am using in the block:
class Namepace_Module_Block_Page_Filtered
extends Mage_Catalog_Block_Product_List
{
private $attributes;
public function _construct()
{
$this->attributes = array();
$this->setTemplate = 'page/list.phtml';
}
public function addFilterAttribute($key, $value)
{
$this->attributes[$key] = $value;
}
public function getProducts()
{
$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('*');
foreach($this->attributes as $key => $value)
{
$collection->addAttributeToFilter($key, array('eq' => $value));
}
$collection->load();
return $collection;
}
}
But as I said, the list is empty, what can I do?
EDIT
I tested some values, and if I change the xml
to:
<reference name="content">
<block type="mymodule/page_filtered" name="mymodule.men.content">
<action method="addFilterAttribute">
<key>gender</key>
<value>9</value>
</action>
</block>
</reference>
I does the Job, but I do not want to fiddle with numerical attribute values, since the would be very inconvenient in configuration.