Search code examples
phphtmlmagentomagento-1.8

Magento - How to display products below no results page?


Basically if a user searches for a product and the search returns emtpy, I want to be able to display a specific category under the text "No results matched your search".

I've tried different approaches to this, but it seems the results.phtml sorts every non-matching product from the search query string. It doesn't matter if the string matches a certain group of products - it will only display these products.

I'm trying to use a simple code:

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

This piece of code works anywhere on the template pages except results.phtml since the search feature blocks all non-matching products. (It will display a product if it matches the search using this code block).

Similar problem here, but no solution: Display specific category products on no results search page

Thanks, appreciate any answer to this question :-)


Solution

  • You can create custom block as follows and set pagination like here

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

    UPDATE

    • Create Module xml file

    app/etc/modules/Custom_Products.xml

    as follows

    <config>
        <modules>
            <Custom_Products>
                <active>true</active>
                <codePool>local</codePool>
            </Custom_Products>
        </modules>
    </config> 
    
    • Create

    app/code/local/Custom/Products/etc/config.xml

    file as follows

    <config>
        <global>
            <blocks>
                <customproducts>
                    <class>Custom_Products_Block</class>
                </customproducts>
            </blocks>
        </global>
    </config>
    
    • Create

    app/code/local/Custom/Products/Block/Customlist.php

    file as follows

    <?php
    class Custom_Products_Block_Customlist extends Mage_Core_Block_Template
    {
    
        protected function _construct()
        {
            parent::_construct();
    
            // We get our collection through our model
            $this->_collection = Mage::getModel('catalog/category')->load($category_id)
            ->getProductCollection()
            ->addAttributeToSelect('*') // add all attributes - optional
            ->addAttributeToFilter('status', 1) // enabled
            ->addAttributeToFilter('visibility', 4) //visibility in catalog,search
            ->setOrder('price', 'ASC'); //sets the order by price
    
            // Instantiate a new Pager block
            $pager = new Mage_Page_Block_Html_Pager();
    
    
            // We set our limit (here an integer store in configuration).
            // /!\ The limit must be set before the collection
            $pager
            ->setLimit(5)
            ->setCollection($this->_collection);
    
            // Add our Pager block to our current list block
            $this->setChild('pager', $pager);
        }
    
    }
    ?>
    
    • Create

    app/design/YOUR_PACKAGE/YOUR_THEME/template/catalog/product/customlist.phtml

    as follows

    <?php    
        $_productCollection=$this->_collection;
        ...
        your products code here
        ...     
    
    ?>
    
    <?php   
       echo $this->getChildHtml('pager');
    ?>
    

    Check if it is working for you.