Skip to content

Commit 455030a

Browse files
committed
Merge remote-tracking branch 'origin/imported-magento-magento2-31629' into 2.4-develop-pr120
2 parents a78bb39 + 9ef6460 commit 455030a

File tree

4 files changed

+176
-55
lines changed

4 files changed

+176
-55
lines changed

app/code/Magento/Catalog/Model/ProductRepository.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
use Magento\Framework\Api\ImageContentValidatorInterface;
1818
use Magento\Framework\Api\ImageProcessorInterface;
1919
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;
20+
use Magento\Framework\Api\SearchCriteriaInterface;
2021
use Magento\Framework\DB\Adapter\ConnectionException;
2122
use Magento\Framework\DB\Adapter\DeadlockException;
2223
use Magento\Framework\DB\Adapter\LockWaitException;
@@ -619,7 +620,7 @@ public function deleteById($sku)
619620
/**
620621
* @inheritdoc
621622
*/
622-
public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCriteria)
623+
public function getList(SearchCriteriaInterface $searchCriteria)
623624
{
624625
/** @var \Magento\Catalog\Model\ResourceModel\Product\Collection $collection */
625626
$collection = $this->collectionFactory->create();
@@ -628,6 +629,7 @@ public function getList(\Magento\Framework\Api\SearchCriteriaInterface $searchCr
628629
$collection->addAttributeToSelect('*');
629630
$collection->joinAttribute('status', 'catalog_product/status', 'entity_id', null, 'inner');
630631
$collection->joinAttribute('visibility', 'catalog_product/visibility', 'entity_id', null, 'inner');
632+
$this->joinPositionField($collection, $searchCriteria);
631633

632634
$this->collectionProcessor->process($searchCriteria, $collection);
633635

@@ -856,4 +858,36 @@ private function saveProduct($product): void
856858
);
857859
}
858860
}
861+
862+
/**
863+
* Join category position field to make sorting by position possible.
864+
*
865+
* @param Collection $collection
866+
* @param SearchCriteriaInterface $searchCriteria
867+
* @return void
868+
*/
869+
private function joinPositionField(
870+
Collection $collection,
871+
SearchCriteriaInterface $searchCriteria
872+
): void {
873+
$categoryIds = [[]];
874+
foreach ($searchCriteria->getFilterGroups() as $filterGroup) {
875+
foreach ($filterGroup->getFilters() as $filter) {
876+
if ($filter->getField() === 'category_id') {
877+
$categoryIds[] = explode(',', $filter->getValue());
878+
}
879+
}
880+
}
881+
$categoryIds = array_unique(array_merge(...$categoryIds));
882+
if (count($categoryIds) === 1) {
883+
$collection->joinField(
884+
'position',
885+
'catalog_category_product',
886+
'position',
887+
'product_id=entity_id',
888+
['category_id' => current($categoryIds)],
889+
'left'
890+
);
891+
}
892+
}
859893
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -852,6 +852,9 @@ public function testDeleteById()
852852
public function testGetList()
853853
{
854854
$searchCriteriaMock = $this->getMockForAbstractClass(SearchCriteriaInterface::class);
855+
$searchCriteriaMock->expects($this->once())
856+
->method('getFilterGroups')
857+
->willReturn([]);
855858
$collectionMock = $this->createMock(Collection::class);
856859
$this->collectionFactory->expects($this->once())->method('create')->willReturn($collectionMock);
857860
$this->product->method('getSku')->willReturn('simple');

0 commit comments

Comments
 (0)