1

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?

2

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);
  • Oh my! It's so simple! Thank you so much! I was on the right track, but I was putting the cat_id in the addCategoryFilter function. Thank you! – Tim Wachter Feb 3 '14 at 9:27
  • I have checked the Mage_CatalogSearch_Model_Resource_Search_Collection and Mage_Catalog_Model_Resource_Product_Collection, but unfortunately i did not able to find the "$catalogSearchModel->getResultCollection();" line can you able to me guide in where i need to do the mentioned changes – senthil Dec 7 '17 at 16:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.