1
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;
 }
}
asked Jun 29, 2022 at 8:49

4 Answers 4

2

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

answered Nov 25, 2022 at 9:48
1
  • totally true, i had the same issue and found that those modules was disabled in config.php Commented Oct 10, 2023 at 15:36
1
+50

Can you add following code in you modules etc/di.xml file.

<preference for="Magento\InventorySalesApi\Api\GetProductSalableQtyInterface" type="Magento\InventorySales\Model\GetProductSalableQty"/>
answered Aug 1, 2022 at 21:47
8
  • "message": "The requested qty is not available" Commented 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 stock Commented 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 GetProductSalableQtyInterface Commented 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. Commented Aug 2, 2022 at 13:11
  • 1
    its means : Magento\InventorySalesApi\Api\GetProductSalableQtyInterface look how it will be usable. Commented Aug 2, 2022 at 13:22
0

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"/>
answered Jul 30, 2022 at 7:21
3
  • preference exist in vendor but still cant instantiate error occurs Commented Aug 1, 2022 at 7:42
  • @S.Shah is Magento_IntentorySalesApi module enabled? Commented Aug 2, 2022 at 10:02
  • yes module is enabled Commented Aug 2, 2022 at 14:08
0

Run di:compile command

php bin/magento setup:di:compile

or you run this command

php bin/magento in:rein

will show the issue

answered Aug 2, 2022 at 13:36

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.