Question

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?

Was it helpful?

Solution

The result of

$catalogSearchModelCollection = $catalogSearchModel->getResultCollection();

is an instance of Mage_CatalogSearch_Model_Resource_Search_Collection that extends Mage_Catalog_Model_Resource_Product_Collection.
So I conclude is save to use addCategoryFilter.
Something like:

$category = Mage::getModel('catalog/category')->load(22);
$catalogSearchModelCollection = $catalogSearchModel->getResultCollection();
$catalogSearchModelCollection->addCategoryFilter($category);
Licensed under: CC-BY-SA with attribution
Not affiliated with magento.stackexchange
scroll top