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.


  • 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
    Commented Nov 22, 2017 at 10:22
  • means you have to just update inventory right?
    – Moin Malek
    Commented Nov 22, 2017 at 13:39
  • @MoinMalek yes but how can this be done via CSV? Commented Nov 22, 2017 at 16:02

1 Answer 1


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

$file = "stock.txt";
$importArray = file($file);
use Magento\Framework\App\Bootstrap;
$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
$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());
    $stockRegistry->updateStockItemBySku($sku, $stockItem);

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


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.

  • nice, this script works for me Commented Feb 10, 2021 at 22:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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