Are you sure the error you are getting? In Magento 1.7.x, when I replace the body of the prepareProductCollection method prepareProductCollection one above, I get this error
SQLSTATE[42S22]: Column not found: 1054 Unknown column 'e.min_price' in 'where clause'
This is because my prepareProductCollection did not set the same properties as the original. To get started, make sure you have one. If you do this using configuration-based rewriting, call
parent::prepareProductCollection($collection);
at the beginning of your method. If you use override of the local or community pool, then you need to copy / paste the code from the core file. In 1.7.1 it will look like
$collection ->addAttributeToSelect(Mage::getSingleton('catalog/config')->getProductAttributes()) ->addMinimalPrice() ->addFinalPrice() ->addTaxPercents() ->addUrlRewrite($this->getCurrentCategory()->getId()); Mage::getSingleton('catalog/product_status')->addVisibleFilterToCollection($collection); Mage::getSingleton('catalog/product_visibility')->addVisibleInCatalogFilterToCollection($collection); $collection ->addAttributeToSelect('type_id') ->addAttributeToFilter('type_id','simple');
If this does not help you, trace your column - if your error is really 1054 Unknown column 'e.type_id' in 'where clause' , then I assume that you have some kind of custom code that works somewhere that copies collection filters to another collection.
Alternatively, you can get the initial SQL collection query using
echo $collection->getSelect()->__toString();
This should give you enough debugging information.
Update: OK, in accordance with the new information above, this is displayed only when there is a price filter and / or when the indices are in a certain state.
Here is your problem. Take a look at this call stack line.
If you go to getCount method, you will see _getSelect call
#File: app/code/core/Mage/Catalog/Model/Resource/Layer/Filter/Price.php public function getCount($filter, $range) { $select = $this->_getSelect($filter);
If you look at the definition of _getSelect ,
#partial method reproduction #File: app/code/core/Mage/Catalog/Model/Resource/Layer/Filter/Price.php protected function _getSelect($filter) { $collection = $filter->getLayer()->getProductCollection(); $collection->addPriceData($filter->getCustomerGroupId(), $filter->getWebsiteId()); if (!is_null($collection->getCatalogPreparedSelect())) { $select = clone $collection->getCatalogPreparedSelect(); } else { $select = clone $collection->getSelect(); }
you will see that Magento is cloning a selection from a collection, and then changing it to select from an index table
// processing FROM part $priceIndexJoinPart = $fromPart[Mage_Catalog_Model_Resource_Product_Collection::INDEX_TABLE_ALIAS]; $priceIndexJoinConditions = explode('AND', $priceIndexJoinPart['joinCondition']); $priceIndexJoinPart['joinType'] = Zend_Db_Select::FROM; $priceIndexJoinPart['joinCondition'] = null; $fromPart[Mage_Catalog_Model_Resource_Product_Collection::MAIN_TABLE_ALIAS] = $priceIndexJoinPart; unset($fromPart[Mage_Catalog_Model_Resource_Product_Collection::INDEX_TABLE_ALIAS]); $select->setPart(Zend_Db_Select::FROM, $fromPart); foreach ($fromPart as $key => $fromJoinItem) { $fromPart[$key]['joinCondition'] = $this->_replaceTableAlias($fromJoinItem['joinCondition']); } $select->setPart(Zend_Db_Select::FROM, $fromPart);
Since there is no type_id column in the index table, the Magento stock code above is not compatible with your change. If I was going to continue this, I would
Switch to rewrite based approach
Create a conversation that will add your type_id filter instead of your override
Create a second rewrite using the _getSelect method above, which checks the selection for the type_id filter, and if it finds it, removes it
Alternatively, you can try to find another method to overwrite the collection object, which is closer to where it was used.
Good luck