Skip to content

Commit d1a27f4

Browse files
author
Stanislav Idolov
authored
ENGCOM-3035: [Forwardport] Make sure all linked products (related, upsells, crosss… #18207
2 parents 9c4e95d + d0041c0 commit d1a27f4

File tree

2 files changed

+28
-17
lines changed

2 files changed

+28
-17
lines changed

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

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@
99
use Magento\Catalog\Model\ProductLink\Converter\ConverterPool;
1010
use Magento\Framework\Exception\NoSuchEntityException;
1111

12+
/**
13+
* Provides a collection of linked product items (crosssells, related, upsells, ...)
14+
*/
1215
class CollectionProvider
1316
{
1417
/**
@@ -47,22 +50,20 @@ public function getCollection(\Magento\Catalog\Model\Product $product, $type)
4750

4851
$products = $this->providers[$type]->getLinkedProducts($product);
4952
$converter = $this->converterPool->getConverter($type);
50-
$output = [];
5153
$sorterItems = [];
5254
foreach ($products as $item) {
53-
$output[$item->getId()] = $converter->convert($item);
55+
$itemId = $item->getId();
56+
$sorterItems[$itemId] = $converter->convert($item);
57+
$sorterItems[$itemId]['position'] = $sorterItems[$itemId]['position'] ?? 0;
5458
}
5559

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);
60+
usort($sorterItems, function ($itemA, $itemB) {
61+
$posA = intval($itemA['position']);
62+
$posB = intval($itemB['position']);
63+
64+
return $posA <=> $posB;
65+
});
66+
6667
return $sorterItems;
6768
}
6869
}

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)