I'm using Magento 2.1.2 Since I'm not so good with php language, I would like to know if exists any way to update quantities in my catalog reading a file with 2 columns
 SKU | Qty
 889588... 3
and do a query like this
UPDATE table_stock SET qty = x WHERE sku = y
If the qty is 0 the product has to be out of stock.
My Idea is to create a php file and update it on the root of magento installation on the server, and run it calling it's path.
- 
 you can export csv from backend.Rakesh Jesadiya– Rakesh Jesadiya2016年11月14日 08:45:58 +00:00Commented Nov 14, 2016 at 8:45
- 
 @Rakesh I would like to import quantitiesMartina– Martina2016年11月14日 08:57:43 +00:00Commented Nov 14, 2016 at 8:57
- 
 yes you can import csv using backend, you dont need to do update script.Rakesh Jesadiya– Rakesh Jesadiya2016年11月14日 09:02:12 +00:00Commented Nov 14, 2016 at 9:02
- 
 please let me know if you have any issueRakesh Jesadiya– Rakesh Jesadiya2016年11月14日 09:28:47 +00:00Commented Nov 14, 2016 at 9:28
- 
 @Rakesh yes, this is a good idea but it seems to be not too fast...I have to do this every evening and maybe more times a day... I'm searching for something faster if it's possible...Martina– Martina2016年11月14日 10:01:25 +00:00Commented Nov 14, 2016 at 10:01
2 Answers 2
I've written the following script to achieve what you asked for. I suggest placing this outside your root Magento directory inside a scripts folder so this is not accessible by browser.
Create file in suggested directory: /var/www/html/magento/scripts/update-low-qty.php. Add the following:
<?php
use Magento\Framework\App\Bootstrap;
include('../htdocs/app/bootstrap.php');
$bootstrap = Bootstrap::create(BP, $_SERVER);
$_objectManager = $bootstrap->getObjectManager();
$state = $_objectManager->get('Magento\Framework\App\State');
$state->setAreaCode('adminhtml');
//list of products to check
//sku => update quantity
$products = [
 'test-product-1' => 3,
 'test-product-2' => 6,
 'test-product-3' => 30
];
echo "Starting...\n";
$_zeroQtyProducts = $_objectManager->create('Magento\Catalog\Model\ResourceModel\Product\Collection')->addFieldToFilter('sku', array_keys($products));
$_stockState = $_objectManager->get('\Magento\CatalogInventory\Api\StockStateInterface');
$_stockRegistry = $_objectManager->get('\Magento\CatalogInventory\Api\StockRegistryInterface');
//if any products found
if($_zeroQtyProducts) {
 echo sprintf("Found %s product(s) with a qty of 0\n", count($_zeroQtyProducts));
 foreach ($_zeroQtyProducts as $_product) {
 $_stock = $_stockState->getStockQty($_product->getId(), $_product->getStore()->getWebsiteId());
 $_sku = $_product->getSku();
 echo sprintf("## Processing %s ##\n", $_sku);
 //do a double check quantity is 0 and product has been set to update
 if ((int)$_stock == 0 && isset($products[$_sku])) {
 $_stockItem = $_stockRegistry->getStockItem($_product->getId());
 $_stockItem->setData('is_in_stock',1); //set updated data as your requirement
 $_stockItem->setData('qty', $products[$_sku]); //set updated quantity
 $_stockItem->save(); //save stock of item
 $_product->save();
 echo sprintf("Product had 0 qty..updated to: %s\n", $products[$_sku]);
 }
 echo sprintf("## Finished processing %s ##\n", $_sku);
 }
} else {
 echo sprintf("0 Products found with provided SKU's.\n");
}
exit("Finished.");
Then run this script by command line by running php update-low-qty.php.
To configure which product sku's get updated and what they get updated to, add to the products array which is in the format of product_sku => quantity_to.
Expected output: enter image description here
- 
 Hi Ben, thanks for your answer, in the next days I will try this solutions and I let you know. In case I will set you as answer.Martina– Martina2016年11月17日 19:29:55 +00:00Commented Nov 17, 2016 at 19:29
- 
 Was this answer useful @MartinaF ?Ben Unsworth– Ben Unsworth2016年12月13日 16:23:26 +00:00Commented Dec 13, 2016 at 16:23
- 
 it's workig fine But how can i update both price and qty @ same time ?Sarfaraj Sipai– Sarfaraj Sipai2018年06月06日 08:00:59 +00:00Commented Jun 6, 2018 at 8:00
- First export your product from backend : System / Export / Productsas CSV.
- Update the qty column of the CSV file with Excel or similar application.
- Then import the CSV from backend : System / Import / ProductswithAdd / Updateas the import behavior
- 
 yes, this is a good idea but it seems to be not too fast...I have to do this every evening and maybe more times a day... I'm searching for something faster if it's possible...Martina– Martina2016年11月14日 10:01:30 +00:00Commented Nov 14, 2016 at 10:01
- 
 You can create a script to generate your CSV file with the correct qty. You do not need to fill all colums. Then you can manually import them. If you have Magento 2 EE, you can schedule the product import.Franck Garnier– Franck Garnier2016年11月14日 10:03:42 +00:00Commented Nov 14, 2016 at 10:03
- 
 You can check with the community extensions. Such as xtento.com/magento-extensions/… . I never used it.Franck Garnier– Franck Garnier2016年11月14日 10:05:25 +00:00Commented Nov 14, 2016 at 10:05