Magento 1.7 CatalogSearch – Display Category Products in Empty Search Result

catalogsearchmagento-1.7product-list

In empty search result page, I need to display the product list of a particular category. So, I have added the code

<?php echo $this->getLayout()->createBlock('catalog/product_list')->setCategoryId(5)->setTemplate('catalog/product/list.phtml')->toHtml()?> 

in catalogsearch/result.phtml. But this is not worked.

While check the flow, I have found that $this->getLayer() calls the catalogsearch in app/code/core/Mage/Catalog/Block/Product/List.php. Can I change the layer as Catalog, if the search is empty. If it is possible to change the layer, then how to do that?

Best Answer

Write down below code in your result.phtml

  1. check weather search result contains result record more then 1.
  2. If not write down following code...

    $category_id=2; // your category id here..

    $category = Mage::getModel('catalog/category')->load(trim($category_id));
    
        foreach ($category->getProductCollection() as $product) {
    
        $product= Mage::getModel('catalog/product')->load($product->getId());
        echo $product->getData("name");
    
    }
    
Related Topic