4

I'm finding inconsistent documentation on how to update product stock for M2 2.4.1.

Magento\Catalog\Model\Product::setStockData is documented in code to be deprecated and points to Magento\CatalogInventory\Api\Data\StockItemInterface.

StockItemInterface in turn is documented in code to be deprecated and links to pages https://devdocs.magento.com/guides/v2.3/inventory/index.html and https://devdocs.magento.com/guides/v2.3/inventory/catalog-inventory-replacements.html, which are obviously out of date.

Going to the latest page, https://devdocs.magento.com/guides/v2.4/inventory/inventory-api-reference.html, points to Magento\CatalogInventory\Api\StockRepositoryInterface, along with a bunch of other APIs, which is also documented to be deprecated and says "Replaced with Multi Source Inventory".

What is the proper way to programmatically update stock on 2.4?

2 Answers 2

5

In order to use the new API to update stock, add this to the constructor:

public function __construct(
    \Magento\InventoryApi\Api\SourceItemsSaveInterface $sourceItemsSaveInterface,
    \Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory $sourceItemFactory,
    \Magento\InventoryConfigurationApi\Api\GetStockItemConfigurationInterface $getStockItemConfiguration,
    \Magento\InventoryConfigurationApi\Api\SaveStockItemConfigurationInterface $saveStockItemConfiguration
) {
    $this->sourceItemsSaveInterface = $sourceItemsSaveInterface;
    $this->sourceItemFactory = $sourceItemFactory;
    $this->getStockItemConfiguration = $getStockItemConfiguration;
    $this->saveStockItemConfiguration = $saveStockItemConfiguration;
}

Updating the quantity looks like this:

$sourceItem = $this->sourceItemFactory->create();
$sourceItem->setSourceCode('default');
$sourceItem->setSku($sku);
$sourceItem->setQuantity($qty);
$sourceItem->setStatus(1);
$this->sourceItemsSaveInterface->execute([$sourceItem]);

// Update stock item (Advanced inventory insettings)
$stockItemConfiguration = $this->getStockItemConfiguration->execute(
    $product->getSku(), $this->defaultStockId
);
$stockItemConfigurationExtension = $stockItemConfiguration->getExtensionAttributes();
$stockItemConfigurationExtension->setIsInStock($status);
// set other stock item properties here ('cataloginventory_stock_item' table in DB)
$stockItemConfiguration->setExtensionAttributes($stockItemConfigurationExtension);
$this->saveStockItemConfiguration->execute(
    $product->getSku(),
    $this->defaultStockId,
    $stockItemConfiguration
);

Source link: https://www.czettner.com/2020/08/24/magento-2-3-update-stock-multi-source.html

2
  • This leaves some questions - how to set if stock is managed and so on? The source Item does not provide all functions the Advanced Inventory offers.. can you elaborate how to set them?
    – Max
    Sep 26, 2021 at 11:23
  • 1
    @Max, please, see updated example Feb 28, 2023 at 10:19
0

I had implemented same codes but for some reason sometime only price get updated and qty set always 0. Sometime vise versa. When investigated further found out its a kind of a bug in Magento 2. Please refer https://github.com/magento/inventory/issues/2396

What actually worked for me was the below codes that I got it from the above link.

..
..
\Magento\Catalog\Model\ProductRepository $productRepository,
\Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory $sourceItemFactory,
\Magento\InventoryApi\Api\SourceItemsSaveInterface $sourceItemsSaveInterface,
...   
...
$this->productRepository = $productRepository;
$this->sourceItemFactory = $sourceItemFactory;  
$this->sourceItemsSaveInterface = $sourceItemsSaveInterface;
...
...
//
// Ref: https://github.com/magento/inventory/issues/2396
// Set product price
//
               
$model = $this->productRepository->get($product_sku, true, null, true);
$model->setPrice($new_price);                
$model->save();// This call needs to be before the stock level update, due to the magento bug

// Update the stock level                
$sourceItem = $this->sourceItemFactory->create();
$sourceItem->setSourceCode('default');
$sourceItem->setSku($product_sku);
$sourceItem->setQuantity($new_qty);
$sourceItem->setStatus($is_in_stock);
$this->sourceItemsSaveInterface->execute([$sourceItem]);

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.