<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mathanagopal Sankarasubramanian</title>
    <description>The latest articles on DEV Community by Mathanagopal Sankarasubramanian (@mathanagopal97).</description>
    <link>https://dev.to/mathanagopal97</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F320621%2F82f42997-540e-4288-be6a-2a6438ad4834.png</url>
      <title>DEV Community: Mathanagopal Sankarasubramanian</title>
      <link>https://dev.to/mathanagopal97</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mathanagopal97"/>
    <language>en</language>
    <item>
      <title>Magento 2 product saleable quantity</title>
      <dc:creator>Mathanagopal Sankarasubramanian</dc:creator>
      <pubDate>Sun, 13 Jun 2021 11:04:48 +0000</pubDate>
      <link>https://dev.to/mathanagopal97/magento-2-product-saleable-quantity-3m8k</link>
      <guid>https://dev.to/mathanagopal97/magento-2-product-saleable-quantity-3m8k</guid>
      <description>&lt;p&gt;Hiya fellow devs, this is my first post here. Please bear with me.&lt;/p&gt;

&lt;p&gt;Stock Management in Magento 2 is usually not that difficult before they deprecated the StockRegistryInterface.&lt;/p&gt;

&lt;p&gt;This post explains how to get the saleable quantity of a product in Magento 2.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;First inject the &lt;code&gt;Magento\InventorySalesApi\Api\StockResolverInterface&lt;/code&gt; and &lt;code&gt;Magento\InventorySalesApi\Api\GetProductSalableQtyInterface&lt;/code&gt; into the constructor of the class.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;
&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;__construct&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
&lt;span class="p"&gt;...&lt;/span&gt;
&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nc"&gt;Magento\InventorySalesApi\Api\StockResolverInterface&lt;/span&gt; &lt;span class="nv"&gt;$stockResolver&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
&lt;span class="err"&gt;\&lt;/span&gt;&lt;span class="nc"&gt;Magento\InventorySalesApi\Api\GetProductSalableQtyInterface&lt;/span&gt;
&lt;span class="nv"&gt;$getProductSaleableQty&lt;/span&gt;
&lt;span class="mf"&gt;...&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
&lt;span class="mf"&gt;...&lt;/span&gt;
   &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;stockResolver&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$stockResolver&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getProductSaleableQty&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$getProductSaleableQty&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You have guessed it correctly, we are going to use &lt;code&gt;GetProductSalableQtyInterface&lt;/code&gt; to get the saleable quantity of the product.&lt;/p&gt;

&lt;p&gt;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 the &lt;code&gt;StockResolverInterface&lt;/code&gt;. These terminologies come from MSI feature of Magento.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight php"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;function&lt;/span&gt; &lt;span class="n"&gt;getProductSaleableQty&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$productSku&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$websiteCode&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
   &lt;span class="nv"&gt;$stockId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;stockResolver&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;SalesChannelInterface&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;TYPE_WEBSITE&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$websiteCode&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;getStockId&lt;/span&gt;&lt;span class="p"&gt;();&lt;/span&gt;
   &lt;span class="k"&gt;try&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;
      &lt;span class="nv"&gt;$qty&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nv"&gt;$this&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="n"&gt;getProductSalableQty&lt;/span&gt;&lt;span class="o"&gt;-&amp;gt;&lt;/span&gt;&lt;span class="nf"&gt;execute&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nv"&gt;$productSku&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;$stockId&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="k"&gt;catch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;Exception&lt;/span&gt; &lt;span class="nv"&gt;$exception&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;
      &lt;span class="nv"&gt;$qty&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
   &lt;span class="p"&gt;}&lt;/span&gt;
   &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nv"&gt;$qty&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here &lt;code&gt;SalesChannelInterface&lt;/code&gt; is from &lt;code&gt;Magento\InventorySalesApi\Api\Data\SalesChannelInterface&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Just by this stock ID you can get the following details related to stock in Magento 2:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Retrieve sources related to current stock ordered by priority. - &lt;code&gt;GetSourcesAssignedToStockOrderedByPriorityInterface&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Use it in &lt;code&gt;StockRepositoryInterface&lt;/code&gt; to get the details of the stock.&lt;/li&gt;
&lt;li&gt;Check if product is assigned to stock - &lt;code&gt;IsProductAssignedToStockInterface&lt;/code&gt;
&lt;/li&gt;
&lt;li&gt;Check whether products are saleable for given stock - &lt;code&gt;AreProductsSalableInterface&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;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 - &lt;code&gt;AreProductsSalableForRequestedQtyInterface&lt;/code&gt;
&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Hope this helps for someone.&lt;/p&gt;

</description>
      <category>magento2</category>
      <category>stock</category>
      <category>product</category>
      <category>php</category>
    </item>
  </channel>
</rss>
