Magento - foreach simple product show name, sku and stock quantity?

Hi, I am trying to accomplish the following. I would like a simple PHP script to show/export: foreach simple product name, sku, stock quantity, stock status

And pref also for configurable products (same but then summed)

I've come accross many good import scripts, but not a small sript to actually show the current stock status. The problem is that we have an employee who just doesnt understand data profiles etc: the problem arises btw when they have to open FTP to look up the file


Question: Any ideas how I can create a small script to show all current stock levels per product?

2 Answers 2


This should work to quickly grab everything you want. There may be a better way to do it.

// Load Magento
require_once '../app/Mage.php';

//get the collection filter the simple products & join the cataloginventory/stock_item table
$collection = Mage::getModel('catalog/product')->getCollection()->addAttributeToSelect('sku')->addAttributeToFilter('type_id', 'simple')->joinField(
) ;
foreach ($collection as $product) {
    echo "<tr><td>".$product->getSku()."</td><td> ".(int)$product->getQty()."</td></tr>";


We use this


// define('MAGENTO_ROOT', getcwd());
define('MAGENTO_ROOT', dirname(dirname(__FILE__)));

$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
// umask(0);

ini_set('display_errors', 1);

/* Store or website code */
$mageRunCode = isset($_SERVER['MAGE_RUN_CODE']) ? $_SERVER['MAGE_RUN_CODE'] : '';

/* Run store or run website */
$mageRunType = isset($_SERVER['MAGE_RUN_TYPE']) ? $_SERVER['MAGE_RUN_TYPE'] : 'store';



$data = Mage::getModel('catalog/product')->getCollection()
                    ->addFilter('type_id', 'simple')
                    ->addAttributeToSort('name', 'ASC');

//                    ->addFieldToFilter('store_id', Mage::app()->getStore()->getId())

header('Content-type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=stockstatus.xls");
// echo "val1\tval2\tval3";
// echo "<html><body>";

// echo "Name\tsku\tis_in_stock\tqty". "\r\n";
echo "sku\tis_in_stock\tqty\t\ttmp_mainprod\ttmp_size". "\r\n";
foreach($data as $product) {
    $stock = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty();
    $tmp_stock = $product->isAvailable() ? 1 : 0;
    echo $product->getSku() . "\t" .
    $tmp_stock . "\t" .
    $stock . "\t\t" .
    substr($product->getSku(), -2) . "\t" .
    substr($product->getSku(), 0, 6) . "\r\n";
//  $product->getStatus() . "\t" .
// $product->getName() . "\t" .


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.