1

enter image description here

How to get product salable qty by productId?

2 Answers 2

1

You have to get sku from product id then you can get salable qty by sku

<?php
namespace Mital\InventorySalesAdminUi\Model;

use Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku; 
use Magento\Catalog\Api\ProductRepositoryInterface;

class GetSalableQuantityDataBySku
{
    private $getSalableQuantityDataBySku;

    public function __construct(
        GetSalableQuantityDataBySku $getSalableQuantityDataBySku,
        ProductRepositoryInterface $productrepository
    ) 
    {
        $this->getSalableQuantityDataBySku = $getSalableQuantityDataBySku;
        $this->productrepository = $productrepository;
    }

    public function getProductSalableQty()
    {   
        $productid = 12;
        $sku = $this->getProductDataUsingId($productid);
        $salable = $this->getSalableQuantityDataBySku->execute($sku);
        echo json_encode($salable);
    }

    public function getProductDataUsingId($productid)
    {
       $product = $this->productrepository->getById($productid);
       return $product->getSku();
   }
}
1

You can do this :

<?php

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();
$StockState = $objectManager->get('\Magento\InventorySalesAdminUi\Model\GetSalableQuantityDataBySku');

$qty = $StockState->execute($product->getSku()); //Being product a product model loaded by your id
$quantity= $qty[0]['qty']; // This is the salable qty

3
  • This is useful.but objectmanager method is not good practice
    – Alshihab
    Sep 29, 2022 at 4:34
  • Hello @Alshihab why is it not a good practice? Thanks Sep 29, 2022 at 11:20
  • According to Magento's core group, you should not use Object Manager in modules because it makes the class lose dependency injection. magento.stackexchange.com/questions/117098/…
    – Alshihab
    Sep 29, 2022 at 11:45

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.