2

I would like to sort the display of a Magento 2.0.6 website products by date. I am looking forward to apply this sorting on the frontend.

I have been trying to find an extension so I can have this sorting option on the catalog setting but I am out of luck so far.

3

Instead of Extension You can add your own Sorting option

You can create your own module and then create the following plugins

Vendor/Modulename/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="Custom_NewSorting::addCustomOptions" type="Vendor\Modulename\Plugin\Model\Config" />
    </type>
    <type name="Magento\Catalog\Block\Product\ProductList\Toolbar">
        <plugin name="Custom_NewSorting::addPriceDecendingFilterInToolbar" type="Vendor\Modulename\Plugin\Product\ProductList\Toolbar" />
    </type>
</config>

And then write a plugin

Vendor/Modulename/Plugin/Model/Config.php

<?php
namespace Vendor\Modulename\Plugin\Model;

use Magento\Store\Model\StoreManagerInterface;

class Config  {

    public function afterGetAttributeUsedForSortByArray(\Magento\Catalog\Model\Config $catalogConfig, $options)
    {

       //New sorting options
        $customOption['created_at'] = __( ' Date' );


      //Merge default sorting options with custom options
        $options = array_merge($customOption, $options);

        return $options;
    }
}

Override the Toolbar.php***

Vendor/Modulename/Plugin/Product/ProductList/Toolbar.php

<?php
namespace Vendor\Modulename\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;
    }
}

Hope it works !! Happy Coding :)

0

Magento 2 CE doesn't have a sort option "Created At" by default. But you can write module which add label to this attribute. After that you will see this attribute in configuration for Catalog sorting.

Also you can use my modules for Catalog.

  • This is no answer – Rick Mar 28 at 13:54

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.