0

I have created one custom product attribute(bestseller). Now I want to sort by custom product attribute(bestseller) using search criteria builder sort by option but it's not working for custom product attribute. FYI Sort by option working with price attribute but not working with custom product attribute option. Any help appreciate...

//Code for fetch products based on search term.
$limit = $this->scopeConfig->getValue('catalog/frontend/list_per_page');    
$p = $p?$p:1;
$pageSize = $limit?$limit:20;            
$this->_storeManager->setCurrentStore($storeid);
$search_criteria = $this->searchCriteriaBuilder->create();
$search_criteria->setRequestName("quick_search_container");
$search_criteria->setCurrentPage($p);
$search_criteria->setPageSize($pageSize);
$filter = $this->filterBuilder->setField('search_term')
->setValue($q)
->setConditionType("like")
->create();               
$filterGroup = $this->filterGroupBuilder->addFilter($filter)->create();
$search_criteria->setFilterGroups([$filterGroup]);
$sortOrder = $this->sortOrderBuilder->setField('bestseller')->setDirection(SortOrder::SORT_DESC)->create();
$search_criteria->setSortOrders([$sortOrder]);
$search_result = $this->searchInterface->search($search_criteria);
$totalSearchResults = $search_result->getTotalCount();        
$products = $search_result->getItems();

1 Answer 1

0

Inject 3 classes

use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface;

check below code

   public function getList(SearchCriteriaInterface $searchCriteria)
    {
        /** @var \Custom\Module\Model\ResourceModel\CustomModel\CollectionFactory $collection */
        $collection = $this->CustomModelListCollectionFactory->create();
        foreach ($searchCriteria->getFilterGroups() as $filterGroup) {
            $fields = [];
            $conditions = [];
            foreach ($filterGroup->getFilters() as $filter) {
                if ($filter->getField() === 'store_id') {
                    $collection->addStoreFilter($filter->getValue(), false);
                    continue;
                }
                $fields[] = $filter->getField();
                $condition = $filter->getConditionType() ?: 'eq';
                $conditions[] = [$condition => $filter->getValue()];
            }
            $collection->addFieldToFilter($fields, $conditions);
        }

        $sortOrders = $searchCriteria->getSortOrders();
        if ($sortOrders) {
            /** @var SortOrder $sortOrder */
            foreach ($sortOrders as $sortOrder) {
                $collection->addOrder(
                    $sortOrder->getField(),
                    ($sortOrder->getDirection() === SortOrder::SORT_ASC) ? 'ASC' : 'DESC'
                );
            }
        }
        $collection->setCurPage($searchCriteria->getCurrentPage());
        $collection->setPageSize($searchCriteria->getPageSize());
        $this->collectionProcessor->process($searchCriteria, $collection);

        /** @var \Custom\Module\Api\Data\CustomModelSearchResultsInterfaceFactory $searchResults */
        $searchResults = $this->searchResultsFactory->create();
        $searchResults->setSearchCriteria($searchCriteria);
        $searchResults->setTotalCount($collection->getSize());
        $searchResults->setItems($collection->getItems());

        return $searchResults;
    }

below sort condition

$sortOrders = $searchCriteria->getSortOrders();
        if ($sortOrders) {
            /** @var SortOrder $sortOrder */
            foreach ($sortOrders as $sortOrder) {
                $collection->addOrder(
                    $sortOrder->getField(),
                    ($sortOrder->getDirection() === SortOrder::SORT_ASC) ? 'ASC' : 'DESC'
                );
            }
        }
5
  • Thanks for your help. can you share the dependency classes for this code? Commented Dec 19, 2021 at 18:04
  • Can you guide with my code where I need to change ? Commented Dec 20, 2021 at 9:41
  • check updated answer
    – Devidas
    Commented Dec 20, 2021 at 10:07
  • Any dependency for this line collectionProcessor ? $this->collectionProcessor->process($searchCriteria, $collection); Commented Dec 20, 2021 at 12:38
  • yes Magento\Framework\Api\SearchCriteria\CollectionProcessorInterface; this is the dependency
    – Devidas
    Commented Dec 21, 2021 at 5:26

Your Answer

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

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