回答:
created_atadmin-> stores->(attribute)製品にないような属性を使用する場合、adminで定義された属性には設定Sorting in Product Listing = Yes/Noがあるため、次の2つのファイルを操作する必要があります。
\vendor\magento\module-catalog\Block\Product\ProductList\Toolbar.php\vendor\magento\module-catalog\Model\Config.php
でToolbar.php見ることができます
$this->_availableOrder = $this->_catalogConfig->getAttributeUsedForSortByArray();それgetAttributeUsedForSortByArray()から呼び出してConfig.php、リストのコレクションをソートするために利用可能な属性の配列を返します。
ここで、created_atここに属性を追加する必要があります。どうやって?プラグインでやった
/**
 * Add sort order option created_at to frontend
 */
public function afterGetAttributeUsedForSortByArray(
    \Magento\Catalog\Model\Config $catalogConfig,
    $options
) {
    $options['created_at'] = __('New');
    return $options;
}created_atソートするために使用可能な属性を挿入しました。これで、使用するカスタムコレクションを作成するだけです。ここで私は私のものでオーバーライド \vendor\magento\module-catalog\Block\Product\ProductList\Toolbar.phpしてオーバーライドすることを選択しToolbar.phpますsetCollection()
/**
 * Set collection to pager
 *
 * @param \Magento\Framework\Data\Collection $collection
 * @return $this
 */
 public function setCollection($collection) {
    $this->_collection = $collection;
    $this->_collection->setCurPage($this->getCurrentPage());
    // we need to set pagination only if passed value integer and more that 0
    $limit = (int)$this->getLimit();
    if ($limit) {
        $this->_collection->setPageSize($limit);
    }
    // switch between sort order options
    if ($this->getCurrentOrder()) {
        // create custom query for created_at option
        switch ($this->getCurrentOrder()) {
            case 'created_at':
                if ($this->getCurrentDirection() == 'desc') {
                    $this->_collection
                        ->getSelect()
                        ->order('e.created_at DESC');
                } elseif ($this->getCurrentDirection() == 'asc') {
                    $this->_collection
                        ->getSelect()
                        ->order('e.created_at ASC');           
                }
                break;
            default:
                $this->_collection->setOrder($this->getCurrentOrder(), $this->getCurrentDirection());
                break;
        }
    }
    // echo '<pre>';
    // var_dump($this->getCurrentOrder());
    // var_dump((string) $this->_collection->getSelect());
    // die;
    return $this;        
}それはすべて、私にとって魅力のように動作します。
$block->addOrderToAvailableOrders('created_at', 'New')は、ソーターテンプレートで組み込みのパブリック関数を使用することもできます。
                    created_atし、カスタム価格属性コードで属性コードを変更することができます
                    プラグインを使用して実現できます。モジュールに次のファイルを作成してください。
app / code / Package / CustomToolbar / etc / di.xml
<type name="Magento\Catalog\Model\Config">
    <plugin name="Package_CustomToolbar::addCustomOptions" type="Package\CustomToolbar\Plugin\Model\Config" />
</type>
<type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
    <plugin name="Package_CustomToolbar::addPriceDecendingFilterInToolbar" type="Package\CustomToolbar\Plugin\Product\ProductList\Toolbar" />
