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="MagentoCatalogModelConfig">
<plugin name="catalog_config_plugin" type="SunarcNearByProductPluginConfig" />
</type>`
<preference for="MagentoCatalogBlockProductProductListToolbar" type="SunarcNearByProductBlockCatalogProductProductListToolbar" />
<type name="MagentoElasticsearchModelResourceModelFulltextCollectionSearchCriteriaResolver">
<plugin name="ajourquin_unset_es_order" type="SunarcNearByProductPluginElasticsearchModelResourceModelFulltextCollectionSearchCriteriaResolver" />
</type>
appcodeSunarcNearByProductPluginconfig.php
public function afterGetAttributeUsedForSortByArray(MagentoCatalogModelConfig $catalogConfig,$results)
{
$results['distance'] = __('Distance');
return $results;
}
appcodeSunarcNearByProductPluginElasticsearchModelResourceModelFulltextCollectionSearchCriteriaResolver.php
public function afterResolve(MagentoSearchCriteriaResolver $subject, SearchCriteria $result): SearchCriteria
{
$sortOrders = $result->getSortOrders();
unset($sortOrders['distance']);
$result->setSortOrders($sortOrders);
return $result;
}
appcodeSunarcNearByProductBlockCatalogProductProductListToolbar.php
$ids = array(4,5,2,1,3);
$ids = implode(',',$ids);
$this->_collection->getSelect()->reset(MagentoFrameworkDBSelect::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.