I've got this code below to retrieve and update 1 of the SKU of my Magento product.
It's updated and looks alright when I view on the code $product->getQty();
But when I look into Magento, the qty was not updated....
Could you please let me know where did I go wrong?
<?php
define('MAGENTO_ROOT', dirname(__FILE__));
$mageFilename = MAGENTO_ROOT . '/app/Mage.php';
require_once $mageFilename;
Varien_Profiler::enable();
ini_set('display_errors', 1);
umask(0);
Mage::app();
//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) {
if ($product->getSku() == "test123") {
try
{
$product->setData('qty', 99);
$product->save();
}
catch (Exception $e)
{
echo $e->getMessage();
}
}
echo "<tr><td>".$product->getSku()."</td><td> ".(int)$product->getQty()."</td></tr>";
};
?>