Skip to content

Commit a7eb8f8

Browse files
Slabko,Michael(mslabko)Slabko,Michael(mslabko)
Slabko,Michael(mslabko)
authored and
Slabko,Michael(mslabko)
committed
Merge pull request #48 from magento-goinc/merchant_beta
[GoInc] MAGETWO-44237
2 parents ea0362a + 6855934 commit a7eb8f8

File tree

3 files changed

+28
-8
lines changed

3 files changed

+28
-8
lines changed

app/code/Magento/Catalog/Model/CategoryLinkManagement.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -36,20 +36,20 @@ public function __construct(
3636
public function getAssignedProducts($categoryId)
3737
{
3838
$category = $this->categoryRepository->get($categoryId);
39-
$productsPosition = $category->getProductsPosition();
4039

41-
/** @var \Magento\Framework\Data\Collection\AbstractDb $products */
40+
/** @var \Magento\Catalog\Model\Resource\Product\Collection $products */
4241
$products = $category->getProductCollection();
42+
$products->addFieldToSelect('position');
4343

4444
/** @var \Magento\Catalog\Api\Data\CategoryProductLinkInterface[] $links */
4545
$links = [];
4646

4747
/** @var \Magento\Catalog\Model\Product $product */
48-
foreach ($products->getItems() as $productId => $product) {
48+
foreach ($products->getItems() as $product) {
4949
/** @var \Magento\Catalog\Api\Data\CategoryProductLinkInterface $link */
5050
$link = $this->productLinkFactory->create();
5151
$link->setSku($product->getSku())
52-
->setPosition($productsPosition[$productId])
52+
->setPosition($product->getData('cat_index_position'))
5353
->setCategoryId($category->getId());
5454
$links[] = $link;
5555
}

app/code/Magento/Catalog/Model/Resource/Category/Flat.php

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -671,4 +671,23 @@ public function getAnchorsAbove(array $filterIds, $storeId = 0)
671671

672672
return $this->_getReadAdapter()->fetchCol($select);
673673
}
674+
675+
/**
676+
* Get positions of associated to category products
677+
*
678+
* @param \Magento\Catalog\Model\Category $category
679+
* @return array
680+
*/
681+
public function getProductsPosition($category)
682+
{
683+
$select = $this->_getReadAdapter()->select()->from(
684+
$this->getTable('catalog_category_product'),
685+
['product_id', 'position']
686+
)->where(
687+
'category_id = :category_id'
688+
);
689+
$bind = ['category_id' => (int)$category->getId()];
690+
691+
return $this->_getReadAdapter()->fetchPairs($select, $bind);
692+
}
674693
}

app/code/Magento/Catalog/Test/Unit/Model/CategoryLinkManagementTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function testGetAssignedProducts()
4444
{
4545
$categoryId = 42;
4646
$productId = 55;
47-
$productsPosition = [$productId => 25];
47+
$position = 25;
4848
$productSku = 'testSku';
4949
$categoryProductLinkMock = $this->getMock('\Magento\Catalog\Api\Data\CategoryProductLinkInterface');
5050
$categoryMock = $this->getMock(
@@ -56,13 +56,14 @@ public function testGetAssignedProducts()
5656
);
5757
$productMock = $this->getMock('\Magento\Catalog\Model\Product', [], [], '', false);
5858
$productMock->expects($this->once())->method('getSku')->willReturn($productSku);
59+
$productMock->expects($this->once())->method('getData')->with('cat_index_position')->willReturn($position);
5960
$items = [$productId => $productMock];
60-
$productsMock = $this->getMock('\Magento\Framework\Data\Collection\AbstractDb', [], [], '', false);
61+
$productsMock = $this->getMock('Magento\Catalog\Model\Resource\Product\Collection', [], [], '', false);
6162
$this->categoryRepositoryMock->expects($this->once())->method('get')->with($categoryId)
6263
->willReturn($categoryMock);
63-
$categoryMock->expects($this->once())->method('getProductsPosition')->willReturn($productsPosition);
6464
$categoryMock->expects($this->once())->method('getProductCollection')->willReturn($productsMock);
6565
$categoryMock->expects($this->once())->method('getId')->willReturn($categoryId);
66+
$productsMock->expects($this->once())->method('addFieldToSelect')->with('position')->willReturnSelf();
6667
$productsMock->expects($this->once())->method('getItems')->willReturn($items);
6768
$this->productLinkFactoryMock->expects($this->once())->method('create')->willReturn($categoryProductLinkMock);
6869
$categoryProductLinkMock->expects($this->once())
@@ -71,7 +72,7 @@ public function testGetAssignedProducts()
7172
->willReturnSelf();
7273
$categoryProductLinkMock->expects($this->once())
7374
->method('setPosition')
74-
->with(25)
75+
->with($position)
7576
->willReturnSelf();
7677
$categoryProductLinkMock->expects($this->once())
7778
->method('setCategoryId')

0 commit comments

Comments
 (0)