Posting in the Magento forums has been disabled pending the implementation of a new and improved forum solution which should better serve the community.

For new questions please post at magento.stackexchange.com, the community-run support site for the Magento community. We will be providing updates on the new forum solution soon. For questions or concerns please email community@magento.com.

Magento Forum

Page 1 of 2
How do I list ‘Out of Stock’ products
 
IrishC
Sr. Member
 
Avatar
Total Posts:  89
Joined:  2008-01-29
 

Hi,

We pretty much know the answer to this is ‘no’ already, but is there anyway to generate a report or filter the search results to see which products are ‘Out of Stock’?

IF NOT....  (please keep reading, we are desperate for an interim solution)....

Can anyone give us any pointers on the syntax of a standalone script which would loop through the products in the database and output (on-screen) the names of the ones that are out of stock… for example....

This is just pseduo/imaginary code.....

require_once MAGENTO.'/app/Mage.php';
Mage::app();

foreach (
$_products->getItems() as $_product){
if($_product->SomeOutofStockFunction()==1){
   
echo $_product->getName();
 
}
}

Can anyone help me to realise this please?

Thanks so much.

C

 
Magento Community Magento Community
Magento Community
Magento Community
 
MageParts
Guru
 
Avatar
Total Posts:  415
Joined:  2007-11-18
 

Yo, got a solution for you ^_^. You can use this pretty much anywhere you like to, I placed it in a block (for testing purposes).

$stockCollection Mage::getModel('cataloginventory/stock_item')->getCollection()
        ->
