浏览文章
文章信息
Magento2 添加最新产品排序到后台,兼容Elsaticsearch | Magento2 add sorting to admin category panel
12414
下面是如何将已有的排序方式添加到后台,可实现分类页面设置默认排序和可用排序的过程。
提示:Magento2.4.*使用elasticsearch的,created_at排序需要更新created_at为可排序属性。
1、di.xml
<preference for="Magento\Catalog\Model\Config\Source\ListSort" type="Aiweline\SparshAdvancedSorting\Model\Config\Source\ListSource"/> <preference for="Magento\Catalog\Model\Category\Attribute\Source\Sortby" type="Aiweline\SparshAdvancedSorting\Model\Category\Attribute\Source\Sortby"/>2、Aiweline\SparshAdvancedSorting\Model\Category\Attribute\Source\Sortby.php
<?php namespace Aiweline\SparshAdvancedSorting\Model\Category\Attribute\Source; class Sortby extends \Magento\Catalog\Model\Category\Attribute\Source\Sortby { /** * @inheritDoc */ public function getAllOptions() { $options[] = ['label' => __('New Arrival'), 'value' => 'created_at']; foreach ($this->_getCatalogConfig()->getAttributeUsedForSortByArray() as $code => $label) { $options[] = ['label' => __($label), 'value' => $code]; } return $options; } }3、Aiweline\SparshAdvancedSorting\Model\Config\Source\ListSource.php
<?php namespace Aiweline\SparshAdvancedSorting\Model\Config\Source; class ListSource extends \Magento\Catalog\Model\Config\Source\ListSort { /** * @inheritDoc */ public function toOptionArray() { $options[] = ['label' => __('New Arrival'), 'value' => 'created_at']; foreach ($this->_getCatalogConfig()->getAttributeUsedForSortByArray() as $code => $label) { $options[] = ['label' => __($label), 'value' => $code]; } return $options; } }