diff -Nuar a/vendor/magento/module-inventory-configurable-product/Pricing/Price/Indexer/StockStatusBaseSelectProcessor.php b/vendor/magento/module-inventory-configurable-product/Pricing/Price/Indexer/StockStatusBaseSelectProcessor.php new file mode 100644 index 00000000000..68cb883c79a --- /dev/null +++ b/vendor/magento/module-inventory-configurable-product/Pricing/Price/Indexer/StockStatusBaseSelectProcessor.php @@ -0,0 +1,138 @@ +stockIndexTableNameResolver = $stockIndexTableNameResolver; + $this->stockConfig = $stockConfig; + $this->storeManager = $storeManager; + $this->stockResolver = $stockResolver; + $this->resourceConnection = $resourceConnection; + $this->defaultStockProvider = $defaultStockProvider; + $this->metadataPool = $metadataPool; + } + + /** + * Improves the select with stock status sub query. + * + * @param Select $select + * @return Select + * @throws NoSuchEntityException + */ + public function process(Select $select) + { + if (!$this->stockConfig->isShowOutOfStock()) { + return $select; + } + + $websiteCode = $this->storeManager->getWebsite()->getCode(); + $stock = $this->stockResolver->execute(SalesChannelInterface::TYPE_WEBSITE, $websiteCode); + $stockId = (int)$stock->getStockId(); + if ($stockId === $this->defaultStockProvider->getId()) { + return $select; + } + + $metadata = $this->metadataPool->getMetadata(ProductInterface::class); + $linkField = $metadata->getLinkField(); + + $select->joinInner( + ['le2' => $this->resourceConnection->getTableName('catalog_product_entity')], + 'le2.' . $linkField . ' = l.product_id', + [] + )->joinInner( + ['stock' => $this->stockIndexTableNameResolver->execute($stockId)], + 'stock.sku = le2.sku', + [] + )->joinInner( + ['stock_parent' => $this->stockIndexTableNameResolver->execute($stockId)], + 'stock_parent.sku = le.sku', + [] + )->where( + 'stock.is_salable = ?', + 1 + )->orWhere( + 'stock.is_salable = ?', + 0 + )->where( + 'stock_parent.is_salable = ?', + 0 + ); + + return $select; + } +} diff -Nuar a/vendor/magento/module-inventory-configurable-product/etc/di.xml b/vendor/magento/module-inventory-configurable-product/etc/di.xml index 77358c2d991..3fe63f887b6 100644 --- a/vendor/magento/module-inventory-configurable-product/etc/di.xml +++ b/vendor/magento/module-inventory-configurable-product/etc/di.xml @@ -26,4 +26,9 @@ + + + Magento\InventoryConfigurableProduct\Pricing\Price\Indexer\StockStatusBaseSelectProcessor + +