Skip to content

Commit

Permalink
[Forwardport] Make sure all linked products (related, upsells, crosss…
Browse files Browse the repository at this point in the history
…ells) show up in the backend grids and in the correct order. Fixes magento#13720

(cherry picked from commit a3f1c38)
  • Loading branch information
hostep committed Sep 23, 2018
1 parent 8fd89cf commit 74e1ea9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
22 changes: 10 additions & 12 deletions app/code/Magento/Catalog/Model/ProductLink/CollectionProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,22 +47,20 @@ public function getCollection(\Magento\Catalog\Model\Product $product, $type)

$products = $this->providers[$type]->getLinkedProducts($product);
$converter = $this->converterPool->getConverter($type);
$output = [];
$sorterItems = [];
foreach ($products as $item) {
$output[$item->getId()] = $converter->convert($item);
$itemId = $item->getId();
$sorterItems[$itemId] = $converter->convert($item);
$sorterItems[$itemId]['position'] = $sorterItems[$itemId]['position'] ?? 0;
}

foreach ($output as $item) {
$itemPosition = $item['position'];
if (!isset($sorterItems[$itemPosition])) {
$sorterItems[$itemPosition] = $item;
} else {
$newPosition = $itemPosition + 1;
$sorterItems[$newPosition] = $item;
}
}
ksort($sorterItems);
usort($sorterItems, function ($itemA, $itemB) {
$posA = intval($itemA['position']);
$posB = intval($itemB['position']);

return $posA <=> $posB;
});

return $sorterItems;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,14 @@ public function testGetCollection()
$linkedProductOneMock = $this->createMock(Product::class);
$linkedProductTwoMock = $this->createMock(Product::class);
$linkedProductThreeMock = $this->createMock(Product::class);
$linkedProductFourMock = $this->createMock(Product::class);
$linkedProductFiveMock = $this->createMock(Product::class);

$linkedProductOneMock->expects($this->once())->method('getId')->willReturn(1);
$linkedProductTwoMock->expects($this->once())->method('getId')->willReturn(2);
$linkedProductThreeMock->expects($this->once())->method('getId')->willReturn(3);
$linkedProductFourMock->expects($this->once())->method('getId')->willReturn(4);
$linkedProductFiveMock->expects($this->once())->method('getId')->willReturn(5);

$this->converterPoolMock->expects($this->once())
->method('getConverter')
Expand All @@ -71,9 +75,11 @@ public function testGetCollection()
[$linkedProductOneMock, ['name' => 'Product One', 'position' => 10]],
[$linkedProductTwoMock, ['name' => 'Product Two', 'position' => 2]],
[$linkedProductThreeMock, ['name' => 'Product Three', 'position' => 2]],
[$linkedProductFourMock, ['name' => 'Product Four', 'position' => null]],
[$linkedProductFiveMock, ['name' => 'Product Five']],
];

$this->converterMock->expects($this->exactly(3))->method('convert')->willReturnMap($map);
$this->converterMock->expects($this->exactly(5))->method('convert')->willReturnMap($map);

$this->providerMock->expects($this->once())
->method('getLinkedProducts')
Expand All @@ -82,14 +88,18 @@ public function testGetCollection()
[
$linkedProductOneMock,
$linkedProductTwoMock,
$linkedProductThreeMock
$linkedProductThreeMock,
$linkedProductFourMock,
$linkedProductFiveMock,
]
);

$expectedResult = [
2 => ['name' => 'Product Two', 'position' => 2],
3 => ['name' => 'Product Three', 'position' => 2],
10 => ['name' => 'Product One', 'position' => 10],
0 => ['name' => 'Product Four', 'position' => 0],
1 => ['name' => 'Product Five', 'position' => 0],
2 => ['name' => 'Product Three', 'position' => 2],
3 => ['name' => 'Product Two', 'position' => 2],
4 => ['name' => 'Product One', 'position' => 10],
];

$actualResult = $this->model->getCollection($this->productMock, 'crosssell');
Expand Down

0 comments on commit 74e1ea9

Please sign in to comment.