</type>app / code / Package / CustomToolbar / Plugin / Model / Config.php
namespace Package\CustomToolbar\Plugin\Model;
use Magento\Store\Model\StoreManagerInterface;
class Config
{
    protected $_storeManager;
public function __construct(
    StoreManagerInterface $storeManager
) {
    $this->_storeManager = $storeManager;
}
/**
 * Adding custom options and changing labels
 *
 * @param \Magento\Catalog\Model\Config $catalogConfig
 * @param [] $options
 * @return []
 */
public function afterGetAttributeUsedForSortByArray(\Magento\Catalog\Model\Config $catalogConfig, $options)
{
    $store = $this->_storeManager->getStore();
    $currencySymbol = $store->getCurrentCurrency()->getCurrencySymbol();
    //Remove specific default sorting options
    unset($options['position']);
    unset($options['name']);
    unset($options['price']);
    //Changing label
    $customOption['position'] = __('Relevance');
    //New sorting options
    $customOption['price_desc'] = __($currencySymbol.' (High to Low)');
    $customOption['price_asc'] = __($currencySymbol.' (Low to High)');
    //Merge default sorting options with custom options
    $options = array_merge($customOption, $options);
    return $options;
}
}app / code / Package / CustomToolbar / Plugin / Product / ProductList / Toolbar.php
namespace Package\CustomToolbar\Plugin\Product\ProductList;
class Toolbar
{
    /**
     * Plugin
     *
     * @param \Magento\Catalog\Block\Product\ProductList\Toolbar $subject
     * @param \Closure $proceed
     * @param \Magento\Framework\Data\Collection $collection
     * @return \Magento\Catalog\Block\Product\ProductList\Toolbar
     */
    public function aroundSetCollection(
        \Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
        \Closure $proceed,
        $collection
    ) {
        $currentOrder = $subject->getCurrentOrder();
        $result = $proceed($collection);
        if ($currentOrder) {
            if ($currentOrder == 'price_desc') {
                $subject->getCollection()->setOrder('price', 'desc');
            } elseif ($currentOrder == 'price_asc') {
                $subject->getCollection()->setOrder('price', 'asc');
            }
        }
        return $result;
    }
}これは、Magentoクラスを書き換えることなく正常に機能しています。
Create At属性のみを使用する場合は、管理パネルの並べ替えオプションでこの属性を有効にできます。
例:
<?php
namespace Vendor\Module\Setup;
use Magento\Eav\Setup\EavSetup;
use Magento\Eav\Setup\EavSetupFactory;
use Magento\Framework\Setup\ModuleContextInterface;
use Magento\Framework\Setup\ModuleDataSetupInterface;
use Magento\Framework\Setup\UpgradeDataInterface;
class UpgradeData implements UpgradeDataInterface
{
    protected $eavSetupFactory;
    /**
     * UpgradeData constructor.
     *
     * @param EavSetupFactory $eavSetupFactory
     */
    public function __construct(
        EavSetupFactory $eavSetupFactory
    ) {
        $this->eavSetupFactory = $eavSetupFactory;
    }
    /**
     * @param ModuleDataSetupInterface $setup
     * @param ModuleContextInterface $context
     */
    public function upgrade(
        ModuleDataSetupInterface $setup,
        ModuleContextInterface $context
    ) {
        /** @var EavSetup $eavSetup */
        $eavSetup = $this->eavSetupFactory->create(['setup' => $setup]);
        if (version_compare($context->getVersion(), '2.1.1', '<')) {
            try {
                $entityType = $eavSetup->getEntityTypeId('catalog_product');
                $label = 'Created At';
                $eavSetup->updateAttribute($entityType, 'created_at', 'frontend_label', $label, null);
                $eavSetup->updateAttribute($entityType, 'created_at', 'used_for_sort_by', 1, null);
            } catch (LocalizedException $e) {
            }
        }
    }
}Setup / UpgradeData.phpからのこのコードですが、代わりにInstallData.phpを使用する方が良いでしょう。
ステップ1:最初にregistration.phpを作成する必要があります
ベンダー名:Arun
モジュール名:NewSorting
ベンダー/モジュール名/registration.php
<?php \Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE, 'Arun_NewSorting',
__DIR__
);?>ステップ2:module.xmlを作成します
ベンダー/モジュール名/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
    <module name="Arun_NewSorting" setup_version="0.0.1">
        <sequence>
            <module name="Magento_Catalog"/>
        </sequence>
    </module>
</config>ステップ3:プラグインを作成します
ベンダー/モジュール名/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
    <type name="Magento\Catalog\Model\Config">
        <plugin name="Arun_NewSorting::addCustomOptions" type="Arun\NewSorting\Plugin\Model\Config" />
    </type>
    <type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
        <plugin name="Arun_NewSorting::addPriceDecendingFilterInToolbar" type="Arun\NewSorting\Plugin\Product\ProductList\Toolbar" />
    </type>
</config>ステップ4:次にconfig.phpを作成します
ベンダー/モジュール名/プラグイン/モデル/Config.php
<?php
namespace Arun\NewSorting\Plugin\Model;
use Magento\Store\Model\StoreManagerInterface;
class Config  {
    protected $_storeManager;
    public function __construct(
        StoreManagerInterface $storeManager
    ) {
        $this->_storeManager = $storeManager;
    }
    public function afterGetAttributeUsedForSortByArray(\Magento\Catalog\Model\Config $catalogConfig, $options)
    {
        $store = $this->_storeManager->getStore();
        $currencySymbol = $store->getCurrentCurrency()->getCurrencySymbol();
        // Remove specific default sorting options
        $default_options = [];
        $default_options['name'] = $options['name'];
        unset($options['position']);
        unset($options['name']);
        unset($options['price']);
        //Changing label
        $customOption['position'] = __( 'Relevance' );
        //New sorting options
        $customOption['created_at'] = __( ' New' );
        $customOption['name'] = $default_options['name'];
        //Merge default sorting options with custom options
        $options = array_merge($customOption, $options);
        return $options;
    }
}ステップ5:Toolbar.php ***をオーバーライドする
ベンダー/モジュール名/プラグイン/製品/製品リスト/Toolbar.php
<?php
namespace Arun\NewSorting\Plugin\Product\ProductList;
class Toolbar
{
    public function aroundSetCollection(
        \Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
        \Closure $proceed,
        $collection
    ) {
        $currentOrder = $subject->getCurrentOrder();
        $result = $proceed($collection);
        if ($currentOrder) {
            if ($currentOrder == 'created_at') {
                $subject->getCollection()->setOrder('created_at', 'desc');
            } 
        }
        return $result;
    }
}それは完璧に動作します
created_atDBテーブルeav_attributeで製品属性を見つけ、その列frontend_labelをCreated At(デフォルトはnull)に設定します。
created_atDBテーブルcatalog_eav_attributeで製品属性を見つけ、その列used_for_sort_byを1(デフォルトは0)に設定します。
サイトキャッシュを消去し、機能しています。
# Get the attribute_id of 'created_at'
select attribute_id from eav_attribute where attribute_code = 'created_at' and entity_type_id=4;
# Set frontend_label
update eav_attribute set frontend_label = 'Created At' where attribute_id=112;
# Set used_for_sort_by
update catalog_eav_attribute set used_for_sort_by = 1 where attribute_id=112;
} elseif ( $this->getCurrentDirection() == 'asc' ) {し} else {ます。