Magento List all in stock.

I am trying to create a cms page that lists all the items in stock.

protected function _getProductCollection()
   {

        $collection = Mage::getModel('catalog/product')->getCollection();
        $collection = $this->_addProductAttributesAndPrices($collection)
         ->addStoreFilter()
         ->addAttributeToSort('entity_id', 'desc') //<b>THIS WILL SHOW THE LATEST PRODUCTS FIRST</b>
         ->addAttributeToFilter('stock_status', 0)


         ->setPageSize($this->get_prod_count())
         ->setCurPage($this->get_cur_page());
        $this->setProductCollection($collection);
        return $collection;
   }

But it does not work. It lists all the items. What is a method for listing all elements that are qty = 0 or is_in_stock = 0?

+3
source share
3 answers

Here is the function I used

function getOutOfStockProducts()
{
        $products = Mage::getModel('catalog/product')
                ->getCollection()
                ->joinField(
                        'is_in_stock',
                        'cataloginventory/stock_item',
                        'is_in_stock',
                        'product_id=entity_id',
                        '{{table}}.stock_id=1',
                        'left'
                )
                ->addAttributeToFilter('is_in_stock', array('eq' => 0));

        return $products;
}
+7
source

for this you need to use another collection

$outOfStockItems = Mage::getModel('cataloginventory/stock_item')
        ->getCollection()
        ->addFieldToFilter('is_in_stock', 0);
+6
source

This is how I ended up using. Lists all items missing on the backend.

protected function _getProductCollection()
   {


         $stockCollection = Mage::getModel('cataloginventory/stock_item')->getCollection()

        /*->addFieldToFilter('is_in_stock', 0);*/ //this can also be used to filter
        ->addFieldToFilter('qty', 0);


$productIds = array();

    foreach ($stockCollection as $item) {
        $productIds[] = $item->getOrigData('product_id');
    }

    $productCollection = Mage::getModel('catalog/product')->getCollection();
    $productCollection = $this->_addProductAttributesAndPrices($productCollection)
        ->addIdFilter($productIds)
         ->addAttributeToSort('entity_id', 'desc') //THIS WILL SHOW THE LATEST PRODUCTS FIRST
         ->setPageSize($this->get_prod_count())
         ->setCurPage($this->get_cur_page());
        $this->setProductCollection($productCollection);
        return $productCollection;
   }// _getProductCollection
}// Mage_Catalog_Block_Product_Outofstock
+2
source

All Articles