For reference, Please install magento 2.3.0 instance with sample data and observe LifeLong Fitness IV product.
On product detail page, it's showing Out of Stock and it makes sense to me as quantity is 0 and it's not even using Advanced Inventory options as well for stock management.
How come it's showing Add to Cart button on home page/grid. when it is out of stock.
- Also the Backorders options is set as No Backorders.
According to my research, I have figured out that isSalable() is returning true for this product because of the function getStatusExpression() in /var/www/html/magento2/vendor/magento/module-catalog-inventory/Model/ResourceModel/Indexer/Stock/DefaultStock.php.
protected function getStatusExpression(AdapterInterface $connection, $isAggregate = false) { $isInStockExpression = $isAggregate ? 'MAX(cisi.is_in_stock)' : 'cisi.is_in_stock'; if ($this->_isManageStock()) { $statusExpr = $connection->getCheckSql( 'cisi.use_config_manage_stock = 0 AND cisi.manage_stock = 0', 1, $isInStockExpression ); } else { $statusExpr = $connection->getCheckSql( 'cisi.use_config_manage_stock = 0 AND cisi.manage_stock = 1', $isInStockExpression, 1 ); } return $statusExpr; }
Basically what it does is that whenever indexing is performed, it sets stock_status of product by default as 1 for products not using Advanced inventory options.
The reason why we see Add to Cart for the mentioned product is because it sets stock_status as 1 and makes it saleable product even though it's quantity is 0.
Please check this tables for reference
cataloginventory_stock_item
&
cataloginventory_stock_status
If you set stock_staus manually in cataloginventory_stock_status as 0, it will not show Add to Cart button.
What I want is that I don't want to show Add to Cart button if the product is out of stock.
Please let me know if there is any configuration for it that I'm missing or it requires any customization on core files or if it's a magento bug.