6

I have to setup Product 'in stock' and update Quantity programmatically. Here is my code I am trying:

$product->setData('is_in_stock', 1);
$product->setData('qty', 5);
$product->save();

4 Answers 4

18

Can you please try below code? It will save inventory data.

$product->setStockData(['qty' => $stockData, 'is_in_stock' => $stockData]);
$product->setQuantityAndStockStatus(['qty' => $stockData, 'is_in_stock' => $stockData]);
$product->save();
4
  • Thanks SH Patel, I used last three line of above code and it works for me. Commented May 3, 2017 at 6:08
  • I used only first and third line but it not worked. the second line is also important to get it work. Commented Nov 7, 2017 at 14:45
  • I used second line and third line and it is working, so first line $product->setStockData(['qty' => $stockData, 'is_in_stock' => $stockData]) is unnecessary. @shashank
    – Key Shang
    Commented Jun 19, 2018 at 4:18
  • This code working for me bit only for enable product quantity update. Which products are disable, it's quantity not update. Can you help me for solve it.
    – Anil
    Commented Jun 27, 2019 at 11:02
2

In Magento 2.3 was deprecated change stock directly via setStockData(...) Instead of it, you MUST use this way for updating product stock

  1. Init product stock repository
    public function __construct(
        \Magento\InventoryApi\Api\StockRepositoryInterface $productStockRepository
    ) {
        $this->productStockRepository = $productStockRepository;
    }

  1. Use Repository for product stock update
    $productStock = $productStockRepository->get($entity->getId());
    $productStock->setStockData(['is_in_stock' => false]);
    $productStockRepository->save($productStock);

In case if you don't use Multi Source Inventory (Magento_InventoryApi extesions), you can update product stock this way:

  1. Init product stock registry
    public function __construct(
        \Magento\InventoryApi\Api\StockRepositoryInterface $productStockRepository
    ) {
        $this->productStockRepository = $productStockRepository;
    }
  1. Get product stock item and change product stock
    $productStock = $productStockRegistry->getStockItem($entity->getId());
    $productStock = $productStockRepository->get($entity->getId());
    $productStock->setIsInStock(true);
    $productStock->setQty(10);
    $productStock->save();
2
  • What is the entity here? Is it a product?
    – Avesh Naik
    Commented Jun 26 at 18:06
  • No, it's a product stock entity.
    – bakalov
    Commented Jun 28 at 23:59
0

Magento latest version uses the MSI inventory module for managing the inventory. Also providing provisions to create multiple inventory sources. By default, It will use the default source. If you want to set or update product stock then use the below code.

public function __construct(
    .....
    \Magento\InventoryCatalogApi\Model\SourceItemsProcessorInterface $sourceItemsProcessor
) {
    .....
    $this->sourceItemsProcessor = $sourceItemsProcessor;
}
public function execute()
{
    $data = [
        ['source_code'=>'default', 'status'=>1, 'quantity'=>50],
    ];
    
    $this->sourceItemsProcessor->execute(
        'your_sku',
        $data
    );
}
0

Other solutions are workable but they are deprecated.

So, You may follow the links below. I found these ones helpful.

https://magento.stackexchange.com/questions/325544/proper-way-to-update-stock-in-2-4
https://magento.stackexchange.com/questions/272296/how-to-set-qty-to-product-on-msi-magento-2-3

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.