1

I'm trying to get the product collection containing only the products that appear in a search, from inside app\design\frontend[theme]\catalog\layer\view.phtml

I've tried:

$search = Mage::getSingleton('catalogsearch/advanced');
$searchCollection = $search->getProductCollection()->addAttributeToFilter('visibility', 4);

and

$searchCollection = Mage::getModel('catalog/category')->getProductCollection();
$searchCollection = Mage::getSingleton('catalogsearch/advanced')->prepareProductCollection( $searchCollection );
$searchCollection = $searchCollection->getProductCollection();

Both of these return every single product on the site, not just the ones in the search results.

Can anyone help?

2

This was the only working solution I could find: https://stackoverflow.com/questions/20978893/whats-the-proper-way-to-call-catalogsearch-from-a-custom-block-in-magento

Though I did replace

$searchText = 'ipad';

with

$searchText = Mage::helper('catalogsearch')->getQueryText();

to make it dynamic.

  • if u got the answer. then close the question. – Helphin Mar 18 '14 at 8:33
  • This is a working solution, but it is bulky. I'm pretty sure there is a better solution out there. – Tyler V. Mar 18 '14 at 17:32
1

Well we know the product collection is loaded and present somewhere, so rather than querying the database again, if we know we are in the context of a search results page it is probably safe to just get the collection from the list block.

On a /catalogsearch/result page the list block will be at search_result_list, so we can fetch it from any block or template with:

$oListBlock = $this->getLayout()->getBlock('search_result_list')

And from here we can get the product collection off the block with:

$oListBlock->getLoadedProductCollection();

Also the Mage_Catalog_Block_Product_List::getLoadedProductCollection() method calls Mage_Catalog_Block_Product_List::_getProductCollection which does some digging for a usable product collection, which is handy if you're writing a block that needs to be used in a few different circumstances.

  • worked great and seems the simplest way to accomplish this and the most D.R.Y friendly method – asherrard Jun 27 '17 at 21:40
-1

Are you looking for this,

$search = Mage::getModel('catalog/product')->getCollection();
$searchCollection = $search->addAttributeToFilter('visibility', '3');
  • I don't think so, no. I am on the URL /catalogsearch/result/?q=example and trying to get the collection of products that matches the query "example" from inside app\design\frontend[theme]\catalog\layer\view.phtml so that I can make a list of categories that are used by the products returned in the search. I can't seem to get a collection that only contains the search results from this file. (Obviously app\design\frontend[theme]\catalog\product\list.phtml just uses $this->getLoadedProductCollection() but that's not available in layer.phtml) – Tyler V. Mar 14 '14 at 17:00

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.