I've been battling with this issue for quite some time but have yet to find a good solution. I am trying to programmatically search products within a certain category. I do not want to build my own search functionality and want to use the standard Magento 'catalogsearch' module. My current code looks like
$catalogSearchModel = Mage::getModel('catalogsearch/query')->setQueryText(search query);
$catalogSearchModelCollection = $catalogSearchModel->getResultCollection();
$catalogSearchModelCollection->getSelect()->limit(limit1, limit2);
$data = $catalogSearchModelCollection->getData();
$filters = array();
foreach ($data as $key => $value) {
$filters[] = array("attribute" => "entity_id", "eq" => $value["entity_id"]);
}
$productCollection->addAttributeToFilter($filters);
I use the product collection because I need more attributes for the products then there are available in the search model.
Is there a way to add a filter field to the catalog search collection, or something like that?
Best Answer
The result of
is an instance of
Mage_CatalogSearch_Model_Resource_Search_Collection
that extendsMage_Catalog_Model_Resource_Product_Collection
.So I conclude is save to use
addCategoryFilter
.Something like: