1

I have 10 products; 1, 2, 3, 4, 5, 6, 7, 8, 9, 10. I want to be able to bulk change 1, 3, 4, 5 and 9 to 0 quantity and out of stock and 2, 6, 7, 8 and 10 to 100 quantity and in stock. How can I do this programmatically? I have and am running magento 2.3.

  • 1
    @joeybab3 incorrect this is for M2 – Dominic Xigen Aug 14 at 22:44
  • This is not duplicate, because other question is not for magento 2 – Shoaib Munir Aug 15 at 3:24
0

use this script it will help you out.

First Initialize required instances
You can use object manager if you are using custom script (But it is not recommended)

protected $productRepository;
protected $stockRegistry;

public function __construct(
    ...
    \Magento\Catalog\Model\ProductRepository $productRepository,
    \Magento\CatalogInventory\Api\StockRegistryInterface $stockRegistry,
    ...
)
{
    $this->productRepository = $productRepository;
    $this->stockRegistry = $stockRegistry;
}

Now use above instances and do update stock like this:

    $outOfStockProducts = array(1, 3, 4, 5, 9);
    $inStockProducts = array(2, 6, 7, 8, 10);

    //For out of stock products
    foreach($outOfStockProducts as $k=>$id){
        $product = $this->productRepository->getById($id);
        $stockItem = $this->stockRegistry->getStockItemBySku($product->getSku());
        $stockItem->setQty(0);
        $this->stockRegistry->updateStockItemBySku($product->getSku(), $stockItem);
    }

    //For 100 stock products
    foreach($inStockProducts as $k=>$id){
        $product = $this->productRepository->getById($id);
        $stockItem = $this->stockRegistry->getStockItemBySku($product->getSku());
        $stockItem->setQty(100);
        $this->stockRegistry->updateStockItemBySku($product->getSku(), $stockItem);
    }

Edit:

If you want to do it with object manager (Which is not recommended) then use this:

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$productRepository = $objectManager->create('\Magento\Catalog\Model\ProductRepository');
$stockRegistry = $objectManager->create('\Magento\CatalogInventory\Api\StockRegistryInterface');

    $outOfStockProducts = array(1, 3, 4, 5, 9);
    $inStockProducts = array(2, 6, 7, 8, 10);

    //For out of stock products
    foreach($outOfStockProducts as $k=>$id){
        $product = $productRepository->getById($id);
        $stockItem = $stockRegistry->getStockItemBySku($product->getSku());
        $stockItem->setQty(0);
        $stockRegistry->updateStockItemBySku($product->getSku(), $stockItem);
    }

    //For 100 stock products
    foreach($inStockProducts as $k=>$id){
        $product = $productRepository->getById($id);
        $stockItem = $stockRegistry->getStockItemBySku($product->getSku());
        $stockItem->setQty(100);
        $stockRegistry->updateStockItemBySku($product->getSku(), $stockItem);
    }

Edit 2

You need these commands to update stock directly on databae

UPDATE cataloginventory_stock_item set qty=100 where product_id IN(2, 6, 7, 8, 10);
UPDATE cataloginventory_stock_status SET qty=100 AND stock_status=1 WHERE product_id IN(2, 6, 7, 8, 10);


UPDATE cataloginventory_stock_item set qty=0 where product_id IN(2, 6, 7, 8, 10);
UPDATE cataloginventory_stock_status SET qty=0 AND stock_status=0 WHERE product_id IN(2, 6, 7, 8, 10);

Hope this will resolve your issue.

  • Hi Shoaib, I need this in a custom script (object manager) – davidelias16 Aug 15 at 4:58
  • answer updated, please check – Shoaib Munir Aug 15 at 5:19
  • Hi Shoaib, that's perfect. Now all I need is to do them all in bulk, i.e. something like "UPDATE product_stock_table SET qty=0, in_stock=0 WHERE id IN (1, 3, 4, 5, 9)" – davidelias16 Aug 15 at 5:41
  • The statement above can do ~10,000 in under 0.025 seconds, but the PDO in your example takes over 2 minutes just to do ~500, that is what I am referring to. – davidelias16 Aug 15 at 5:51
  • It is not recommended to do direct update command. The solution that I have gave you is the best solution I can give. Now it's up to you how you can apply it – Shoaib Munir Aug 15 at 7:15

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