0

In the category products page I need to display the products in order which we select from the option drop down (price, position or name). At the same time I need to move Out Of Stock products last.

I override the Mage_Catalog_Block_Product_List_Toolbar setCollection() function and added below code:

$outOfStock = array();
        foreach ($collection as $key => $item) {
            $product = Mage::getModel('catalog/product')->load($key);
            $inStock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getIsInStock();
            if ($inStock == 0) {
                $outOfStock[] = $product;
                $collection->removeItemByKey($key);
                //Mage::log($inStock, null, 'sssssssssss.log');
            }
        }

        // re-add $items
        foreach ($outOfStock as $item) {
            $collection->addItem($item);
        }

Using this code all the Out Of Stock products are moving to the end of the collection. But the issue is after adding above code pagination is NOT working correctly. All the products display in one page.

Can anyone please help me. Thank You.

0

I solve the issue. Actually I totally remove the above code I added. Then to do the task I need I override the Mage_Catalog_Model_Layer files. Override the below function:

public function getProductCollection()
    {
        if (isset($this->_productCollections[$this->getCurrentCategory()->getId()])) {
            $collection = $this->_productCollections[$this->getCurrentCategory()->getId()];
        } else {
            $collection = $this->getCurrentCategory()->getProductCollection();

            $collection->getSelect()->joinLeft(
                array('stock_status' => $collection->getTable('cataloginventory/stock_status')),
                'e.entity_id = stock_status.product_id AND stock_status.website_id='.Mage::app()->getWebsite()->getId()
            );
            $collection->getSelect()->order('stock_status DESC');

            $this->prepareProductCollection($collection);
            $this->_productCollections[$this->getCurrentCategory()->getId()] = $collection;
        }

        return $collection;
    }

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.