2

I want to show out of stock products at the end of product search results page. I have tried to do that by using some free extension/code but none of code didn't worked for search results page. please suggest me how can I do that.

2

Sorry if solution isn't very clean. I just copied from one of the modules which has been written many years ago...

etc/config.xml

        <catalog_product_collection_load_before>
            <observers>
                <product_sort>
                     <type>singleton</type>
                     <class>custom/observer</class>
                     <method>catalogProductSort</method>
                </product_sort>
            </observers>
        </catalog_product_collection_load_before>

Observer method:

public function catalogProductSort($observer) {
    $collection = $observer->getEvent()->getCollection();

    if ( !($collection instanceof Mage_Catalog_Model_Resource_Product_Collection)
        || !Mage::helper('catalog')->isModuleEnabled('Mage_CatalogInventory')
    ) {
        return ;
    }

    $allowedClasses = array(
        'Mage_Catalog_Model_Resource_Product_Collection',
        'Mage_CatalogSearch_Model_Resource_Fulltext_Collection',
        'Activo_CatalogSearch_Model_Resource_Fulltext',
    );
    if ( !in_array( get_class($collection), $allowedClasses) ) {
        return ;
    }

    $collection->joinField('is_in_stock',
        'cataloginventory/stock_item',
        'is_in_stock',
        'product_id=entity_id',
        '{{table}}.stock_id=1',
        'left');

    $select = $collection->getSelect();
    $orders = $select->order( 'is_in_stock '.Zend_Db_Select::SQL_DESC)
            ->getPart('order');
    array_unshift($orders, array_pop($orders));
    $select->setPart('order',$orders);
}

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.