Error: Cannot instantiate interface Magento\InventorySalesApi\Api\GetProductSalableQtyInterface
Found this error while removing deprecation of StockStateInterface for REST API, Does anyone has an idea why this error occurs? Thanks in advance
My code is
namespace Vendor\Module\Plugin;
use Magento\Catalog\Api\ProductRepositoryInterface;
use Magento\InventorySalesApi\Api\GetProductSalableQtyInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Api\Data\CartItemExtensionFactory;
use Psr\Log\LoggerInterface;
class CartItemPlugin
{
 /**
 * @var CartItemExtensionFactory
 */
 protected $cartItemExtension;
 /**
 * @var ProductRepositoryInterface
 */
 protected $productRepository;
 /**
 * @var GetProductSalableQtyInterface
 */
 protected $stockState;
 /**
 * @var LoggerInterface
 */
 protected $logger;
 /**
 * @param CartItemExtensionFactory $cartItemExtension
 * @param ProductRepositoryInterface $productRepository
 * @param LoggerInterface $logger
 * @param GetProductSalableQtyInterface $stockState
 */
 public function __construct(
 CartItemExtensionFactory $cartItemExtension,
 ProductRepositoryInterface $productRepository,
 LoggerInterface $logger,
 GetProductSalableQtyInterface $stockState
 ) {
 $this->cartItemExtension = $cartItemExtension;
 $this->productRepository = $productRepository;
 $this->logger = $logger;
 $this->stockState = $stockState;
 }
 
 public function afterSave(CartRepositoryInterface $subject,$quote
 ) {
 if ($quote !=null) {
 foreach ($quote->getItems() as $item) {
 $productData = $this->productRepository->get($item->getSku());
 $stock = $this->stockState->getStockQty($productData->getId());
 $stockArray = [
 'image' => $productData->getThumbnail(),
 'special_price' => $productData->getPrice(),
 'stock_qty' => $stock,
 'is_in_stock' => $productData->getStatus() ? "Yes" : "NO",
 ];
 $extensionAttributes = $item->getExtensionAttributes();
 $extensionAttributes->setStockItem($stockArray);
 $item->setExtensionAttributes($extensionAttributes);
 }
 }
 return $quote;
 }
}
 4 Answers 4
This might come alittle late but i just happened upon this error
With @piotr-rusin answer i was able to figure out that the simple solution in my case was that the Magento_InventorySales and underlying modules were disabled in app/etc/config.php
This might not be obvious but it's a good debugging step to ensure the modules in question are actually enabled ;)
The shop i was working on had disabled the MSI modules
- 
 totally true, i had the same issue and found that those modules was disabled in config.phptaoufiqaitali– taoufiqaitali2023年10月10日 15:36:41 +00:00Commented Oct 10, 2023 at 15:36
 
Can you add following code in you modules etc/di.xml file.
<preference for="Magento\InventorySalesApi\Api\GetProductSalableQtyInterface" type="Magento\InventorySales\Model\GetProductSalableQty"/>
 - 
 
"message": "The requested qty is not available"S.Shah– S.Shah2022年08月02日 07:05:00 +00:00Commented Aug 2, 2022 at 7:05 - 
 your main issue has been resolved for which you added question. your interface is now mapping properly with respective class. Now "The requested qty is not available" is your logical code error which is usually for products having out of stockHassan Ali Shahzad– Hassan Ali Shahzad2022年08月02日 09:11:55 +00:00Commented Aug 2, 2022 at 9:11
 - 
 but if I use StockStateInterface with the same logic it will return quantity of same product .....but not in GetProductSalableQtyInterfaceS.Shah– S.Shah2022年08月02日 09:59:05 +00:00Commented Aug 2, 2022 at 9:59
 - 
 Their are difference in having quantity shown in admin products grid and actually salable quantity. please study its documentations.Hassan Ali Shahzad– Hassan Ali Shahzad2022年08月02日 13:11:37 +00:00Commented Aug 2, 2022 at 13:11
 - 
 1its means : Magento\InventorySalesApi\Api\GetProductSalableQtyInterface look how it will be usable.Hassan Ali Shahzad– Hassan Ali Shahzad2022年08月02日 13:22:44 +00:00Commented Aug 2, 2022 at 13:22
 
Cannot instantiate interface... errors often mean that no di.xml preference was found for a given interface.
Does vendor/magento/module-inventory-sales/etc/di.xml exist in your project?
Preference for the interface that you mentioned is specified on line 36 in that file:
<preference for="Magento\InventorySalesApi\Api\GetProductSalableQtyInterface" type="Magento\InventorySales\Model\GetProductSalableQty"/>
 - 
 preference exist in vendor but still cant instantiate error occursS.Shah– S.Shah2022年08月01日 07:42:11 +00:00Commented Aug 1, 2022 at 7:42
 - 
 @S.Shah is
Magento_IntentorySalesApimodule enabled?Piotr Rusin– Piotr Rusin2022年08月02日 10:02:07 +00:00Commented Aug 2, 2022 at 10:02 - 
 
 
Run di:compile command
php bin/magento setup:di:compile
or you run this command
php bin/magento in:rein
will show the issue