1

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 Nov 14 '16 at 8:45
  • @Rakesh I would like to import quantities – Martina Nov 14 '16 at 8:57
  • yes you can import csv using backend, you dont need to do update script. – Rakesh Jesadiya Nov 14 '16 at 9:02
  • please let me know if you have any issue – Rakesh Jesadiya Nov 14 '16 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 Nov 14 '16 at 10:01
3

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 Nov 17 '16 at 19:29
  • Was this answer useful @MartinaF ? – Ben Unsworth Dec 13 '16 at 16:23
  • it's workig fine But how can i update both price and qty @ same time ? – Sarfaraj Sipai Jun 6 '18 at 8:00
0
  1. First export your product from backend : System / Export / Products as CSV.
  2. Update the qty column of the CSV file with Excel or similar application.
  3. Then import the CSV from backend : System / Import / Products with Add / Update as 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 Nov 14 '16 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 Nov 14 '16 at 10:03
  • You can check with the community extensions. Such as xtento.com/magento-extensions/… . I never used it. – Franck Garnier Nov 14 '16 at 10:05

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.