Enhance your Magento 2 store by introducing a "Sort by Date" feature, enabling your customers to find the newest items with ease. Follow this quick guide to add this functionality to your store.
Directory Setup: Create app/code/Vendor/Module using your chosen vendor/module names.
module.xml: In app/code/Vendor/Module/etc/module.xml, add:
<?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="Vendor_Module" setup_version="1.0.0"/>
</config>
registration.php: In app/code/Vendor/Module/registration.php, add:
<?php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Vendor_Module',
__DIR__
);
Create Plugin: Add the following to app/code/Vendor/Module/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="vendor_module_sort" type="Vendor\Module\Plugin\Config" />
</type>
</config>
Plugin Class: In app/code/Vendor/Module/Plugin/Config.php, include:
<?php
namespace Vendor\Module\Plugin;
class Config
{
public function afterGetAttributeUsedForSortByArray($subject, $result)
{
$result['created_at'] = __('Date');
return $result;
}
}
Enable Module: Run these commands:
php bin/magento module:enable Vendor_Module
php bin/magento setup:upgrade
php bin/magento cache:clean
php bin/magento cache:flush
Verify: Check your store's frontend to confirm the "Sort by Date" option works.
If you're looking for more comprehensive sorting options and advanced features, consider exploring our Improved Sorting for Magento 2 extension. This solution offers enhanced flexibility and a robust set of features to further improve customer satisfaction and drive sales. So that you can create a more intuitive shopping experience tailored to your customers' needs.
Login and Registration Form