diff --git a/src/module-elasticsuite-virtual-category/Model/ResourceModel/Category/Product/Position.php b/src/module-elasticsuite-virtual-category/Model/ResourceModel/Category/Product/Position.php index b86c96127..e0803d22a 100644 --- a/src/module-elasticsuite-virtual-category/Model/ResourceModel/Category/Product/Position.php +++ b/src/module-elasticsuite-virtual-category/Model/ResourceModel/Category/Product/Position.php @@ -31,6 +31,44 @@ class Position extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb */ const TABLE_NAME = 'smile_virtualcategory_catalog_category_product_position'; + /** + * Get product positions for a given categoryId and Store Id. + * + * @param int $categoryId The Category Id. + * @param int $storeId The Store Id. + * + * @return array + */ + public function getProductPositions($categoryId, $storeId) + { + $select = $this->getBaseSelect() + ->where('category_id = ?', (int) $categoryId) + ->where('store_id = ?', (int) $storeId) + ->where('position IS NOT NULL') + ->columns(['product_id', 'position']); + + return $this->getConnection()->fetchPairs($select); + } + + /** + * Get product blacklist for a given categoryId and Store Id. + * + * @param int $categoryId The Category Id. + * @param int $storeId The Store Id. + * + * @return array + */ + public function getProductBlacklist($categoryId, $storeId) + { + $select = $this->getBaseSelect() + ->columns(['product_id']) + ->where('category_id = ?', (int) $categoryId) + ->where('store_id = ?', (int) $storeId) + ->where('is_blacklisted = ?', (int) true); + + return $this->getConnection()->fetchCol($select); + } + /** * Load product positions for the given category. * @@ -40,7 +78,7 @@ class Position extends \Magento\Framework\Model\ResourceModel\Db\AbstractDb */ public function getProductPositionsByCategory($category) { - $storeId = 0; + $storeId = \Magento\Store\Model\Store::DEFAULT_STORE_ID; if (is_object($category)) { if ($category->getUseStorePositions()) { $storeId = $category->getStoreId(); @@ -48,13 +86,7 @@ public function getProductPositionsByCategory($category) $category = $category->getId(); } - $select = $this->getBaseSelect() - ->where('category_id = ?', (int) $category) - ->where('store_id = ?', (int) $storeId) - ->where('position IS NOT NULL') - ->columns(['product_id', 'position']); - - return $this->getConnection()->fetchPairs($select); + return $this->getProductPositions($category, $storeId); } /** @@ -66,7 +98,7 @@ public function getProductPositionsByCategory($category) */ public function getProductBlacklistByCategory($category) { - $storeId = 0; + $storeId = \Magento\Store\Model\Store::DEFAULT_STORE_ID; if (is_object($category)) { if ($category->getUseStorePositions()) { $storeId = $category->getStoreId(); @@ -74,13 +106,7 @@ public function getProductBlacklistByCategory($category) $category = $category->getId(); } - $select = $this->getBaseSelect() - ->columns(['product_id']) - ->where('category_id = ?', (int) $category) - ->where('store_id = ?', (int) $storeId) - ->where('is_blacklisted = ?', (int) true); - - return $this->getConnection()->fetchCol($select); + return $this->getProductBlacklist($category, $storeId); } /** diff --git a/src/module-elasticsuite-virtual-category/Plugin/Catalog/Category/SaveProductsPositions.php b/src/module-elasticsuite-virtual-category/Plugin/Catalog/Category/SaveProductsPositions.php index 262c84768..63f42c775 100644 --- a/src/module-elasticsuite-virtual-category/Plugin/Catalog/Category/SaveProductsPositions.php +++ b/src/module-elasticsuite-virtual-category/Plugin/Catalog/Category/SaveProductsPositions.php @@ -101,16 +101,36 @@ function () use ($category) { */ private function getAffectedProductIds($category) { - $oldPositionProductIds = array_keys($this->saveHandler->getProductPositionsByCategory($category)); - $newPositionProductIds = array_keys($category->getSortedProducts()); + $oldPositionProductIds = array_keys($this->saveHandler->getProductPositionsByCategory($category)); + $defaultPositionProductIds = []; + $newPositionProductIds = array_keys($category->getSortedProducts()); + + $oldBlacklistedProductIds = array_values($this->saveHandler->getProductBlacklistByCategory($category)); + $defaultBlacklistedProductIds = []; + $newBlacklistedProductIds = array_values($category->getBlacklistedProducts() ?? []); + + if (true === (bool) $category->getUseStorePositions()) { + $defaultPositionProductIds = array_keys( + $this->saveHandler->getProductPositions( + $category->getId(), + \Magento\Store\Model\Store::DEFAULT_STORE_ID + ) + ); - $oldBlacklistedProductIds = array_values($this->saveHandler->getProductBlacklistByCategory($category)); - $newBlacklistedProductIds = array_values($category->getBlacklistedProducts() ?? []); + $defaultBlacklistedProductIds = array_values( + $this->saveHandler->getProductBlacklist( + $category->getId(), + \Magento\Store\Model\Store::DEFAULT_STORE_ID + ) + ); + } $affectedProductIds = array_merge( $oldPositionProductIds, + $defaultPositionProductIds, $newPositionProductIds, $oldBlacklistedProductIds, + $defaultBlacklistedProductIds, $newBlacklistedProductIds );