I am writing a module that loops over a result-set and creates products. Creating products works fine, but some how I am not able to save stock qty.

This is my code: http://pastebin.com/7Drn8Jht

Here I assign my stockItem to the product object

$productExtension = $this->productExtensionFactory->create();

$product->setStockData(['qty' => 100, 'is_in_stock' => 1]);
$product->setQuantityAndStockStatus(['qty' => 100, 'is_in_stock' => 1]);

But for some reason this is not working. If I look at my catalog the stock qty is for each product empty. What am I doing wrong?


Two problems here:

  • first you're not saving the stock item so your values will never show up
  • second you're not linking your stock item to your product.

What I suggest is, once you've save your product, load the corresponding stock item, update it and save it, for example:

$productId = $product->getId();
$stockItem = $this->_objectManager->get('Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory')->create();
$this->_stockItemResource->loadByProductId($stockItem, $productId, $this->_storeManager->getWebsite()->getId());
  • 2
    A friendly reminder to not suggest instantiating objects using the Object Manager. Or at least suggest it using Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory::class. These snippets will be here for a long time, and they'll have an IE6-effect in which you'll need to tell everyone to NOT do it that way, but injecting the dependency instead. I know you wrote it like that so that others see what actual object you're using, but I'm sure we can find other ways to say that. mwop.net/blog/2016-04-26-on-locators.html
    – nevvermind
    May 9 '16 at 13:39

Just to simplify this answer further without the need for 'Magento\CatalogInventory\Model\ResourceModel\Stock\Item' and provide a full example with dependency injection, which is the "Magento way" of doing things:


namespace Yournamespace\YourModule\Path

use \Magento\CatalogInventory\Api\StockRegistryInterface;

class Yourclass{

   protected $stockRegistry;

   public function __construct(StockRegistryInterface $stockRegistry){

      $this->stockRegistry = $stockRegistry;


   public function yourCustomStockFunction($productId){

     $stockItem = $this->stockRegistry->getStockItem($productId);
     return $stockItem->save();



You can also load the stock registry by Sku if you wish:

  • I don't see a save function within the stockregistryinterface Oct 20 '17 at 4:56
  • Beats my why this works but its an elegant solution. Thanks! Oct 20 '17 at 5:04

Thank you, I solved it like this

$product = $this->productRepository->save($product);

$productId = $product->getId();
$stockItem = $this->stockFactory->create();
$stockItemResource = $this->objectManager->get('Magento\CatalogInventory\Model\ResourceModel\Stock\Item');
$stockItemResource->loadByProductId($stockItem, $productId,$this->storeManager->getWebsite()->getId());

This is short and simple method

$stockiteminterface = $objectManager->get('Magento\CatalogInventory\Api\Data\StockItemInterfaceFactory')->create();
  • Object manager is not recommended. Dec 13 '18 at 16:15

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.