0

    use \Magento\CatalogSearch\Helper\Data as CatalogSearch;

    use \Magento\Catalog\Model\Layer\Category\FilterableAttributeList;

    use \Magento\Catalog\Api\ProductRepositoryInterface;

    use \Magento\Framework\Api\Search\SearchCriteriaBuilder;

    use \Magento\Framework\Api\FilterBuilder;

    use \Magento\Framework\Api\Search\FilterGroupBuilder;

    use Magento\Framework\Api\SortOrder;

    /*
     * Motor de busqueda por defecto (Magento)
     */

    class Magento extends Base

    {


    protected $_catalogSearchHelper;

    protected $_filterableAttributeList;

    protected $_productRepository;

    protected $_searchCriteriaBuilder;

    protected $_filterBuilder;

    protected $_sortOrderBuilder;

    protected $_filterGroupBuilder;


    /**
     * Magento constructor.
     * @param CatalogSearch $catalogSearchHelper
     * @param FilterableAttributeList $filterableAttributeList
     * @param ProductRepositoryInterface $productRepository
     * @param SearchCriteriaBuilder $searchCriteriaBuilder
     * @param FilterBuilder $filterBuilder
     * @param FilterGroupBuilder $filterGroupBuilder
     */
    public function __construct(CatalogSearch $catalogSearchHelper, FilterableAttributeList $filterableAttributeList, ProductRepositoryInterface $productRepository, SearchCriteriaBuilder $searchCriteriaBuilder, FilterBuilder $filterBuilder, FilterGroupBuilder $filterGroupBuilder){

        $this->_catalogSearchHelper = $catalogSearchHelper;
        $this->_filterableAttributeList = $filterableAttributeList;
        $this->_productRepository = $productRepository;
        $this->_searchCriteriaBuilder = $searchCriteriaBuilder;
        $this->_filterBuilder = $filterBuilder;
        $this->_filterGroupBuilder = $filterGroupBuilder;

    }

    /**
     * Devuelve el nombre del motor de busqueda
     * @return string
     */
    public function getEngineName()
    {
        return "Magento";
    }

    /**
     * Devuelve si es valido el criterio de busqueda
     * @param $searchTerm
     * @param $storeId
     * @return bool
     */
    public function isValidSearchData($searchTerm, $storeId)
    {

        if (is_null($searchTerm) || strlen($searchTerm) == 0) {
            return false;
        }
        $helper = $this->_catalogSearchHelper;
        $len = strlen($searchTerm);
        if ($len getMinQueryLength() || $len > $helper->getMaxQueryLength()) {
            return false;
        }
        return true;

    }

    /**
     * @param $storeId
     * @param $searchTerm
     * @param $searchOrder
     * @param $searchDir
     * @param $page
     * @param $pageSize
     * @param $totalSearchResults
     * @param $attrs_applied
     * @param $attrs_available
     * @return array
     */
    public function search($storeId, $searchTerm, $searchOrder, $searchDir, $page, $pageSize, &$totalSearchResults, &$attrs_applied, &$attrs_available)
    {

        $result = array();


        $filter1 = $this->_filterBuilder->setField('name')
            ->setValue('%' . $searchTerm . '%')
            ->setConditionType("like")
            ->create();

        $filter2 = $this->_filterBuilder->setField('description')
            ->setValue('%' . $searchTerm . '%')
            ->setConditionType("like")
            ->create();


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


        $searchDirection = ($searchDir == "DESC") ? SortOrder::SORT_DESC : SortOrder::SORT_ASC;
        //$this->_searchCriteriaBuilder->addFilter($filter1);
        $this->_searchCriteriaBuilder->addSortOrder($searchOrder, $searchDirection);
        $searchCriteria = $this->_searchCriteriaBuilder->setFilterGroups([$filterGroup])->setCurrentPage($page)->setPageSize($pageSize)->create();
        //$searchCriteria->setFilterGroups([$filterGroup]);
        $products = $this->_productRepository->getList($searchCriteria);
        $totalSearchResults = $products->getTotalCount();
        $products = $products->getItems();


        //$searchCriteria->setFilterGroups([$filterGroup]);


        //ids encontrados
        foreach ($products as $product) {
            $result[] = $product->getId();
        }


        return $result;
    }

}

Fatal error: Uncaught Error: Call to undefined method Magento\Framework\Api\Search\SearchCriteriaBuilder::setFilterGroups()

2 Answers 2

1

For OR condition check following code.

DI


public function __construct(
    \Magento\Catalog\Api\ProductRepositoryInterface $productRepository,
    \Magento\Framework\Api\SearchCriteriaBuilder $searchCriteriaBuilder,
    \Magento\Framework\Api\FilterBuilder $filterBuilder,
    \Magento\Framework\Api\Search\FilterGroupBuilder $filterGroupBuilder,
    ..........
) {
    $this->productRepository = $productRepository;
    $this->searchCriteriaBuilder = $searchCriteriaBuilder;
    $this->filterBuilder = $filterBuilder;
    $this->filterGroupBuilder = $filterGroupBuilder;
    ...........
}

And now inside your method:


$filter1 = $this->filterBuilder->setField('name')
    ->setValue('%'.$searchTerm.'%')
    ->setConditionType("like")
    ->create();

$filter2 = $this->filterBuilder->setField('description')
    ->setValue('%'.$searchTerm.'%')
    ->setConditionType("like")
    ->create();

$filterGroup = $this->filterGroupBuilder->addFilter($filter1)
    ->addFilter($filter2)
    ->create();
$searchCriteria = $this->searchCriteriaBuilder->setFilterGroups([$filterGroup])->create();

$products = $this->productRepository->getList($searchCriteria);
$totalSearchResults = $products->getTotalCount();
$products = $products->getItems();

More detail

8
  • Not work: throw Call to undefined method Magento\Framework\Api\Search\SearchCriteriaBuilder::setFilterGroups()
    – Miguel
    Commented Jun 29, 2017 at 15:50
  • Which version you tried? It's good with 2.1
    – Sohel Rana
    Commented Jun 29, 2017 at 15:53
  • i am using magento 2.1.7 and my class SearchCriteriaBuilder NOT has that method
    – Miguel
    Commented Jun 29, 2017 at 16:02
  • It's works with 2.1.7. Can you check first section of code? ' __construct'?
    – Sohel Rana
    Commented Jun 29, 2017 at 16:09
1

I had to use

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

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.