I am using the following script to update my inventory. After the update, I want to clear the cache and reindex the data, because the updated value on qty not set on the product page. How can i do this?
$mageFilename = '../app/Mage.php'; require_once $mageFilename; Mage::setIsDeveloperMode(true); ini_set('display_errors', 1); umask(0); Mage::app('admin'); Mage::register('isSecureArea', 1); Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID); set_time_limit(0); ini_set('memory_limit','1024M'); function _getConnection($type = 'core_read'){ return Mage::getSingleton('core/resource')->getConnection($type); } function _getTableName($tableName){ return Mage::getSingleton('core/resource')->getTableName($tableName); } function _getAttributeId($attribute_code = 'price'){ $connection = _getConnection('core_read'); $sql = "SELECT attribute_id FROM " . _getTableName('eav_attribute') . " WHERE entity_type_id = ? AND attribute_code = ?"; $entity_type_id = _getEntityTypeId(); return $connection->fetchOne($sql, array($entity_type_id, $attribute_code)); } function _getEntityTypeId($entity_type_code = 'catalog_product'){ $connection = _getConnection('core_read'); $sql = "SELECT entity_type_id FROM " . _getTableName('eav_entity_type') . " WHERE entity_type_code = ?"; return $connection->fetchOne($sql, array($entity_type_code)); } function _checkIfSkuExists($sku){ $connection = _getConnection('core_read'); $sql = "SELECT COUNT(*) AS count_no FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?"; $count = $connection->fetchOne($sql, array($sku)); if($count > 0){ return true; }else{ return false; } } function _getIdFromSku($sku){ $connection = _getConnection('core_read'); $sql = "SELECT entity_id FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?"; return $connection->fetchOne($sql, array($sku)); } function _updateStocks($data){ $connection = _getConnection('core_write'); $sku = $data[0]; $newQty = $data[1]; $productId = _getIdFromSku($sku); $attributeId = _getAttributeId(); $sql = "UPDATE " . _getTableName('cataloginventory_stock_item') . " csi, " . _getTableName('cataloginventory_stock_status') . " css SET csi.qty = ?, csi.is_in_stock = ?, css.qty = ?, css.stock_status = ? WHERE csi.product_id = ? AND csi.product_id = css.product_id"; $isInStock = $newQty > 0 ? 1 : 0; $stockStatus = $newQty > 0 ? 1 : 0; $connection->query($sql, array($newQty, $isInStock, $newQty, $stockStatus, $productId)); } $csv = new Varien_File_Csv(); $data = $csv->getData('stocks.csv');
I am trying to do this, but the update was very slow. I ran this script before echo $message.
$processes = Mage::getSingleton('index/indexer')->getProcessesCollection(); $processes->walk('reindexAll'); $processes->walk('setMode', array(Mage_Index_Model_Process::MODE_REAL_TIME)); $processes->walk('save');
user1716516
source share