diff --git a/config/rapidez/magento-defaults.php b/config/rapidez/magento-defaults.php index a04a6935e..ffd8e8650 100644 --- a/config/rapidez/magento-defaults.php +++ b/config/rapidez/magento-defaults.php @@ -1,9 +1,10 @@ 'position', 'catalog/frontend/flat_catalog_category' => '0', - 'catalog/frontend/grid_per_page_values' => '12,24,36', 'catalog/frontend/flat_catalog_product' => '0', + 'catalog/frontend/grid_per_page_values' => '12,24,36', 'catalog/frontend/grid_per_page' => '12', 'catalog/frontend/show_swatches_in_product_list' => '1', 'catalog/recently_products/viewed_count' => '5', diff --git a/src/Http/Controllers/ConfigController.php b/src/Http/Controllers/ConfigController.php index 4c1576541..a2eeffb09 100644 --- a/src/Http/Controllers/ConfigController.php +++ b/src/Http/Controllers/ConfigController.php @@ -250,21 +250,34 @@ public function getSearchkitSorting(): array "rapidez::frontend.sorting.{$attribute['code']}.{$direction}", trans_fallback("rapidez::frontend.{$attribute['code']}", ucfirst($attribute['code'])) . ' ' . trans_fallback("rapidez::frontend.{$direction}", $direction), ), + 'code' => $attribute['code'], 'field' => $attribute['code'] . (Attribute::hasKeyword($attribute['code']) ? '.keyword' : ''), 'order' => $direction, 'value' => "{$index}_{$attribute['code']}_{$direction}", 'key' => "_{$attribute['code']}_{$direction}", ])); - // Add default relevance sort + // Add relevance sort $sortableAttributes->prepend([ 'label' => __('Relevance'), + 'code' => 'position', 'field' => '_score', 'order' => 'desc', - 'value' => $index, - 'key' => 'default', + 'value' => "{$index}_score_desc", + 'key' => '_score_desc', ]); - return $sortableAttributes->keyBy('key')->toArray(); + // Set default sort by + $defaultSortBy = Rapidez::config('catalog/frontend/default_sort_by'); + $sortableAttributes = $sortableAttributes + ->sortBy(fn ($attribute) => $attribute['code'] == $defaultSortBy ? 0 : 1) + ->values() + ->toArray(); + + // Change value and key of default sort + $sortableAttributes[0]['value'] = $index; + $sortableAttributes[0]['key'] = 'default'; + + return Arr::keyBy($sortableAttributes, 'key'); } }