Search code examples
magentoproductinventory

How to get products with inventory greater than 0 and In Stock in Magento


How to get products which inventory is greater than 0 and "In Stock" (inventory and In Stock attribute is set in Admin panel)

This is my current code

$_productCollection = $product->getCollection()->addAttributeToSelect('*')
                                    ->addAttributeToFilter('visibility', 4) // Only catalog, search visiblity
                                    ->addAttributeToFilter('status', array('eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED))
                                    ->setCurPage(1)
                                    ->setPageSize(12);

What condition should i add to get what i want?


Solution

  • To know whether product is in stock or not, you can use isInStock().

       $id = 24 //product id
       $_product = Mage::getModel('catalog/product')->load($id);
    
       //check whether product is in stock
       if($_product->isInStock()){
    
           //do some stuff here
       }
    

    You can display | hide products without inventory in front-end through admin.For this..

    1. Go to System ->Configuration.

    2. Select appropriate scope.

    3. Select inventory tab under CATALOG

    4. There you have an option to set this

    enter image description here

    Hope it helps