0
$this->_collection->addAttributeToSort('created_at', 'DESC'); // not working
$this->_collection->setOrder('created_at', 'DESC'); // not working
//$this->_collection->setOrder('position', 'DESC');// this is working

I'm using class Toolbar extends \Magento\Catalog\Block\Product\ProductList\Toolbar Any idea how can I do similarly?

2

2 Answers 2

0

The code you are using should work to sort a collection by the product created_at attribute

->addAttributeToSort('created_at', 'desc')

However I am not sure if extending Toolbar.php will let you change the current product listing sort order.

If you want to add created_at as a sort option or make it the default sorting option for product listing pages follow the instructions here to enable sorting for this attribute.

https://magento.stackexchange.com/a/287793/7863

You can then set this as the sorting option per category, or as the default sorting option for all product lists in admin Stores -> Configuration -> Catalog -> Catalog -> Storefront.

enter image description here

0

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;
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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