Skip to content

Sorting galery media by position #27593

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 16 additions & 5 deletions app/code/Magento/Catalog/Model/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -1033,7 +1033,7 @@ public function priceReindexCallback()
*/
public function eavReindexCallback()
{
if ($this->isObjectNew() || $this->isDataChanged($this)) {
if ($this->isObjectNew() || $this->isDataChanged()) {
$this->_productEavIndexerProcessor->reindexRow($this->getEntityId());
}
}
Expand Down Expand Up @@ -1168,8 +1168,8 @@ public function setTierPrices(array $tierPrices = null)
/**
* Get product tier price for the customer, based on qty of this product
*
* @param float $qty
* @return float|array
* @param float $qty
* @return float|array
*/
public function getTierPrice($qty = null)
{
Expand All @@ -1179,7 +1179,7 @@ public function getTierPrice($qty = null)
/**
* Get formatted by currency product price
*
* @return array|double
* @return array|float
* @since 102.0.6
*/
public function getFormattedPrice()
Expand Down Expand Up @@ -1547,7 +1547,18 @@ public function getMediaGalleryImages()
}
if (!$this->getData('media_gallery_images')->count() && is_array($this->getMediaGallery('images'))) {
$images = $this->getData('media_gallery_images');
foreach ($this->getMediaGallery('images') as $image) {
$mediaGallery = $this->getMediaGallery('images');
usort(
$mediaGallery,
function (array $media, array $compare): int {
$posA = (int)$media['position'];
$posB = (int)$compare['position'];

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

foreach ($mediaGallery as $image) {
if (!empty($image['disabled'])
|| !empty($image['removed'])
|| empty($image['value_id'])
Expand Down
46 changes: 26 additions & 20 deletions app/code/Magento/Catalog/Test/Unit/Model/ProductTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@
use PHPUnit\Framework\TestCase;

/**
* Unit test for Product class
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @SuppressWarnings(PHPMD.TooManyFields)
* @SuppressWarnings(PHPMD.ExcessivePublicCount)
Expand Down Expand Up @@ -313,10 +315,7 @@ protected function setUp(): void

$contextMock = $this->createPartialMock(
Context::class,
['getEventDispatcher', 'getCacheManager', 'getAppState', 'getActionValidator'],
[],
'',
false
['getEventDispatcher', 'getCacheManager', 'getAppState', 'getActionValidator']
);
$contextMock->expects($this->any())->method('getAppState')->willReturn($this->appStateMock);
$contextMock->expects($this->any())
Expand Down Expand Up @@ -619,7 +618,7 @@ public function testGetCategoryCollectionCollectionNull($initCategoryCollection,

$result = $product->getCategoryCollection();

$productIdCachedActual = $this->getPropertyValue($product, '_productIdCached', $productIdCached);
$productIdCachedActual = $this->getPropertyValue($product, '_productIdCached');
$this->assertEquals($getIdResult, $productIdCachedActual);
$this->assertEquals($initCategoryCollection, $result);
}
Expand Down Expand Up @@ -1056,6 +1055,8 @@ public function testGetIsSalableHasDataIsSaleable()

/**
* Configure environment for `testSave` and `testSaveAndDuplicate` methods
*
* @return void
*/
protected function configureSaveTest()
{
Expand Down Expand Up @@ -1330,22 +1331,25 @@ public function testGetMediaGalleryImagesMerging()
{
$mediaEntries =
[
'images' => [
[
'value_id' => 1,
'file' => 'imageFile.jpg',
'media_type' => 'image',
],
[
'value_id' => 3,
'file' => 'imageFile.jpg',
],
[
'value_id' => 2,
'file' => 'smallImageFile.jpg',
'media_type' => 'image',
],
'images' => [
[
'value_id' => 1,
'file' => 'imageFile.jpg',
'media_type' => 'image',
'position' => 1,
],
[
'value_id' => 3,
'file' => 'imageFile.jpg',
'position' => 2,
],
[
'value_id' => 2,
'file' => 'smallImageFile.jpg',
'media_type' => 'image',
'position' => 3,
]
]
];
$expectedImageDataObject = new DataObject(
[
Expand All @@ -1355,6 +1359,7 @@ public function testGetMediaGalleryImagesMerging()
'url' => 'http://magento.dev/pub/imageFile.jpg',
'id' => 1,
'path' => '/var/www/html/pub/imageFile.jpg',
'position' => 1,
]
);
$expectedSmallImageDataObject = new DataObject(
Expand All @@ -1365,6 +1370,7 @@ public function testGetMediaGalleryImagesMerging()
'url' => 'http://magento.dev/pub/smallImageFile.jpg',
'id' => 2,
'path' => '/var/www/html/pub/smallImageFile.jpg',
'position' => 3,
]
);

Expand Down