M2 – Add a product into the cart – message “There are no source items with the in-stock status”

A Bug story :

I have faced an issue on my customer website that a few products can not be added into a cart. An error message I got is ” There are no source items with the in-stock status”. I tried to deeply debug ( see the steps below ) and realized this SQL return empty result.

SELECT `main_table`.* FROM `inventory_source_item` AS `main_table` WHERE ((`sku` = 'XXX-YYY')) AND ((`source_code` IN('default'))) AND ((`status` = 1))

A strange thing, everything in the backend shows that the product has qty of 2 and in-stock status. Then I go to MySQLAdmin to find a record relate to the product SKU from table “inventory_source_item”. It shows me a record with qty = 0 and status = 0.

I’m sure we have done reindex a thousand times. I tried to save the product again from the backend. And then, what amazing !!!, it works and qty and stock status are updated correctly on table “inventory_source_item” for the product.

I think the problem happened because we did migrate data from Magento 1 to Magento 2.

Here is quick fixing for this issue for every product without saving each product on the backend.

NOTE !!! In-case: You have only stock source: “default “

- Backup table inventory_source_item
- Truncate table inventory_source_item

INSERT INTO inventory_source_item (`source_code`,`sku`, `quantity`, `status`) 
(
 SELECT 'default', t_p.`sku`, t_status.`qty`, t_status.`stock_status` FROM cataloginventory_stock_status t_status
 INNER JOIN catalog_product_entity t_p ON t_p.entity_id = t_status.product_id 
)
ON DUPLICATE KEY UPDATE status = status , quantity = quantity;

Magento\InventorySales\Model\IsProductSalableForRequestedQtyCondition\\IsAnySourceItemInStockCondition
public function execute(string $sku, int $stockId, float $requestedQty): ProductSalableResultInterface
    {
        $errors = [];

        if (!$this->isAnySourceInStockCondition->execute($sku, $stockId)) {
            $data = [
                'code' => 'is_any_source_item_in_stock-no_source_items_in_stock',
                'message' => __('There are no source items with the in stock status')
            ];
            $errors[] = $this->productSalabilityErrorFactory->create($data);
        }

        return $this->productSalableResultFactory->create(['errors' => $errors]);
    }
Magento\Inventory\Model\SourceItemRepository
public function getList(SearchCriteriaInterface $searchCriteria): SourceItemSearchResultsInterface
    {
        return $this->commandGetList->execute($searchCriteria);
    }

Magento\Inventory\Model\SourceItem\Command\GetList
public function execute(SearchCriteriaInterface $searchCriteria): SourceItemSearchResultsInterface
    {
        /** @var Collection $collection */
        $collection = $this->sourceItemCollectionFactory->create();
        $this->collectionProcessor->process($searchCriteria, $collection);

        /** @var SourceItemSearchResultsInterface $searchResult */
        $searchResult = $this->sourceItemSearchResultsFactory->create();
        $searchResult->setItems($collection->getItems());
        $searchResult->setTotalCount($collection->getSize());
        $searchResult->setSearchCriteria($searchCriteria);
       
        return $searchResult;
    }

1 Reply to “M2 – Add a product into the cart – message “There are no source items with the in-stock status””

  1. Thank you, kind Sir. I found you thanks to Google search and this helped me fix the exact same problem I was having. Update the query you don’t need the () before Select. Having over 43k products with this issue going in and saving them manually in the admin was not even an option. Also, our issue was due to data migration. So again big thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *