3

I need to get current product collection (actually I just need all Product IDS of products in current search result) of search result page in outside result.phtml, because I'm going to do something on layered navigation according to the product collection of search page can someone help me to get the product collection outside search result page

I have tried with

$term = Mage::helper('catalogsearch')->getQueryText();
    $collection = Mage::getResourceModel('catalogsearch/query_collection')->setQueryFilter($term)->getData();

but can not seems a way to return product IDs from above code.

7

Never mind I found a solution and it got work

$term = Mage::helper('catalogsearch')->getQueryText();
    $query = Mage::getModel('catalogsearch/query')->setQueryText($term)->prepare();
    $fulltextResource = Mage::getResourceModel('catalogsearch/fulltext')->prepareResult(
        Mage::getModel('catalogsearch/fulltext'),
        $term,
        $query
    );

    $collection = Mage::getResourceModel('catalog/product_collection');
    $collection->getSelect()->joinInner(
        array('search_result' => $collection->getTable('catalogsearch/result')),
        $collection->getConnection()->quoteInto(
            'search_result.product_id=e.entity_id AND search_result.query_id=?',
            $query->getId()
        ),
        array('relevance' => 'relevance')
    );

    $productIds = array();
    $productIds = $collection->getAllIds(); // as per Amit Bera s' comment
Zend_Debug::dump($productIds);
  • 2
    Chamal Chamikara,instead of get product ids from loop,you need to use $collection->getAllIds()better for getting data fast – Amit Bera Jul 7 '15 at 12:02
  • @AmitBera ah yes :) it worked well thanks – Chamal Chamikara Jul 7 '15 at 12:05

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.