1

I am facing an issue in Magento 1.9 where in product listing page every thing is working fine, We have a number of sorting options for products and a paging tab, now our requirement is to show first in-stock items in a list and after that, it should show out-of-stock items if any.

I tried below code in /app/code/local/Mage/Catalog/Block/Product/List/Toolbar.php

public function setCollection($collection)
    {
        $this->_collection = $collection;
        $this->_collection->setCurPage($this->getCurrentPage());

        // we need to set pagination only if passed value integer and more that 0
        $limit = (int)$this->getLimit();
        if ($limit) {
            $this->_collection->setPageSize($limit);
        }
        if ($this->getCurrentOrder()) {
                $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
                $this->_collection->getSelect()
                ->joinLeft(
                            array('sfoi' => $this->_collection->getResource()->getTable('cataloginventory/stock_item')),
                             'e.entity_id = sfoi.product_id'
                             ,array('qty' => 'sfoi.qty')
                         )
                     //->group('e.entity_id')
                     ->order('sfoi.qty DESC');

                //echo $this->_collection->getSelect();exit;

        }
        return $this;
    }

The above code is sorting products first with in-stock but it is having issues -

1- it showing products from out of the category which is not assigned to it. 2- products are sorted as per quantity, so it will change the alignment of products when quantity changed.

Any help will be helpful to me.

Thanks
EDIT
====

After doing R & D , I come to know that if you have allowed products in Backorders then the auto updation by magento to update status to `out of stock` has been stoped, i.e. if a product has been out of stock, magento will still show and keep status `in stock`, so that customers can add them to back orders.

Now my issue is that , I still want to show those products above in a list which is actually are in stock or you can say that greater than 0 in quantity, will any one please let me help on this , Thanks

  • Have you use array('qty') instead of array('qty' => 'sfoi.qty') ? – zed Blackbeard Jul 17 '17 at 8:46
  • @zedBlackbeard, yes, I tried but same result, actually, problem is that it taking extra products in join it should show only those which are assigned to that category – amit_game Jul 17 '17 at 9:07
1

Try this, I hope this will solve your problem.

public function setCollection($collection)
    {
        $this->_collection = $collection;
        $this->_collection->setCurPage($this->getCurrentPage());

        // we need to set pagination only if passed value integer and more that 0
        $limit = (int)$this->getLimit();
        if ($limit) {
            $this->_collection->setPageSize($limit);
        }
        if ($this->getCurrentOrder()) {
                $this->_collection->joinField('inventory_in_stock', 'cataloginventory_stock_item', 'is_in_stock', 'product_id=entity_id','is_in_stock>=0', 'left')->setOrder('inventory_in_stock', 'desc');
                $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
        }
        return $this;
    }

Source : ShowInStockFirst

  • I tried above, but still no result, now the in-stock first concept has gone, and showing simple list of products by default was coming – amit_game Jul 17 '17 at 10:04
  • Have you checked with whole module ? – zed Blackbeard Jul 17 '17 at 10:07
  • yes, just copy and paste your code. – amit_game Jul 17 '17 at 10:17
0
$productCollection = Mage::getResourceModel('catalog/product_collection')
                        ->addAttributeToSelect(array('name', 'image', 'price'))
                        ->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED));

Mage::getSingleton('cataloginventory/stock')
    ->addInStockFilterToCollection($productCollection);

$this->_productCollection = $productCollection;
  • how to use this code , ? – amit_game Jul 17 '17 at 8:38

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.