0

I need to search product programatically using search_criteria

public function search($storeId, $searchTerm, $searchOrder, $searchDir, $page, $pageSize, &$totalSearchResults, &$attrs_applied, &$attrs_available)
{

 $search_criteria = $this->_searchCriteriaBuilder->create();

 $search_criteria->setRequestName("quick_search_container");


$firstCharacter = substr($searchTerm, 0, 1);


if($firstCharacter == ":") {


   $filter = $this->_filterBuilder->setField('sku')
        ->setValue(substr($searchTerm, 1))
        ->setConditionType("sku")
        ->create();


} else {

   $filter = $this->_filterBuilder->setField('search_term')
        ->setValue($searchTerm)
        ->setConditionType("like")
        ->create();
}

    $filterGroup = $this->_filterGroupBuilder->addFilter($filter)->create();

    $search_criteria->setFilterGroups([$filterGroup]);

    $resultx = $this->_SearchInterface->search($search_criteria);

    $totalSearchResults = $resultx->getTotalCount();

    $products = $resultx->getItems();

    return $products;

}

but no return results, and throw exception

Exception #0 (Magento\Framework\Search\Request\EmptyRequestDataException): The request query and filters aren't set. Verify the query and filters and try again.

any idea?

  • why you need $search_criteria->setRequestName("quick_search_container"); ? – Amit Bera Feb 18 at 10:23
  • Please provide full code – Amit Bera Feb 18 at 10:33
  • ok i added the full code, i want to search by search term o by sku, first letter dependent – Miguel Feb 18 at 11:14
  • I donot think you have provided full code and i need to know what is the type of _searchCriteriaBuilder – Amit Bera Feb 18 at 11:42
  • SearchCriteriaBuilder $searchCriteriaBuilder – Miguel Feb 18 at 12:04

Your Answer

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

Browse other questions tagged or ask your own question.