UPDATE: I figured out that the issue is that while this script updates the item in the cataloginventory_stock_item
table, it fails to update it in the cataloginventory_stock_status
table. Added an answer for how to fix this.
The following script is being used to update the inventory levels of items in my store:
<?
define('MAGENTO', realpath(dirname(__FILE__)));
require_once MAGENTO . '/app/Mage.php';
umask(0);
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$count = 0;
$file = fopen(MAGENTO . '/var/import/updateStockLevels.csv', 'r');
while (($line = fgetcsv($file)) !== FALSE) {
if ($count == 0) {
foreach ($line as $key=>$value) {
$cols[$value] = $key;
}
}
$count++;
if ($count == 1) continue;
#Convert the lines to cols
if ($count > 0) {
foreach($cols as $col=>$value) {
unset(${$col});
${$col} = $line[$value];
}
}
// Check if SKU exists
$product = Mage::getModel('catalog/product')->loadByAttribute('sku',$sku);
if ( $product ) {
$productId = $product->getId();
$stockItem = Mage::getModel('cataloginventory/stock_item')->loadByProduct($productId);
$stockItemId = $stockItem->getId();
$stock = array();
if (!$stockItemId) {
$stockItem->setData('product_id', $product->getId());
$stockItem->setData('stock_id', 1);
} else {
$stock = $stockItem->getData();
}
foreach($cols as $col=>$value) {
$stock[$col] = $line[$value];
}
foreach($stock as $field => $value) {
$stockItem->setData($field, $value?$value:0);
}
$stockItem->save();
unset($stockItem);
unset($product);
}
echo "<br />Stock updated $sku";
}
fclose($file);
?>
This script works great and all qty levels are updated accurately. The issue is with the in stock check.
If I go to an item page on the frontend that has qty zero after running the script, it shows it as In Stock
. However, when I go to check that item in the backend, the Inventory->Stock Availability
field correctly shows as Out of Stock
, and the database itself has the correct value.
If I go to the product page in the admin, and click to save the product (since it already shows it correctly being out of stock in the availability field), its fixes the problem. The frontend will now show it properly as out of stock.
Since qty
set to 0
and is_in_stock
set to false (0)
is not enough to mark it properly as Out of Stock
on the frontend, I assume that there must be some field that I am not properly updating in my script, that is only updated properly when I save the item from the admin backend. However I don't know what field(s) I could be missing.