I've created a custom filter in manage products page and I want to filter from Mage::getModel('catalog/product')->getCollection()
values by attribute name.
UPDATE:
protected function _prepareCollection() {
if(Mage::getStoreConfig(self::CONFIG_ENABLED)) {
$store = $this->_getStore();
$collection = Mage::getModel('catalog/product')->getCollection()
->addAttributeToSelect('sku')
->addAttributeToSelect('name')
->addAttributeToSelect('attribute_set_id')
->addAttributeToSelect('type_id')
->joinField('qty',
'cataloginventory/stock_item',
'qty',
'product_id=entity_id',
'{{table}}.stock_id=1',
'left');
if ($store->getId()) {
$collection->setStoreId($store->getId());
$adminStore = Mage_Core_Model_App::ADMIN_STORE_ID;
$collection->addStoreFilter($store);
$collection->joinAttribute('name', 'catalog_product/name', 'entity_id', null, 'inner', $adminStore);
$collection->joinAttribute('custom_name', 'catalog_product/name', 'entity_id', null, 'inner', $store->getId());
$collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner', $store->getId());
$collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner', $store->getId());
$collection->joinAttribute('price', 'catalog_product/price', 'entity_id', null, 'left', $store->getId());
}
else {
$collection->addAttributeToSelect('price');
$collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
$collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
}
$filter = $this->getParam($this->getVarNameFilter(), null);
if (!is_null($filter)) {
$filter = $this->helper('adminhtml')->prepareFilterString($filter);
if (isset($filter['attribute'])) {
$attributeCode = $filter['attribute'];
$collection->joinAttribute($attributeCode, 'catalog_product/'.$attributeCode, 'entity_id', null, 'left');
$collection->addAttributeToSelect($attributeCode);
}
}
$this->setCollection($collection);
Mage_Adminhtml_Block_Widget_Grid::_prepareCollection();
$this->getCollection()->addWebsiteNamesToResult();
} else {
parent::_prepareCollection();
}
return $this;
}
protected function _prepareColumns() {
parent::_prepareColumns();
if(Mage::getStoreConfig(self::CONFIG_ENABLED)) {
$ignoreAttributes = array('name', 'attribute_set_id', 'type_id', 'qty', 'price', 'status', 'visibility');
$collection = Mage::getResourceModel('catalog/product_attribute_collection')
->addVisibleFilter();
$result = array();
foreach ($collection as $model) {
if(in_array($model->getAttributeCode(), $ignoreAttributes)) {
continue;
}
$productCollection = Mage::getModel('catalog/product')->getCollection();
$productCollection->addAttributeToSelect(array($model->getAttributeCode()));
$productCollection->addAttributeToFilter($model->getAttributeCode(), array('gt' => 0));
if(count($productCollection->getData()) > 0) {
$result[$model->getAttributeCode()] = $model->getAttributeCode();
}
}
$this->addColumn('attribute',
array(
'header'=> 'Attribute',
'width' => '150px',
'index' => 'attribute',
'type' => 'options',
'options' => $result,
'filter_condition_callback' => array($this, '_filterHasAttributeCallback')
));
$filter = $this->getParam($this->getVarNameFilter(), null);
if (!is_null($filter)) {
$filter = $this->helper('adminhtml')->prepareFilterString($filter);
if (isset($filter['attribute'])) {
$attributeCodeConfig = $filter['attribute'];
$attributeId = Mage::getResourceModel('eav/entity_attribute')->getIdByCode('catalog_product', $attributeCodeConfig);
$attribute = Mage::getModel('catalog/resource_eav_attribute')->load($attributeId);
$attributeData = $attribute->getData();
$frontEndLabel = $attributeData['frontend_label'];
$attributeOptions = $attribute->getSource()->getAllOptions();
$b = new Mage_Catalog_Model_Resource_Eav_Attribute();
$attributeOptions2 = array();
foreach ($attributeOptions as $value) {
if(!empty($value['value'])) {
$attributeOptions2[$value['value']] = $value['label'];
}
}
if(count($attributeOptions2) > 0) {
$this->addColumn($attributeCodeConfig,
array(
'header'=> Mage::helper('catalog')->__($frontEndLabel),
'width' => '80px',
'index' => $attributeCodeConfig,
'type' => 'options',
'options' => $attributeOptions2,
));
} else {
$this->addColumn($attributeCodeConfig,
array(
'header'=> Mage::helper('catalog')->__($frontEndLabel),
'width' => '80px',
'index' => $attributeCodeConfig,
));
}
}
}
}
}
protected function _filterHasAttributeCallback($collection, $column) {
if (!$value = $column->getFilter()->getValue()) {
return $this;
}
// STILL DON'T KNOW HOW TO FILTER BY ATTRIBUTE NAME
$this->getCollection()->getSelect()->where("main_table.? IS NOT NULL", $value);
return $this;
}
// STILL DON'T KNOW HOW TO FILTER BY ATTRIBUTE NAME
$this->getCollection()->getSelect()->where("main_table.? IS NOT NULL", $value);
After I select the attribute name for example 'abc_color', I want to filter all the products that use attribute 'abc_color'.