I also tried making a custom option but i was unable to sort it according to my filters
here is my code
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">
<preference for="Magento\VisualMerchandiser\Model\Sorting" type="Vendor\Module\Model\Filters"/>
</config>
model/filters.php
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Vendor\Module\Model;
use Magento\VisualMerchandiser\Model\Sorting;
/**
* Sort the product collection
*
* @api
* @since 100.0.2
*/
class Filters extends Sorting
{
/**
* Get available sorting options
*
* @return array
*/
public function getSortingOptions(): array
{
$options = [];
foreach ($this->sortInstances as $idx => $instance) {
$options[$idx] = $instance->getLabel();
}
array_push($options, 'DateBottom', 'DateTop','SpecialPriceMiddle');
return $options;
}
}
Model/Sorting/DateBottom.php
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Vendor\Module\Model\Sorting;
use Magento\Framework\DB\Select;
use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\VisualMerchandiser\Model\Sorting\SortAbstract;
use Magento\VisualMerchandiser\Model\Sorting\SortInterface;
class DateBottom extends SortAbstract implements SortInterface
{
/**
* @param Collection $collection
* @return Collection
*/
public function sort(
Collection $collection
): Collection {
$this->addPriceData($collection);
$collection->getSelect()
->distinct('entity_id')
->reset(Select::ORDER)
->order('created_at ' . Collection::SORT_ORDER_DESC);
return $collection;
}
/**
* @return string
*/
public function getLabel(): string
{
return __("Date to Bottom");
}
}
here is one of three option i added in this sort option filter but its not sorting in this grid below picture nor frontend. any solutions?