diff -Nuar a/vendor/magento/module-catalog-inventory/Model/ResourceModel/StockStatusFilter.php b/vendor/magento/module-catalog-inventory/Model/ResourceModel/StockStatusFilter.php new file mode 100644 index 00000000000..e9497a1d448 --- /dev/null +++ b/vendor/magento/module-catalog-inventory/Model/ResourceModel/StockStatusFilter.php @@ -0,0 +1,74 @@ +resource = $resource; + $this->stockConfiguration = $stockConfiguration; + } + + /** + * @inheritDoc + */ + public function execute( + Select $select, + string $productTableAlias, + string $stockStatusTableAlias = self::TABLE_ALIAS, + ?int $websiteId = null + ): Select { + $stockStatusTable = $this->resource->getTableName(self::TABLE_NAME); + $joinCondition = [ + "{$stockStatusTableAlias}.product_id = {$productTableAlias}.entity_id", + $select->getConnection()->quoteInto( + "{$stockStatusTableAlias}.website_id = ?", + $this->stockConfiguration->getDefaultScopeId() + ), + $select->getConnection()->quoteInto( + "{$stockStatusTableAlias}.stock_id = ?", + Stock::DEFAULT_STOCK_ID + ) + ]; + $select->join( + [$stockStatusTableAlias => $stockStatusTable], + implode(' AND ', $joinCondition), + [] + ); + $select->where("{$stockStatusTableAlias}.stock_status = ?", StockStatusInterface::STATUS_IN_STOCK); + + return $select; + } +} diff -Nuar a/vendor/magento/module-catalog-inventory/Model/ResourceModel/StockStatusFilterInterface.php b/vendor/magento/module-catalog-inventory/Model/ResourceModel/StockStatusFilterInterface.php new file mode 100644 index 00000000000..26eb4b0fa38 --- /dev/null +++ b/vendor/magento/module-catalog-inventory/Model/ResourceModel/StockStatusFilterInterface.php @@ -0,0 +1,34 @@ + + diff -Nuar a/vendor/magento/module-wishlist/Model/Adminhtml/ResourceModel/Item/Product/CollectionBuilder.php b/vendor/magento/module-wishlist/Model/Adminhtml/ResourceModel/Item/Product/CollectionBuilder.php new file mode 100644 index 00000000000..aa54c17c243 --- /dev/null +++ b/vendor/magento/module-wishlist/Model/Adminhtml/ResourceModel/Item/Product/CollectionBuilder.php @@ -0,0 +1,26 @@ +stockConfiguration = $stockConfiguration; $this->_adminhtmlSales = $adminhtmlSales; @@ -219,6 +238,10 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab parent::__construct($entityFactory, $logger, $fetchStrategy, $eventManager, $connection, $resource); $this->tableMaintainer = $tableMaintainer ?: ObjectManager::getInstance()->get(TableMaintainer::class); $this->salesConfig = $salesConfig ?: ObjectManager::getInstance()->get(ConfigInterface::class); + $this->productCollectionBuilder = $productCollectionBuilder + ?: ObjectManager::getInstance()->get(CollectionBuilderInterface::class); + $this->stockStatusFilter = $stockStatusFilter + ?: ObjectManager::getInstance()->get(StockStatusFilterInterface::class); } /** @@ -309,12 +332,10 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab $productCollection->setVisibility($this->_productVisibility->getVisibleInSiteIds()); } - $productCollection->addPriceData() - ->addTaxPercents() - ->addIdFilter($this->_productIds) - ->addAttributeToSelect($this->_wishlistConfig->getProductAttributes()) - ->addOptionsToResult() - ->addUrlRewrite(); + $productCollection->addIdFilter($this->_productIds) + ->addAttributeToSelect($this->_wishlistConfig->getProductAttributes()); + + $productCollection = $this->productCollectionBuilder->build($this, $productCollection); if ($this->_productSalable) { $productCollection = $this->_adminhtmlSales->applySalableProductTypesFilter($productCollection); @@ -362,15 +383,8 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab $connection = $this->getConnection(); if ($this->_productInStock && !$this->stockConfiguration->isShowOutOfStock()) { - $inStockConditions = [ - "stockItem.product_id = {$mainTableName}.product_id", - $connection->quoteInto('stockItem.stock_status = ?', Stock::STOCK_IN_STOCK), - ]; - $this->getSelect()->join( - ['stockItem' => $this->getTable('cataloginventory_stock_status')], - join(' AND ', $inStockConditions), - [] - ); + $this->joinProductTable(); + $this->stockStatusFilter->execute($this->getSelect(), 'product_entity', 'stockItem'); } if ($this->_productVisible) { @@ -576,10 +590,12 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab $storeId = $this->_storeManager->getStore(\Magento\Store\Model\Store::ADMIN_CODE)->getId(); $entityMetadata = $this->getMetadataPool()->getMetadata(ProductInterface::class); + $linkField = $entityMetadata->getLinkField(); + $this->joinProductTable(); $this->getSelect()->join( ['product_name_table' => $attribute->getBackendTable()], - 'product_name_table.' . $entityMetadata->getLinkField() . ' = main_table.product_id' . + 'product_name_table.' . $linkField . ' = product_entity.' . $linkField . ' AND product_name_table.store_id = ' . $storeId . ' AND product_name_table.attribute_id = ' . @@ -589,6 +605,7 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab $this->_isProductNameJoined = true; } + return $this; } @@ -661,4 +678,21 @@ class Collection extends \Magento\Framework\Model\ResourceModel\Db\Collection\Ab return $this; } + + /** + * Join product table to select if not already joined + * + * @return void + */ + private function joinProductTable(): void + { + if (!$this->isProductTableJoined) { + $this->getSelect()->join( + ['product_entity' => $this->getTable('catalog_product_entity')], + 'product_entity.entity_id = main_table.product_id', + [] + ); + $this->isProductTableJoined = true; + } + } } diff -Nuar a/vendor/magento/module-wishlist/Model/ResourceModel/Item/Product/CollectionBuilder.php b/vendor/magento/module-wishlist/Model/ResourceModel/Item/Product/CollectionBuilder.php new file mode 100644 index 00000000000..05255d1fe39 --- /dev/null +++ b/vendor/magento/module-wishlist/Model/ResourceModel/Item/Product/CollectionBuilder.php @@ -0,0 +1,28 @@ +addPriceData() + ->addTaxPercents() + ->addOptionsToResult() + ->addUrlRewrite(); + } +} diff -Nuar a/vendor/magento/module-wishlist/Model/ResourceModel/Item/Product/CollectionBuilderInterface.php b/vendor/magento/module-wishlist/Model/ResourceModel/Item/Product/CollectionBuilderInterface.php new file mode 100644 index 00000000000..1984d92e08a --- /dev/null +++ b/vendor/magento/module-wishlist/Model/ResourceModel/Item/Product/CollectionBuilderInterface.php @@ -0,0 +1,26 @@ +_useCurrentWebsite = $useCurrentWebsite; $this->_catalogProduct = $catalogProduct; @@ -217,8 +213,8 @@ class Wishlist extends AbstractModel implements IdentityInterface $this->serializer = $serializer ?: ObjectManager::getInstance()->get(Json::class); parent::__construct($context, $registry, $resource, $resourceCollection, $data); $this->productRepository = $productRepository; - $this->scopeConfig = $scopeConfig ?: ObjectManager::getInstance()->get(ScopeConfigInterface::class); - $this->stockRegistry = $stockRegistry ?: ObjectManager::getInstance()->get(StockRegistryInterface::class); + $this->stockConfiguration = $stockConfiguration + ?: ObjectManager::getInstance()->get(StockConfigurationInterface::class); } /** @@ -379,7 +375,7 @@ class Wishlist extends AbstractModel implements IdentityInterface $this )->addStoreFilter( $this->getSharedStoreIds() - )->setVisibilityFilter(); + )->setVisibilityFilter($this->_useCurrentWebsite); } return $this->_itemCollection; @@ -457,7 +453,7 @@ class Wishlist extends AbstractModel implements IdentityInterface throw new LocalizedException(__('Cannot specify product.')); } - if ($this->isInStock($productId)) { + if (!$this->stockConfiguration->isShowOutOfStock($storeId) && !$product->getIsSalable()) { throw new LocalizedException(__('Cannot add product without stock to wishlist.')); } @@ -650,24 +646,6 @@ class Wishlist extends AbstractModel implements IdentityInterface } /** - * Retrieve if product has stock or config is set for showing out of stock products - * - * @param int $productId - * @return bool - */ - private function isInStock($productId) - { - /** @var StockItemInterface $stockItem */ - $stockItem = $this->stockRegistry->getStockItem($productId); - $showOutOfStock = $this->scopeConfig->isSetFlag( - Configuration::XML_PATH_SHOW_OUT_OF_STOCK, - ScopeInterface::SCOPE_STORE - ); - $isInStock = $stockItem ? $stockItem->getIsInStock() : false; - return !$isInStock && !$showOutOfStock; - } - - /** * Check customer is owner this wishlist * * @param int $customerId diff -Nuar a/vendor/magento/module-wishlist/etc/adminhtml/di.xml b/vendor/magento/module-wishlist/etc/adminhtml/di.xml index 124b8c17c3f..be4d1966a46 100644 --- a/vendor/magento/module-wishlist/etc/adminhtml/di.xml +++ b/vendor/magento/module-wishlist/etc/adminhtml/di.xml @@ -24,4 +24,6 @@ + diff -Nuar a/vendor/magento/module-wishlist/etc/di.xml b/vendor/magento/module-wishlist/etc/di.xml index c0230a5326f..924bdfa3eb5 100644 --- a/vendor/magento/module-wishlist/etc/di.xml +++ b/vendor/magento/module-wishlist/etc/di.xml @@ -78,4 +78,6 @@ +