Skip to content

Instantly share code, notes, and snippets.

@seansan
Created December 9, 2014 08:18
Show Gist options
  • Save seansan/aab2707e596b7ce5748b to your computer and use it in GitHub Desktop.
Save seansan/aab2707e596b7ce5748b to your computer and use it in GitHub Desktop.
Magento stock report
<?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())
// ->addFieldToFilter('status',Mage_Catalog_Model_Product_Status::STATUS_ENABLED)
header('Content-type: application/vnd.ms-excel');
header("Content-Disposition: attachment; filename=stockstatus.xls");
$numsep = ","; $ndl='.';
echo "sku\tname\tis_in_stock\tqty\tmain sku\tmain sku2\tsize". "\n";
foreach($data as $product) {
$sku = $product->getSku();
$tmp_sku = preg_split('/[-_.]/',$sku);
$mainsku = (isset($tmp_sku[1]))? $tmp_sku[0] . "." . $tmp_sku[1] : $tmp_sku;
$mainsku2 = (isset($tmp_sku[2]))? $mainsku . "." . $tmp_sku[2] : $mainsku;
$size = end($tmp_sku);
$name = $product->getName();
$stock = (int)Mage::getModel('cataloginventory/stock_item')->loadByProduct($product)->getQty();
$tmp_stock = $product->isAvailable() ? 1 : 0;
echo $sku . "\t" .
$name . "\t" .
$tmp_stock . "\t" .
$stock . "\t" .
$mainsku . "\t" .
$mainsku2 . "\t" .
$size . "\t\n";
}
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment