Are you want to add the new filter option "Date" in product list page
?
step 1 : Create di.xml file at below path,
path : Vendor/module/etc
add the below code in di.xml file
<?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="catalog_config_plugin" type="Vendor\Module\Plugin\Model\Config"/>
</type>
<type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
<plugin name="catalog_productlist_toolbar_plugin" type="Vendor\Module\Plugin\Product\ProductList\Toolbar"/>
</type>
<type name="Magento\Catalog\Model\Config\Source\ListSort">
<plugin name="Trootech_Image::ListSort" type="Vendor\Module\Plugin\Model\Config\Source\ListSort" />
</type>
</config>
Step 2: Create new Toolbar.php class below path,
path : Vendor\Module\Plugin\Product\ProductList
add the below code in Toolbar.php,
<?php
namespace Vendor\Module\Plugin\Product\ProductList;
class Toolbar
{
public function afterSetCollection(
\Magento\Catalog\Block\Product\ProductList\Toolbar $subject,
$result
)
{
$currentOrder = $subject->getCurrentOrder();
$dir = $subject->getCurrentDirection();
$currentDirection = $subject->getCurrentDirection();
if ($currentOrder == "date") {
if ($currentDirection == 'asc') {
$subject->getCollection()->setOrder('created_at', 'desc');
}
else {
$subject->getCollection()->setOrder('created_at', 'asc');
}
}
return $result;
}
}
Step 3: Create new ListSort.php class below path,
path : Vendor\Module\Plugin\Model\Config\Source
add the below code in ListSort.php,
<?php
namespace Vendor\Module\Plugin\Model\Config\Source;
class ListSort
{
public function afterToOptionArray(\Magento\Catalog\Model\Config\Source\ListSort $subject, array $result): array
{
\array_push($result, ['label' => __('Date'), 'value' => 'date']);
return $result;
}
}
step 4 : Create new Config.php class at below path,
path : Vendor\Module\Plugin\Model,
add the below code in Config.php,
<?php
namespace Vendor\Module\Plugin\Model;
class Config
{
public function afterGetAttributeUsedForSortByArray(\Magento\Catalog\Model\Config $catalogConfig, $options)
{
$date = ['date' => __('Date')];
$options = array_merge($options, $date);
return $options;
}
}