cancel
Showing results for 
Search instead for 
Did you mean: 

Import/Update product quantity by CSV

Import/Update product quantity by CSV

Create a csv file with two column is "SKU" and "QTY" and save on root directory on magento.

And create order.php file on magento root directory and run with after domain name.

Here is order.php file...

 

 

<?php
$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');
/*Connection*/

function _getConnection($type = 'core_read')
{
return Mage::getSingleton('core/resource')->getConnection($type);
}

/*Tables*/

function _getTableName($tableName)
{
return Mage::getSingleton('core/resource')->getTableName($tableName);
}

/*Attributes*/

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));
}

/*entity Id*/

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));
}

/*Check Sku */

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;
}
}

/*Get entity_id from SKU*/

function _getIdFromSku($sku)
{
$connection = _getConnection('core_read');
$sql = "SELECT entity_id FROM " . _getTableName('catalog_product_entity') . " WHERE sku = ?";
return $connection->fetchOne($sql, array($sku));
}

/*Update Stock*/
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));
}

/*Insert here your csv name*/

$csv = new Varien_File_Csv();
$data = $csv->getData('new_qty.csv'); //path to csv
array_shift($data);

$message_display = '';
$count = 1;
foreach ($data as $_data) {
if (_checkIfSkuExists($_data[0])) {
try {
_updateStocks($_data);
$message_display .= $count . '> Success:: Qty (' . $_data[1] . ') of Sku (' . $_data[0] . ') has been updated. <br />';

} catch (Exception $e) {
$message_display .= $count . '> Not Found:: while Upating Qty (' . $_data[1] . ') of Sku (' . $_data[0] . ') => ' . $e->getMessage() . '<br />';
}
} else {
$message_display .= $count . '> SKU Not Found:: Product with Sku (' . $_data[0] . ') does\'t exist.<br />';
}
$count++;
}
echo $message_display;
7 REPLIES 7

Re: Import/Update product quantity by CSV

Hi, did you have a question or were you just sharing your code? I've reformatted it for you. 

 

Why did you avoid using the built in import for this, for speed?

----
If you've found one of my answers useful, please give "Kudos" or "Accept as Solution" as appropriate. Thanks!

Re: Import/Update product quantity by CSV

Could do this by going to System > Import /Export > Data Profiles

 

Create an import feed

Set it to be 2 columns, 1 SKU, 1 Qty

 

Upload CSV, Run Import

 

(Just incase someone else comes across this thread and thinks Magento is overtly complicated)

Re: Import/Update product quantity by CSV

i want to update all data for the product using csv so please help me  how to do this 

 

Re: Import/Update product quantity by CSV

If you are updating quantity from 1000 to 900 but 10 customer orders come in before you have completed the update, won't your quantity now be wrong: e.g. 900 instead of 890?

Is there a way to avoid this problem?

Re: Import/Update product quantity by CSV

@elfling  Someone post in StackOverflow,  any help regard same issue, https://magento.stackexchange.com/q/306701/57334 thank you

Re: Import/Update product quantity by CSV

@Aveeva Looks like they got an answer Robot Happy

Re: Import/Update product quantity by CSV

@elfling  Yes, thanks for your notification.