Skip to content

Commit e3276fc

Browse files
ISSUE-29690: Fixing issue when the images's order was using ID instead of position.
1 parent e9fa180 commit e3276fc

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

app/code/Magento/ConfigurableProduct/Block/Plugin/Product/Media/Gallery.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public function afterGetOptionsMediaGalleryDataJson(
5959
private function getProductGallery($product)
6060
{
6161
$result = [];
62-
$images = $product->getMediaGalleryImages();
62+
$images = $this->getImagesOrderedByPosition($product);
6363
foreach ($images as $image) {
6464
$result[] = [
6565
'mediaType' => $image->getMediaType(),
@@ -69,4 +69,18 @@ private function getProductGallery($product)
6969
}
7070
return $result;
7171
}
72+
73+
/**
74+
* @param Product $product
75+
* @return array
76+
*/
77+
private function getImagesOrderedByPosition($product)
78+
{
79+
$imagesCollection = $product->getMediaGalleryImages();
80+
$images = $imagesCollection->getItems();
81+
usort($images, function ($el1, $el2) {
82+
return $el1['position'] <=> $el2['position'];
83+
});
84+
return $images;
85+
}
7286
}

app/code/Magento/ConfigurableProduct/Test/Unit/Block/Plugin/Product/Media/GalleryTest.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Magento\ConfigurableProduct\Block\Plugin\Product\Media\Gallery;
1212
use Magento\ConfigurableProduct\Model\Product\Type\Configurable;
1313
use Magento\Framework\DataObject;
14+
use Magento\Framework\Data\Collection as DataCollection;
1415
use Magento\Framework\Serialize\Serializer\Json;
1516
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager;
1617
use PHPUnit\Framework\MockObject\MockObject;
@@ -41,13 +42,20 @@ public function testAfterGetOptions()
4142
['media_type' => 'type', 'video_url' => 'url', 'file' => 'image.jpg']
4243
);
4344

45+
$dataCollection = $this->getMockBuilder(DataCollection::class)
46+
->disableOriginalConstructor()
47+
->onlyMethods(['getItems'])
48+
->getMock();
49+
50+
4451
$galleryMock->expects(($this->any()))->method('getProduct')->willReturn($productMock);
4552
$productMock->expects($this->once())->method('getTypeId')->willReturn('configurable');
4653
$productMock->expects($this->once())->method('getTypeInstance')->willReturn($configurableTypeMock);
4754
$configurableTypeMock->expects($this->once())->method('getUsedProducts')->with($productMock)
4855
->willReturn([$variationProductMock]);
4956
$variationProductMock->expects($this->once())->method('getId')->willReturn($variationProductId);
50-
$variationProductMock->expects($this->once())->method('getMediaGalleryImages')->willReturn([$image]);
57+
$variationProductMock->expects($this->once())->method('getMediaGalleryImages')->willReturn($dataCollection);
58+
$dataCollection->expects($this->once())->method('getItems')->willReturn([$image]);
5159
$variationProductMock->expects($this->once())->method('getImage')->willReturn('image.jpg');
5260
$jsonMock->expects($this->once())->method('serialize')->with($expectedGalleryJson)
5361
->willReturn(json_encode($expectedGalleryJson));

0 commit comments

Comments
 (0)