Skip to content

Commit 74e1ea9

Browse files
committed
[Forwardport] Make sure all linked products (related, upsells, crosssells) show up in the backend grids and in the correct order. Fixes #13720
(cherry picked from commit a3f1c38)
1 parent 8fd89cf commit 74e1ea9

File tree

2 files changed

+25
-17
lines changed

2 files changed

+25
-17
lines changed

app/code/Magento/Catalog/Model/ProductLink/CollectionProvider.php

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -47,22 +47,20 @@ public function getCollection(\Magento\Catalog\Model\Product $product, $type)
4747

4848
$products = $this->providers[$type]->getLinkedProducts($product);
4949
$converter = $this->converterPool->getConverter($type);
50-
$output = [];
5150
$sorterItems = [];
5251
foreach ($products as $item) {
53-
$output[$item->getId()] = $converter->convert($item);
52+
$itemId = $item->getId();
53+
$sorterItems[$itemId] = $converter->convert($item);
54+
$sorterItems[$itemId]['position'] = $sorterItems[$itemId]['position'] ?? 0;
5455
}
5556

56-
foreach ($output as $item) {
57-
$itemPosition = $item['position'];
58-
if (!isset($sorterItems[$itemPosition])) {
59-
$sorterItems[$itemPosition] = $item;
60-
} else {
61-
$newPosition = $itemPosition + 1;
62-
$sorterItems[$newPosition] = $item;
63-
}
64-
}
65-
ksort($sorterItems);
57+
usort($sorterItems, function ($itemA, $itemB) {
58+
$posA = intval($itemA['position']);
59+
$posB = intval($itemB['position']);
60+
61+
return $posA <=> $posB;
62+
});
63+
6664
return $sorterItems;
6765
}
6866
}

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

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,10 +57,14 @@ public function testGetCollection()
5757
$linkedProductOneMock = $this->createMock(Product::class);
5858
$linkedProductTwoMock = $this->createMock(Product::class);
5959
$linkedProductThreeMock = $this->createMock(Product::class);
60+
$linkedProductFourMock = $this->createMock(Product::class);
61+
$linkedProductFiveMock = $this->createMock(Product::class);
6062

6163
$linkedProductOneMock->expects($this->once())->method('getId')->willReturn(1);
6264
$linkedProductTwoMock->expects($this->once())->method('getId')->willReturn(2);
6365
$linkedProductThreeMock->expects($this->once())->method('getId')->willReturn(3);
66+
$linkedProductFourMock->expects($this->once())->method('getId')->willReturn(4);
67+
$linkedProductFiveMock->expects($this->once())->method('getId')->willReturn(5);
6468

6569
$this->converterPoolMock->expects($this->once())
6670
->method('getConverter')
@@ -71,9 +75,11 @@ public function testGetCollection()
7175
[$linkedProductOneMock, ['name' => 'Product One', 'position' => 10]],
7276
[$linkedProductTwoMock, ['name' => 'Product Two', 'position' => 2]],
7377
[$linkedProductThreeMock, ['name' => 'Product Three', 'position' => 2]],
78+
[$linkedProductFourMock, ['name' => 'Product Four', 'position' => null]],
79+
[$linkedProductFiveMock, ['name' => 'Product Five']],
7480
];
7581

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

7884
$this->providerMock->expects($this->once())
7985
->method('getLinkedProducts')
@@ -82,14 +88,18 @@ public function testGetCollection()
8288
[
8389
$linkedProductOneMock,
8490
$linkedProductTwoMock,
85-
$linkedProductThreeMock
91+
$linkedProductThreeMock,
92+
$linkedProductFourMock,
93+
$linkedProductFiveMock,
8694
]
8795
);
8896

8997
$expectedResult = [
90-
2 => ['name' => 'Product Two', 'position' => 2],
91-
3 => ['name' => 'Product Three', 'position' => 2],
92-
10 => ['name' => 'Product One', 'position' => 10],
98+
0 => ['name' => 'Product Four', 'position' => 0],
99+
1 => ['name' => 'Product Five', 'position' => 0],
100+
2 => ['name' => 'Product Three', 'position' => 2],
101+
3 => ['name' => 'Product Two', 'position' => 2],
102+
4 => ['name' => 'Product One', 'position' => 10],
93103
];
94104

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

0 commit comments

Comments
 (0)