I have created a custom sorting option distance in magento 2.4.3-p1 and sort the collection with custom sorted entity ids
di.xml
`<type name="Magento\Catalog\Model\Config">
 <plugin name="catalog_config_plugin" type="Sunarc\NearByProduct\Plugin\Config" />
</type>`
<preference for="Magento\Catalog\Block\Product\ProductList\Toolbar" type="Sunarc\NearByProduct\Block\Catalog\Product\ProductList\Toolbar" />
<type name="Magento\Elasticsearch\Model\ResourceModel\Fulltext\Collection\SearchCriteriaResolver">
 <plugin name="ajourquin_unset_es_order" type="Sunarc\NearByProduct\Plugin\Elasticsearch\Model\ResourceModel\Fulltext\Collection\SearchCriteriaResolver" />
</type>
app\code\Sunarc\NearByProduct\Plugin\config.php
public function afterGetAttributeUsedForSortByArray(\Magento\Catalog\Model\Config $catalogConfig,$results)
{
 $results['distance'] = __('Distance');
 
 return $results;
}
app\code\Sunarc\NearByProduct\Plugin\Elasticsearch\Model\ResourceModel\Fulltext\Collection\SearchCriteriaResolver.php
public function afterResolve(MagentoSearchCriteriaResolver $subject, SearchCriteria $result): SearchCriteria
{
 $sortOrders = $result->getSortOrders();
 unset($sortOrders['distance']);
 $result->setSortOrders($sortOrders);
 return $result;
}
app\code\Sunarc\NearByProduct\Block\Catalog\Product\ProductListToolbar.php
$ids = array(4,5,2,1,3);
$ids = implode(',',$ids);
$this->_collection->getSelect()->reset(\Magento\Framework\DB\Select::ORDER);
$this->_collection->getSelect()->order(array(new \Zend_Db_Expr("FIELD(e.entity_id, " . $ids . ")")));
//$logger->info('collection'.json_encode($this->_collection->getData()));
 //$logger->info($this->_collection->getSelectSql());
 foreach($this->_collection as $data)
 {
 $logger->info('getData'.json_encode($data->getData()));
 }
Here before foreach collection is sort with entity id 4,5,2,1,3 properly, but when we in the foreach loop $data->getData() give the product in 1,2,3,4,5 entity_id order, and also print query show correct field order 4,5,2,1,3.Because of this products are not showing with sort order on storefront.
Any one tell me what is the issue or any other things which i have missed. Any help is appriciated.
- 
 Please tell me you solved this problem. I have exactly the same right now.Siegfried Schmitz– Siegfried Schmitz2024年03月25日 11:05:56 +00:00Commented Mar 25, 2024 at 11:05
1 Answer 1
If think you just need to save the sorted collection and iteration over that one.
$this->_collection->getSelect()->order(array(new \Zend_Db_Expr("FIELD(e.entity_id, " . $ids . ")")));
Replaced by
$sortedCollection = $this->_collection->getSelect()->order(array(new \Zend_Db_Expr("FIELD(e.entity_id, " . $ids . ")")));
Then for foreach on sortedCollection.
That being said, this is not how you should sort a collection in magento
This is the way
 $productCollection = $this->productCollectionFactory->create();
 $productCollection->addAttributeToSelect('*');
 $productCollection->addAttributeToSort('distance','ASC');
- 
 Thanks but distance is not a product attribute, it's only a option on storefront in sort by option dropdownDivya Rawat– Divya Rawat2022年08月12日 10:56:42 +00:00Commented Aug 12, 2022 at 10:56
- 
 I have only $ids = array(4,5,2,1,3);Divya Rawat– Divya Rawat2022年08月12日 11:05:52 +00:00Commented Aug 12, 2022 at 11:05
Explore related questions
See similar questions with these tags.