diff -Nuar a/vendor/magento/module-inventory-catalog/Model/ResourceModel/AddIsInStockFieldToCollection.php b/vendor/magento/module-inventory-catalog/Model/ResourceModel/AddIsInStockFieldToCollection.php index 5fcf9df771a..c94808dbf7b 100644 --- a/vendor/magento/module-inventory-catalog/Model/ResourceModel/AddIsInStockFieldToCollection.php +++ b/vendor/magento/module-inventory-catalog/Model/ResourceModel/AddIsInStockFieldToCollection.php @@ -8,7 +8,6 @@ declare(strict_types=1); namespace Magento\InventoryCatalog\Model\ResourceModel; use Magento\Catalog\Model\ResourceModel\Product\Collection; -use Magento\InventoryIndexer\Indexer\IndexStructure; use Magento\InventoryIndexer\Model\StockIndexTableNameResolverInterface; /** @@ -20,14 +19,21 @@ class AddIsInStockFieldToCollection * @var StockIndexTableNameResolverInterface */ private $stockIndexTableProvider; + /** + * @var StockStatusFilter + */ + private $stockStatusFilter; /** * @param StockIndexTableNameResolverInterface $stockIndexTableProvider + * @param StockStatusFilter $stockStatusFilter */ public function __construct( - StockIndexTableNameResolverInterface $stockIndexTableProvider + StockIndexTableNameResolverInterface $stockIndexTableProvider, + StockStatusFilter $stockStatusFilter ) { $this->stockIndexTableProvider = $stockIndexTableProvider; + $this->stockStatusFilter = $stockStatusFilter; } /** @@ -39,12 +45,11 @@ class AddIsInStockFieldToCollection */ public function execute($collection, int $stockId) { - $tableName = $this->stockIndexTableProvider->execute($stockId); - - $collection->getSelect()->join( - ['inventory_in_stock' => $tableName], - 'e.sku = inventory_in_stock.sku', - [] - )->where('inventory_in_stock.' . IndexStructure::IS_SALABLE . ' = ?', 1); + $this->stockStatusFilter->execute( + $collection->getSelect(), + 'e', + 'inventory_in_stock', + $stockId + ); } } diff -Nuar a/vendor/magento/module-inventory-catalog/Model/ResourceModel/AddIsInStockFilterToCollection.php b/vendor/magento/module-inventory-catalog/Model/ResourceModel/AddIsInStockFilterToCollection.php index c94d9b873c3..6631db8058d 100644 --- a/vendor/magento/module-inventory-catalog/Model/ResourceModel/AddIsInStockFilterToCollection.php +++ b/vendor/magento/module-inventory-catalog/Model/ResourceModel/AddIsInStockFilterToCollection.php @@ -8,7 +8,6 @@ declare(strict_types=1); namespace Magento\InventoryCatalog\Model\ResourceModel; use Magento\Catalog\Model\ResourceModel\Product\Collection; -use Magento\InventoryIndexer\Indexer\IndexStructure; use Magento\InventoryIndexer\Model\StockIndexTableNameResolverInterface; /** @@ -20,29 +19,37 @@ class AddIsInStockFilterToCollection * @var StockIndexTableNameResolverInterface */ private $stockIndexTableProvider; + /** + * @var StockStatusFilter + */ + private $stockStatusFilter; /** * @param StockIndexTableNameResolverInterface $stockIndexTableProvider + * @param StockStatusFilter $stockStatusFilter */ public function __construct( - StockIndexTableNameResolverInterface $stockIndexTableProvider + StockIndexTableNameResolverInterface $stockIndexTableProvider, + StockStatusFilter $stockStatusFilter ) { $this->stockIndexTableProvider = $stockIndexTableProvider; + $this->stockStatusFilter = $stockStatusFilter; } /** + * Modify "is in stock" collection filter to support non-default stocks. + * * @param Collection $collection * @param int $stockId * @return void */ public function execute($collection, int $stockId) { - $tableName = $this->stockIndexTableProvider->execute($stockId); - - $collection->getSelect()->join( - ['stock_status_index' => $tableName], - 'e.sku = stock_status_index.sku', - [] - )->where('stock_status_index.' . IndexStructure::IS_SALABLE . ' = ?', 1); + $this->stockStatusFilter->execute( + $collection->getSelect(), + 'e', + 'stock_status_index', + $stockId + ); } } diff -Nuar a/vendor/magento/module-inventory-catalog/Model/ResourceModel/StockStatusFilter.php b/vendor/magento/module-inventory-catalog/Model/ResourceModel/StockStatusFilter.php new file mode 100644 index 00000000000..6c5cd84d538 --- /dev/null +++ b/vendor/magento/module-inventory-catalog/Model/ResourceModel/StockStatusFilter.php @@ -0,0 +1,58 @@ +stockIndexTableProvider = $stockIndexTableProvider; + } + + /** + * Add in-stock status constraint to the select for non default stock + * + * @param Select $select + * @param string $productTableAlias + * @param string $stockStatusTableAlias + * @param int $stockId + * @return Select + */ + public function execute( + Select $select, + string $productTableAlias, + string $stockStatusTableAlias, + int $stockId + ): Select { + $stockStatusTable = $this->stockIndexTableProvider->execute($stockId); + $isSalableFieldName = IndexStructure::IS_SALABLE; + $select->join( + [$stockStatusTableAlias => $stockStatusTable], + "{$stockStatusTableAlias}.sku = {$productTableAlias}.sku", + [] + ); + $select->where("{$stockStatusTableAlias}.{$isSalableFieldName} = ?", 1); + return $select; + } +} diff -Nuar a/vendor/magento/module-inventory-catalog/Plugin/CatalogInventory/Model/ResourceModel/StockStatusFilterPlugin.php b/vendor/magento/module-inventory-catalog/Plugin/CatalogInventory/Model/ResourceModel/StockStatusFilterPlugin.php new file mode 100644 index 00000000000..d21aa0f0dd0 --- /dev/null +++ b/vendor/magento/module-inventory-catalog/Plugin/CatalogInventory/Model/ResourceModel/StockStatusFilterPlugin.php @@ -0,0 +1,100 @@ +storeManager = $storeManager; + $this->stockResolver = $stockResolver; + $this->defaultStockProvider = $defaultStockProvider; + $this->stockStatusFilter = $stockStatusFilter; + } + + /** + * Add in-stock status constraint to the select for non default stock + * + * @param StockStatusFilterInterface $subject + * @param callable $proceed + * @param Select $select + * @param string $productTableAlias + * @param string $stockStatusTableAlias + * @param int|null $websiteId + * @return Select + * @throws \Magento\Framework\Exception\LocalizedException + * @throws \Magento\Framework\Exception\NoSuchEntityException + * @SuppressWarnings(PHPMD.UnusedFormalParameter) + */ + public function aroundExecute( + StockStatusFilterInterface $subject, + callable $proceed, + Select $select, + string $productTableAlias, + string $stockStatusTableAlias, + ?int $websiteId = null + ): Select { + $websiteCode = $this->storeManager->getWebsite($websiteId)->getCode(); + $stock = $this->stockResolver->execute(SalesChannelInterface::TYPE_WEBSITE, $websiteCode); + $stockId = (int)$stock->getStockId(); + if ($this->defaultStockProvider->getId() === $stockId) { + $select = $proceed( + $select, + $productTableAlias, + $stockStatusTableAlias, + $websiteId + ); + } else { + $select = $this->stockStatusFilter->execute( + $select, + $productTableAlias, + $stockStatusTableAlias, + $stockId + ); + } + return $select; + } +} diff -Nuar a/vendor/magento/module-inventory-catalog/etc/di.xml b/vendor/magento/module-inventory-catalog/etc/di.xml index a6f836c3835..28279ce8a55 100644 --- a/vendor/magento/module-inventory-catalog/etc/di.xml +++ b/vendor/magento/module-inventory-catalog/etc/di.xml @@ -156,4 +156,7 @@ + + +