addFieldToFilter('is_in_stock'0);
        
    
$productIds = array();
            
    foreach (
$stockCollection as $item{
        $productIds[] 
$item->getOrigData('product_id');
    
}
        
    $productCollection 
Mage::getModel('catalog/product')->getCollection()
        ->
addIdFilter($productIds)
        ->
addAttributeToSelect('name');
                    
    
$products = array();
            
    foreach (
$productCollection as $product{
        $products[] 
$product->getData('name');
    
}
        
    
// die(print_r($products));

The block I used for testing this can be found here: app/code/core/Mage/Catalog/Block/Product/List.php but as said, you should be able to place it pretty much anywhere in Magento. This procedure can probably be made in an easier way, but at least this works. Happy coding wink

 
Magento Community Magento Community
Magento Community
Magento Community
 
IrishC
Sr. Member
 
Avatar
Total Posts:  89
Joined:  2008-01-29
 

Bock, thanks a million!! :D Just tested and its working fine!!

Suppose the next step is trying to integrate it into the admin somehow, but thats a whole other problem lol.  I’ll bookmark that script as an extra little tool and call on it when needed!  Excellent stuff.

Thanks mate!

C

 
Magento Community Magento Community
Magento Community
Magento Community
 
MageParts
Guru
 
Avatar
Total Posts:  415
Joined:  2007-11-18
 

Happy to help ^^. Btw, if you would like to implement this solution as a new report type thats also possible. Although it requires some work no doubt. But here are some files which might be helpful to look at / copy and modify.

app/code/core/Mage/Adminhtml/Block/Report/Product/Lowstock.php
app/code/core/Mage/Adminhtml/Block/Report/Product/Lowstock/Grid.php
app/code/core/Mage/Reports/Model/Product/Collection.php
app/code/core/Reports/etc/config.xml <- line 171-415 (menu / user permissions (acl tag))
app/design/adminhtml/default/default/template/report/grid.phtml
app/design/adminhtml/default/default/template/report/store/switcher.phtml

I hope this can helpful to you. I’ll keep this thread bookmarked and check in every once in a while (usually I go over my threads once a day), so if you run into some problem don’t be afraid to ask. Good luck! smile

 
Magento Community Magento Community
Magento Community
Magento Community
 
jemoon
Sr. Member
 
Avatar
Total Posts:  128
Joined:  2008-09-30
Gdansk, Poland
 

Hi,
IrishC You can also use a RSS for this purpose - just log in into your Backend navigate to Catalog -> Manage products and click RSS channel for Low Stock or something like this (I have non-english backend and I’m guessing).

 
Magento Community Magento Community
Magento Community
Magento Community
 
tunna03
Jr. Member
 
Total Posts:  8
Joined:  2009-06-21
 

I can’t seem to get any of these scripts to work… I just get a syntax error when I try to edit any php at all.  Am I just misunderstanding how this works?  What am I doing wrong?

 
Magento Community Magento Community
Magento Community
Magento Community
 
jazkat
Sr. Member
 
Total Posts:  154
Joined:  2009-07-09
 

Or you can go to “Cataloge/Manage Products” in backend and set filter that is located above listed products
Qty. (Quantity) from 0 to 0, or if you want from 0 to 1. etc… And then click “Search” button that is next to “Reset Filter” button.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Tingu
Jr. Member
 
Total Posts:  14
Joined:  2009-06-01
 

Hi! Bockbertil,

I’m Tingu very new to magento.
Thanks lot for your contributions to us.
I have a issue regarding reports, I’m sure you are the right person to ask.
Please help me. The issue is…

Login into magento admin > System > Configuration > To the left side, under Sales > Shipping Methods > UPS > Gave values like this Calculate Handling Fee = Fixed, Handling Applied = Per Order & Handling fee = 5;

I am getting “Shipping Amount” = “Handling fee” + “Amount given by the UPS”.
Its working fine, having no issue with it at all.

Now Reports > Sales > Shipping, I can see the report with fields “Period”, “Carrier/Method”, “Number of Orders” & “Total Shipping” ,
now also no issue.

But now I want to add a column to the existing columns with the name “Shipping Profit”, Under this for each row, value should be “Total Shipping” − “Amount given by the UPS”. So that I will get value to “Shipping Profit”. At last total row I should have sum of Shipping Profit.

And one more thing is if I change Calculate Handling Fee = Percent, Handling Applied = Per Package & Handling fee =10; Then also it should work.

Bockbertil, Please help me. I will be very thankful to you.
I’m doing a small project using Magento for my academics. If I do it well in time I will get a good grade. Please help me in this issue.

I haven’t created custom reports. Because that is not under the scope of my project.
Once I finish this project I will do that also.

With lots of hope. Please help me……….

Thanks lot for you time.

Truly,
Tingu.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Tingu
Jr. Member
 
Total Posts:  14
Joined:  2009-06-01
 

Hi! Bockbertil,
I have did little work regarding customize shipping report.
Then this I was unable to move forward.
What I did is………

partswebsite/app/code/core/Mage/Adminhtml/Block/Report/Sales/Shipping/Grid.php

In the above mentioned file > in “protected function _prepareColumns()” function > After
$this->addColumn(’total’, array(
‘header’ =>Mage::helper(’reports’)->__(’Total Shipping’),
‘type’ =>’currency’,
‘currency_code’ => $this->getCurrentCurrencyCode(),
‘index’ =>’total’,
‘total’ =>’sum’,
‘renderer’ =>’adminhtml/report_grid_column_renderer_currency’
));

This code
added below code

$this->addColumn(’Profit’, array(
‘header’ =>Mage::helper(’reports’)->__(’Shipping Profit’),
‘type’ =>’currency’,
‘currency_code’ => $this->getCurrentCurrencyCode(),
//’index’ => $this->calShippingProfit(’total’,’orders’ ),
‘index’ => ‘total’,
‘total’ =>’sum’,
‘renderer’ =>’adminhtml/report_grid_column_renderer_currency’
));

Getting additional column with name “Shipping Profit”. But The data in the rows under “Shipping Profit” is same as “Total Shipping”. To this extent I did. Please help me….

Please help me..

Truly,
Tingu.

 
Magento Community Magento Community
Magento Community
Magento Community
 
Tingu
Jr. Member
 
Total Posts:  14
Joined:  2009-06-01
 

Hi! IrishC,

Please help me regarding this issue.
Please Man...............................

You are very handsome in the photo.
And also tell me how can I put my photo.

Thanks.

Tyuly,
Tingu.

 
Magento Community Magento Community
Magento Community
Magento Community
 
allan2fluid
Jr. Member
 
Total Posts:  17
Joined:  2009-08-04
 

Good solution, but with alot of products it can become a very slow and clumsy way of getting productids.  You are far better off using the collection function getAllIds rather than looping through manually pulling into an array.  Alternative code below:

Instead of:

$stockCollection Mage::getModel('cataloginventory/stock_item')->getCollection()
        ->
addFieldToFilter('is_in_stock'0);
        
    
$productIds = array();

Use:

$productIds Mage::getModel('cataloginventory/stock_item')->getCollection()
        ->
addFieldToFilter('is_in_stock'0)->getAllIds();
MageParts - 08 January 2009 04:18 AM

Yo, got a solution for you ^_^. You can use this pretty much anywhere you like to, I placed it in a block (for testing purposes).

$stockCollection Mage::getModel('cataloginventory/stock_item')->getCollection()
        ->
addFieldToFilter('is_in_stock'0);
        
    
$productIds = array();
            
    foreach (
$stockCollection as $item{
        $productIds[] 
$item->getOrigData('product_id');
    
}
        
    $productCollection 
Mage::getModel('catalog/product')->getCollection()
        ->
addIdFilter($productIds)
        ->
addAttributeToSelect('name');
                    
    
$products = array();
            
    foreach (
$productCollection as $product{
        $products[] 
$product->getData('name');
    
}
        
    
// die(print_r($products));

The block I used for testing this can be found here: app/code/core/Mage/Catalog/Block/Product/List.php but as said, you should be able to place it pretty much anywhere in Magento. This procedure can probably be made in an easier way, but at least this works. Happy coding wink
 
Magento Community Magento Community
Magento Community
Magento Community
 
abooit
Jr. Member
 
Total Posts:  3
Joined:  2010-09-01
 

Can’t get it working :(

 
Magento Community Magento Community
Magento Community
Magento Community
 
VWE
Jr. Member
 
Total Posts:  5
Joined:  2009-06-25
 

For an SQL report of out of stock products with product name and SKU in PHPMyAdmin, try this:

SELECT catalog_product_entity_varchar.valuecatalog_product_entity.sku
FROM catalog_product_entity_varchar
INNER JOIN catalog_product_entity
ON catalog_product_entity_varchar
.entity_id  =  catalog_product_entity.entity_id 
WHERE catalog_product_entity_varchar
.attribute_id="56" AND catalog_product_entity_varchar.store_id="0"
AND catalog_product_entity_varchar.entity_id IN
(SELECT product_id  FROM cataloginventory_stock_status WHERE (stock_status ='0')) LIMIT 0100
 
Magento Community Magento Community
Magento Community
Magento Community
 
CristiM
Jr. Member
 
Total Posts:  1
Joined:  2010-09-30
 

There is a more simple sollution for this:

$collection Mage::getModel('catalog/product')->getCollection()
                      ->
addAttributeToSelect('*');  
$collection->getSelect()->joinLeft(
                  array(
'stock' => 'cataloginventory_stock_item'),
                  
"e.entity_id = stock.product_id",
                  array(
'stock.is_in_stock')
          )->
where('stock.is_in_stock = 1');
 
Magento Community Magento Community
Magento Community
Magento Community
 
sourav the inventer
Jr. Member
 
Avatar
Total Posts:  27
Joined:  2010-06-11
India, Westbengal, Kolkata
 

I have use this code and it’s working.

<?php 
     $cat_id 
Mage::getModel('catalog/layer')->getCurrentCategory()->getId(); 
     
$product_collection Mage::getModel('catalog/category')->load($cat_id);  
     
$all_product_collection $product_collection->getProductCollection()->getAllIds();
     
$stockCollection Mage::getModel('cataloginventory/stock_item')->getCollection()->addFieldToFilter('is_in_stock'0);        
     
$oos_productIds = array();            
        foreach (
$stockCollection as $item)
            
$oos_productIds[] $item->getOrigData('product_id');
     
$filtered_product = array();
     foreach(
$all_product_collection as $_pids){
         
foreach($oos_productIds as $oos_pro){
            
if($_pids==$oos_pro)
            
$filtered_product[] $oos_pro;
         
}
     }        
    $productCollection 
Mage::getModel('catalog/product')->getCollection()->addIdFilter($filtered_product);    
    
?>
    
<ul class="products-grid">           
<?php    foreach ($productCollection as $producth
                 $product 
Mage::getModel('catalog/product')->load($producth->getId());
?>
         
<li>
         <
a href="<?php echo $product->getProductUrl() ?>" class="product-image"><img src="<?php echo $this->helper('catalog/image')->init($product, 'small_image')->resize(135); ?>" width="135" height="135" alt="<?php echo $this->stripTags($this->getImageLabel($product, 'small_image'), null, true) ?>" /></a>
                <
h2 class="product-name"><a href="<?php echo $product->getProductUrl() ?>" title="<?php echo $this->stripTags($product->getName(), null, true) ?>"><?php echo $_helper->productAttribute($product$product->getName(), 'name'?></a></h2>
                
<?php if($product->getRatingSummary()): ?>
                <?php 
echo $this->getReviewsSummaryHtml($product'short'?>
                <?php 
endif; ?>
                <?php 
echo $this->getPriceHtml($producttrue?>
                
<div class="actions">
                    
<?php if($product->isSaleable()): ?>
                        
<button type="button" title="<?php echo $this->__('Add to Cart') ?>" class="button btn-cart" onclick="setLocation('<?php echo $this->getAddToCartUrl($product) ?>')"><span><span><?php echo $this->__('Add to Cart'?></span></span></button>
                    
<?php else: ?>
                        
<class="availability out-of-stock"><span><?php echo $this->__('Out of stock'?></span></p>
                    
<?php endif; ?>
                    
<ul class="add-to-links">
                        
<?php if ($this->helper('wishlist')->isAllow()) : ?>
                            
<li><a href="<?php echo $this->helper('wishlist')->getAddUrl($product) ?>" class="link-wishlist"><?php echo $this->__('Add to Wishlist'?></a></li>
                        
<?php endif; ?>
                        <?php 
if($_compareUrl=$this->getAddToCompareUrl($_product)): ?>
                            
<li><span class="separator">|</span> <a href="<?php echo $_compareUrl ?>" class="link-compare"><?php echo $this->__('Add to Compare'?></a></li>
                        
<?php endif; ?>
                    
</ul>
                </
div>         
         </
li>
   
<?php } ?>
 
Magento Community Magento Community
Magento Community
Magento Community
 
rleising
Jr. Member
 
Total Posts:  5
Joined:  2012-03-26
 

I know this is a couple months old, and not sure if this is what you’re looking for, but it works pretty well for me, and is simpler than other solutions.

$_oosCollection Mage::getResourceModel('catalog/product_collection')->addFieldToFilter('attribute_set_id'4)->addFieldToFilter('type_id''simple')->addFieldToFilter('status'1)->addAttributeToSelect('*');
foreach(
$_oosCollection as $oos){
    $isinstock 
$oos->getStockItem()->getData();
    if(
$isinstock['is_in_stock'== 0){
        
// action to take on out of stock products, i.e. echo name
    
}
}

That model will return everything, so I’m filtering out anything not a simple product, and any disabled products, as well as by attribute set.

The first line in the foreach will get an array that contains the is_in_stock property. Then you just check that and you have your out of stock products. 0 is out of stock, 1 is in stock.

 
Magento Community Magento Community
Magento Community
Magento Community
Magento Community
Magento Community
Back to top
Page 1 of 2