Magento allows you to update your product only if you are in "admin store".
So if you want to update magento product outside of admin or without creating admin session then you need to override Mage_Catalog_Model_Product class by changing setOrigData function.
from
public function setOrigData($key=null, $data=null) { if (Mage::app()->getStore()->isAdmin()) { return parent::setOrigData($key, $data); } return $this; }
to
public function setOrigData($key=null, $data=null) { return parent::setOrigData($key, $data); return $this; }
Here is how to update your product inventory using Magento core functions:
Create WebStockInput.csv a tab delimited file with the follwoing contents:
SKU PRICE QTY 2345 229.99 19
<?php ini_set("display_errors", 1); ini_set("memory_limit","1024M"); require_once 'app/Mage.php'; umask(0); $currentStore = Mage::app()->getStore()->getId(); Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); $path = getcwd().'/WebStockInput.csv'; $readfile = file ($path); for ($i=1; $i < count($readfile); $i++ ) { $fields = split("\t",$readfile[$i]); $product = Mage::getModel('catalog/product'); $product->load($product->getIdBySku($fields[0])); // get product's stock data such quantity, in_stock etc $stockData = $product->getStockData(); // update stock data using new data $stockData['qty'] = $fields[2]; $stockData['is_in_stock'] = 1; // then set product's stock data to update $product->setStockData($stockData); try { $product->save(); } catch (Exception $ex) { echo $ex->getMessage(); } } Mage::app()->setCurrentStore($currentStore); ?>
If you are attempting to update product inventory of large quantities of SKUs, be informed that Magento api / dataflow is not very good at handling a large quantities of data. The quickest solution, which imposed itself for massive imports is the direct import into database. This solution requires an extensive research and testing to ensure data integrity . A little trick to improve the import time: Disable indexes on tables used by your script, and then enable ... A simple example: Just before starting the import: ALTER TABLE table_name DISABLE KEYS Just after the import: ALTER TABLE table_name ENABLE KEYS This Basic Example uses direct SQL update:
< ?php ini_set("display_errors", 1); ini_set("memory_limit","1024M"); require_once 'app/Mage.php'; umask(0); $coreResource = Mage::getSingleton('core/resource') ; // fetch write database connection that is used in Mage_Core module $write = $coreResource->getConnection('core_write'); $path = getcwd().'/WebStockInput.csv'; $readfile = file ($path); for ($i=1; $i < count($readfile); $i++ ) { $fields = split("\t",$readfile[$i]); $entity_id = getEntityID_bySKU($write, $fields[0]) ; updateQTY ($write, $entity_id, $fields[2]); } function updateQTY ($db_magento, $entity_id, $qty) { $db_magento->query("UPDATE cataloginventory_stock_item s_i, cataloginventory_stock_status s_s SET s_i.qty = '$qty', s_i.is_in_stock = IF('$qty'>0, 1,0), s_s.qty = '$qty', s_s.stock_status = IF('$qty'>0, 1,0) WHERE s_i.product_id = '$entity_id' AND s_i.product_id = s_s.product_id "); } function getEntityID_bySKU($db_magento, $sku) { $entity_row = $db_magento->query("SELECT entity_id FROM catalog_product_entity p_e WHERE p_e.sku = '$sku'")->fetchObject(); $entity_id = $entity_row->entity_id; return $entity_id; } ?>
Useful links
- Direct SQL Update Extension.
- Direct import into database Script
- Magento et les imports/exports de données (in French)
Need help ? Hire Us.