I am trying to import products programmatically into a Magento 2.4.4 installation but I have problem setting the Salable Quantity. I cannot find a viable solution to this issue, please help if you can.

Here is the code I am using to set product Quantity (simplified):

use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\InventoryApi\Api\Data\SourceItemInterfaceFactory;
use Magento\InventoryApi\Api\SourceItemsSaveInterface;
public function __construct(
  ProductRepositoryInterface $productRepository,
  SourceItemInterfaceFactory $sourceItemFactory,
  SourceItemsSaveInterface   $sourceItemsSave
  $this->productRepository = $productRepository;
  $this->sourceItemFactory = $sourceItemFactory;
  $this->sourceItemsSave = $sourceItemsSave;
private function updateStock($product_id, $qty)
  $product = $this->productRepository->getById($product_id);

  $sourceItem = $this->sourceItemFactory->create();
  $sourceItem->setStatus($qty > 0);

After finishing import, I run manually reindex and cache flush:

bin/magento indexer:reindex & bin/magento cache:flush

When displaying the product grid, the product's Quantity is set correctly but the Salable Quantity is always zero (0). Only after I edit/save the product in admin panel manually, the Salable Quantity is set correctly and equal to Quantity. But this is not a viable solution when having to import thousands of products.

Could you provide an example on how to set the Salable Quantity programmatically?


2 Answers 2

  • To set the saleable quantity of a product , you need to look at this table = "cataloginventory_stock_item" , in this the field = "qty".

  • So in order to set the qty in this table , will automatically set the saleable qty.

  • Write a manual script placed in the magento root directory to set the qty of the product using the product id

  • You can use product repository to load the products and get the id's and store them in an array.

  • Using the product id's , set the qty field of the table i have mentioned above.

  • Also make sure that indexer is save on schedule , because things will get slow if it is in save on update.After the script execution you can run the indexer.

  • Or if you are having any ideas about using Model and Resource Model to set the qty , use these Classes -



  • Please see this sample code , if more info is needed do give a thumbs up!

  • Code


Required for custom code starts

use Magento\Framework\App\Bootstrap;

require __DIR__ . '/../app/bootstrap.php';

$bootstrap = Bootstrap::create(BP, $_SERVER);
$objectManager = $bootstrap->getObjectManager();
$state = $objectManager->get('Magento\Framework\App\State');
//Required for custom code ends


function setProductQty($objectManager)
    $productId = ["1", "2", "3", "4", "5"];
    foreach ($productId as $itemId) {
        try {
            $stockModel = $objectManager->get('Magento\CatalogInventory\Model\Stock\ItemFactory')->create();
            $stockResource = $objectManager->get('Magento\CatalogInventory\Model\ResourceModel\Stock\Item');
            $stockResource->load($stockModel, $itemId,"product_id");
        } catch (Exception $e) {
            echo $e->getMessage();

enter image description here

  • 2
    Yes, I tried your sample code with Model and ResourceModel and it works. Many thanks!
    – jfeid
    Jul 25, 2022 at 18:22
  • Happy to hear that, please do give a like, this motivates me to share more. Thanks and have a nice day. Jul 26, 2022 at 5:15

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],

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.