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;

Debug:

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