Skip to content

Commit

Permalink
Merge pull request #48 from magento-goinc/merchant_beta
Browse files Browse the repository at this point in the history
[GoInc] MAGETWO-44237
  • Loading branch information
Slabko,Michael(mslabko) authored and Slabko,Michael(mslabko) committed Oct 27, 2015
2 parents ea0362a + 6855934 commit a7eb8f8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 8 deletions.
8 changes: 4 additions & 4 deletions app/code/Magento/Catalog/Model/CategoryLinkManagement.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
19 changes: 19 additions & 0 deletions app/code/Magento/Catalog/Model/Resource/Category/Flat.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -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())
Expand All @@ -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')
Expand Down

0 comments on commit a7eb8f8

Please sign in to comment.