diff --git a/app/code/Magento/Catalog/Model/CategoryLinkManagement.php b/app/code/Magento/Catalog/Model/CategoryLinkManagement.php index 0c2c324bd8e2c..d9c0f2798f732 100644 --- a/app/code/Magento/Catalog/Model/CategoryLinkManagement.php +++ b/app/code/Magento/Catalog/Model/CategoryLinkManagement.php @@ -36,20 +36,20 @@ public function __construct( public function getAssignedProducts($categoryId) { $category = $this->categoryRepository->get($categoryId); - $productsPosition = $category->getProductsPosition(); - /** @var \Magento\Framework\Data\Collection\AbstractDb $products */ + /** @var \Magento\Catalog\Model\Resource\Product\Collection $products */ $products = $category->getProductCollection(); + $products->addFieldToSelect('position'); /** @var \Magento\Catalog\Api\Data\CategoryProductLinkInterface[] $links */ $links = []; /** @var \Magento\Catalog\Model\Product $product */ - foreach ($products->getItems() as $productId => $product) { + foreach ($products->getItems() as $product) { /** @var \Magento\Catalog\Api\Data\CategoryProductLinkInterface $link */ $link = $this->productLinkFactory->create(); $link->setSku($product->getSku()) - ->setPosition($productsPosition[$productId]) + ->setPosition($product->getData('cat_index_position')) ->setCategoryId($category->getId()); $links[] = $link; } diff --git a/app/code/Magento/Catalog/Model/Resource/Category/Flat.php b/app/code/Magento/Catalog/Model/Resource/Category/Flat.php index fcc0fbf435f68..e50743dc1b985 100644 --- a/app/code/Magento/Catalog/Model/Resource/Category/Flat.php +++ b/app/code/Magento/Catalog/Model/Resource/Category/Flat.php @@ -671,4 +671,23 @@ public function getAnchorsAbove(array $filterIds, $storeId = 0) return $this->_getReadAdapter()->fetchCol($select); } + + /** + * Get positions of associated to category products + * + * @param \Magento\Catalog\Model\Category $category + * @return array + */ + public function getProductsPosition($category) + { + $select = $this->_getReadAdapter()->select()->from( + $this->getTable('catalog_category_product'), + ['product_id', 'position'] + )->where( + 'category_id = :category_id' + ); + $bind = ['category_id' => (int)$category->getId()]; + + return $this->_getReadAdapter()->fetchPairs($select, $bind); + } } diff --git a/app/code/Magento/Catalog/Test/Unit/Model/CategoryLinkManagementTest.php b/app/code/Magento/Catalog/Test/Unit/Model/CategoryLinkManagementTest.php index 9e75726f9dfa2..994bf9de2b062 100644 --- a/app/code/Magento/Catalog/Test/Unit/Model/CategoryLinkManagementTest.php +++ b/app/code/Magento/Catalog/Test/Unit/Model/CategoryLinkManagementTest.php @@ -44,7 +44,7 @@ public function testGetAssignedProducts() { $categoryId = 42; $productId = 55; - $productsPosition = [$productId => 25]; + $position = 25; $productSku = 'testSku'; $categoryProductLinkMock = $this->getMock('\Magento\Catalog\Api\Data\CategoryProductLinkInterface'); $categoryMock = $this->getMock( @@ -56,13 +56,14 @@ public function testGetAssignedProducts() ); $productMock = $this->getMock('\Magento\Catalog\Model\Product', [], [], '', false); $productMock->expects($this->once())->method('getSku')->willReturn($productSku); + $productMock->expects($this->once())->method('getData')->with('cat_index_position')->willReturn($position); $items = [$productId => $productMock]; - $productsMock = $this->getMock('\Magento\Framework\Data\Collection\AbstractDb', [], [], '', false); + $productsMock = $this->getMock('Magento\Catalog\Model\Resource\Product\Collection', [], [], '', false); $this->categoryRepositoryMock->expects($this->once())->method('get')->with($categoryId) ->willReturn($categoryMock); - $categoryMock->expects($this->once())->method('getProductsPosition')->willReturn($productsPosition); $categoryMock->expects($this->once())->method('getProductCollection')->willReturn($productsMock); $categoryMock->expects($this->once())->method('getId')->willReturn($categoryId); + $productsMock->expects($this->once())->method('addFieldToSelect')->with('position')->willReturnSelf(); $productsMock->expects($this->once())->method('getItems')->willReturn($items); $this->productLinkFactoryMock->expects($this->once())->method('create')->willReturn($categoryProductLinkMock); $categoryProductLinkMock->expects($this->once()) @@ -71,7 +72,7 @@ public function testGetAssignedProducts() ->willReturnSelf(); $categoryProductLinkMock->expects($this->once()) ->method('setPosition') - ->with(25) + ->with($position) ->willReturnSelf(); $categoryProductLinkMock->expects($this->once()) ->method('setCategoryId')