Showing posts with label out of stock. Show all posts
Showing posts with label out of stock. Show all posts

Friday, 20 January 2012

Magento 1.5: Keep out of stock product always at last in the product listing page

Not a good habit  of changing in the core files, first keep in side local folder if any changes required in core files.
You will get less pain during magento version up gradation.

Below code will help you to keep out of stock products in magento always at  last in the product listing page .

First copy Collection.php file from /app/code/core/Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Collection.php  to /app/code/local/Mage/Catalog/Model/Resource/Eav/Mysql4/Product/Collection.php folder structure .

Put below code:

$this->getSelect()->joinLeft(
            array('_inventory_table' => $this->getTable('cataloginventory/stock_status')), "_inventory_table.product_id = e.entity_id", array('stock_status'));

    $this->getSelect()->order('stock_status DESC');

Beginning of the function addAttributeToSort(). See below how it will look like


 public function addAttributeToSort($attribute, $dir='asc') {

 
    $this->getSelect()->joinLeft(
            array('_inventory_table' => $this->getTable('cataloginventory/stock_status')), "_inventory_table.product_id = e.entity_id", array('stock_status'));

    $this->getSelect()->order('stock_status DESC');


Now you are done.

Good luck