Skip to content

Commit a29b455

Browse files
🔃 [EngCom] Public Pull Requests - 2.3-develop
Accepted Public Pull Requests: - #13468: [2.3] Fix Sort by Product Name (by @ihor-sviziev) - #13437: [2.3] Add fallback for product links position attribute (by @ihor-sviziev) - #13351: [2.3] Update the Emogrifier dependency to ^2.0.0 (by @oliverklee)
2 parents 9df49a6 + a4fc618 commit a29b455

File tree

6 files changed

+190
-137
lines changed

6 files changed

+190
-137
lines changed

app/code/Magento/Catalog/Model/Product/Link/SaveHandler.php

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class SaveHandler
3131
private $linkResource;
3232

3333
/**
34+
* SaveHandler constructor.
3435
* @param MetadataPool $metadataPool
3536
* @param Link $linkResource
3637
* @param ProductLinkRepositoryInterface $productLinkRepository
@@ -55,17 +56,50 @@ public function execute($entityType, $entity)
5556
{
5657
$link = $entity->getData($this->metadataPool->getMetadata($entityType)->getLinkField());
5758
if ($this->linkResource->hasProductLinks($link)) {
58-
/** @var \Magento\Catalog\Api\Data\ProductInterface $entity*/
59+
/** @var \Magento\Catalog\Api\Data\ProductInterface $entity */
5960
foreach ($this->productLinkRepository->getList($entity) as $link) {
6061
$this->productLinkRepository->delete($link);
6162
}
6263
}
63-
$productLinks = $entity->getProductLinks();
64+
65+
// Build links per type
66+
$linksByType = [];
67+
foreach ($entity->getProductLinks() as $link) {
68+
$linksByType[$link->getLinkType()][] = $link;
69+
}
70+
71+
// Set array position as a fallback position if necessary
72+
foreach ($linksByType as $linkType => $links) {
73+
if (!$this->hasPosition($links)) {
74+
array_walk($linksByType[$linkType], function ($productLink, $position) {
75+
$productLink->setPosition(++$position);
76+
});
77+
}
78+
}
79+
80+
// Flatten multi-dimensional linksByType in ProductLinks
81+
$productLinks = array_reduce($linksByType, 'array_merge', []);
82+
6483
if (count($productLinks) > 0) {
6584
foreach ($entity->getProductLinks() as $link) {
6685
$this->productLinkRepository->save($link);
6786
}
6887
}
6988
return $entity;
7089
}
90+
91+
/**
92+
* Check if at least one link without position
93+
* @param array $links
94+
* @return bool
95+
*/
96+
private function hasPosition(array $links)
97+
{
98+
foreach ($links as $link) {
99+
if (!array_key_exists('position', $link->getData())) {
100+
return false;
101+
}
102+
}
103+
return true;
104+
}
71105
}

app/code/Magento/CatalogSearch/Model/ResourceModel/Fulltext/Collection.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,15 +366,21 @@ protected function _renderFiltersBefore()
366366
'search_result.'. TemporaryStorage::FIELD_SCORE . ' ' . $this->relevanceOrderDirection
367367
);
368368
}
369+
return parent::_renderFiltersBefore();
370+
}
369371

372+
/**
373+
* @inheritdoc
374+
*/
375+
protected function _beforeLoad()
376+
{
370377
/*
371378
* This order is required to force search results be the same
372379
* for the same requests and products with the same relevance
373380
* NOTE: this does not replace existing orders but ADDs one more
374381
*/
375382
$this->setOrder('entity_id');
376-
377-
return parent::_renderFiltersBefore();
383+
return parent::_beforeLoad();
378384
}
379385

380386
/**

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@
4141
"magento/zendframework1": "~1.13.0",
4242
"monolog/monolog": "^1.17",
4343
"oyejorge/less.php": "~1.7.0",
44-
"pelago/emogrifier": "1.2.0",
44+
"pelago/emogrifier": "^2.0.0",
4545
"phpseclib/phpseclib": "2.0.*",
4646
"ramsey/uuid": "3.6.1",
4747
"sjparkinson/static-review": "~4.1",

0 commit comments

Comments
 (0)