1

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

https://stackoverflow.com/questions/8533426/daily-inventory-update-through-remote-ftp-file

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

2 Answers 2

2

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

<?php 
// Load Magento
require_once '../app/Mage.php';
umask(0);
Mage::app('default');


//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(
    'qty',
    'cataloginventory/stock_item',
    'qty',
    'product_id=entity_id',
    '{{table}}.stock_id=1',
    'left'
) ;
?>
<html>
<head></head>
<body>
<table>
<?php 
foreach ($collection as $product) {
    echo "<tr><td>".$product->getSku()."</td><td> ".(int)$product->getQty()."</td></tr>";
};




?>
</table>
</body></html>
0

We use this

<?php

// 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';

Mage::app($mageRunCode,$mageRunType);

Mage::setIsDeveloperMode(true);

$data = Mage::getModel('catalog/product')->getCollection()
                    ->addAttributeToSelect('name')
                    ->addAttributeToSelect('type_id')
                    ->addAttributeToSelect('sku')
                    ->addAttributeToSelect('price')
                    ->addAttributeToSelect('stock_status')
                    ->addFilter('type_id', 'simple')
                    ->addStoreFilter(Mage::app()->getStore()->getId())
                    ->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" .
}

exit;

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.