0

I am working on a Magento 2 site where we are using a Warehouse management system that manages stock. With it being an old system the only way to obtain latest stock is running a CSV from it at periods through the day. This export includes ALL stock, here lies the problem.

When using the Magento 2 import functionality and using add/update it is adding all the products on the original CSV into the database. Ideally all we want is an 'update' option, is this possible?

So ideally the csv could have a number of different SKU's that may or may not be on the site currently. If they are the Stock value gets updated, if not then it simply skips over it.

Any help would be greatly appreciated.

Thanks

  • This is not possible by default in M2 , our commercial extension (400$) offer that as one of the features - "Update only" behavior which can skip SKU's which is not on Magento 2 and only update existing. – FireBear Nov 22 '17 at 10:22
  • means you have to just update inventory right? – Moin Malek Nov 22 '17 at 13:39
  • @MoinMalek yes but how can this be done via CSV? – CreativeNerdSam Nov 22 '17 at 16:02
0

Late answer but facing the exact same problem, I use this script :

<?php
$file = "stock.txt";
$importArray = file($file);
use Magento\Framework\App\Bootstrap;
include('/YOURMAGENTOPATH/app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('adminhtml');
$product = $objectManager->get('Magento\Catalog\Model\Product');
$stockRegistry = $objectManager->create('Magento\CatalogInventory\Api\StockRegistryInterface');

foreach ($importArray as $line) {
  $line = trim($line);
  $line = str_replace('"','',$line);
  $line = explode(';',$line);
  $sku = $line[0];
  $stockValue = $line[1];
  if ($product->getIdBySku($sku)) {
    $stockItem = $stockRegistry->getStockItem($product->getId());
    $stockItem->setQty($stockValue);
    $stockItem->setIsInStock((bool)$stockValue);
    $stockRegistry->updateStockItemBySku($sku, $stockItem);
  }
};
unlink($file);
exit();

Qty are in a stock.txt upload from FTP with this format :

sku;qty
sku;qty
...;...

PS2 : Not "Magento2 friendly" to use the objectManager directly but works in my case...
Adding this script in the crontab and qty are updated every day.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.