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;
    }
}

4 Answers 4

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"/>
8
  • "message": "The requested qty is not available"
    – S.Shah
    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
    – S.Shah
    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
1

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

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
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"/>
3
  • preference exist in vendor but still cant instantiate error occurs
    – S.Shah
    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
    – S.Shah
    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

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.