Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Mathanagopal Sankarasubramanian
Mathanagopal Sankarasubramanian

Posted on

     

Magento 2 product saleable quantity

Hiya fellow devs, this is my first post here. Please bear with me.

Stock Management in Magento 2 is usually not that difficult before they deprecated the StockRegistryInterface.

This post explains how to get the saleable quantity of a product in Magento 2.

To get this you need product SKU (Stock Keeping unit) and website code of the website you need the saleable quantity for. So now lets get down to code.

First inject theMagento\InventorySalesApi\Api\StockResolverInterface andMagento\InventorySalesApi\Api\GetProductSalableQtyInterface into the constructor of the class.

publicfunction__construct(...\Magento\InventorySalesApi\Api\StockResolverInterface$stockResolver,\Magento\InventorySalesApi\Api\GetProductSalableQtyInterface$getProductSaleableQty...){...$this->stockResolver=$stockResolver;$this->getProductSaleableQty=$getProductSaleableQty;}
Enter fullscreen modeExit fullscreen mode

You have guessed it correctly, we are going to useGetProductSalableQtyInterface to get the saleable quantity of the product.

Then all that is left is to write the logic. You have to retrieve the stock ID of the stock that is appointed to the website. Then you have to pass the SKU and stock ID to get the saleable quantity. Here stock ID is important and to get that we need to use theStockResolverInterface. These terminologies come from MSI feature of Magento.

publicfunctiongetProductSaleableQty($productSku,$websiteCode){$stockId=$this->stockResolver->execute(SalesChannelInterface::TYPE_WEBSITE,$websiteCode)->getStockId();try{$qty=$this->getProductSalableQty->execute($productSku,$stockId);}catch(Exception$exception){$qty=0;}return$qty;}
Enter fullscreen modeExit fullscreen mode

HereSalesChannelInterface is fromMagento\InventorySalesApi\Api\Data\SalesChannelInterface.

Just by this stock ID you can get the following details related to stock in Magento 2:

  1. Retrieve sources related to current stock ordered by priority. -GetSourcesAssignedToStockOrderedByPriorityInterface
  2. Use it inStockRepositoryInterface to get the details of the stock.
  3. Check if product is assigned to stock -IsProductAssignedToStockInterface
  4. Check whether products are saleable for given stock -AreProductsSalableInterface.
  5. Check whether a given products quantities are saleable for a given stock. Meaning, whether you can sell specific quantity of a product in your website -AreProductsSalableForRequestedQtyInterface

Hope this helps for someone.

Top comments(0)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Full stack java enthusiast with knowledge in jsp, php, spring and hibernate
  • Location
    Chennai
  • Work
    Full Stack Developer
  • Joined

Trending onDEV CommunityHot

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp