0

I am using the following script

http://www.sonassi.com/knowledge-base/magento-kb/mass-update-stock-levels-in-magento-fast/

It works beautifully with the test CSV file.

My POS creates a CSV file but it puts a different heading so the script does not work. I want to automate the process. Is there any way to change the names of headers automatically?

The script requires the headers to be

“sku”,”qty”

my CSV is

“ITEM”,”STOCK”

Is there any way for these two different names to be linked within the script so that my script sees ITEM as sku and STOCK as qty?

user1477622
  • 132
  • 7
user1481153
  • 169
  • 2
  • 5
  • 13

3 Answers3

0

You should create a php script with an input of the yourfilename.csv, which is the unformatted file.

 $file = file_get_contents('yourfilename.csv');
 $file = str_replace('ITEM', 'sku', $file);
 $file = str_replace('STOCK', 'qty', $file);
 file_put_contents('yourfilename.csv', $file);

The below links are for your reference.

find and replace values in a flat-file using PHP http://forums.phpfreaks.com/index.php?topic=327900.0

Hope it helps.

Cheers

Community
  • 1
  • 1
Swapna
  • 401
  • 2
  • 6
0

PHP isn't usually the best way to go for file manipulation granting the fact you have SSH access.

You could also run the following commands (if you have perl installed, which is default in most setups...):

perl -pi -e 's/ITEM/sku/g' /path/to/your/csvfile.csv
perl -pi -e 's/STOCK/qty/g' /path/to/your/csvfile.csv
Kenny
  • 5,350
  • 7
  • 29
  • 43
0

If you want qty update using raw sql way then you can create a function like below:

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

And call the above function by passing csv row data as arguments. This is just a hint.

In order to get full working code with details you can refer to the following blog article:
Updating product qty in Magento in an easier & faster way

Hope this helps!

MagePsycho
  • 1,944
  • 2
  • 29
  • 60