Magento – script php to update stock

magento-2.1magento2PHPstock

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.

Best Answer

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