0

I am working on to get product from custom attributes using search criteria. i need to assign multiple filter to single filter group.

what i did:

$cus Query:

{"area":["320","321","322"],"city":"342","size":""} 

My code to get a product:

           foreach($cusQuery as $key => $value)
            {

      // create multiple filter for single attribute based on  multiselect(advanced search):

                $filter= '';

                echo $key ."\n";
                if($key =='city' || 'area')
                {
                    if(is_array($value))
                    {
                        foreach($value as $siglevalue)
                        {

                            $filter[$filter_loop] = $this->_filterBuilder->create()->setField($key)->setConditionType('finset')->setValue($siglevalue);
                            $filter_loop++;
                        }
                    }else{
                        if(strlen($value) != 0) {
                            $filter[$filter_loop] = $this->_filterBuilder->create()->setField($key)->setConditionType('finset')->setValue($value);
                        }
                    }

//create filter Group:

                   if(!empty($filter))
                   {
                       foreach($filter as $fil)
                       {

                           $filter_group[$filter_group_loop] = $this->_filterGroupBuilder->addFilter($fil)->create();
                           $filter_group_loop++;
                       }
                   }

                }
                $filter_loop++;
            }

            $search_criteria = $this->_searchCriteriaBuilder->create()->setFilterGroups($filter_group);
            $productdata = $this->_productModelRepository->getList($search_criteria)->getItems();  
            echo "Product Count" .count($productdata);

I know following way to assign multi-filter to single filter group:

 $filter_group = $this->_filterGroupBuilder
            ->addFilter($filter[0])
            ->addFilter($filter[1])
            ->addFilter($filter[2])
            ->addFilter($filter[4])
            ->create();

But, I have filters in array so, i can't choose these way.

Suggest me, How to assign multiple filter in single filter group for my way or any other way to do this.

0

Yes, I find out the \Magento\Framework\Api\Search\FilterGroupBuilder have setFilters function to give array of multiple filters.

so, i change my code

foreach($filter as $fil)
     {
           $this->_logger->addDebug("array filter group  loop" ."\n");
           $filter_group[$filter_group_loop] = $this->_filterGroupBuilder->addFilter($fil)->create();    //->addFilter($fil)->create();
           $filter_group_loop++;
       }

to :

$filter_group[$filter_group_loop] = $this->_filterGroupBuilder->setFilters($filter)->create();    //->addFilter($fil)->create();

Now, create Multiple Filters to Single Filter Group.

Your Answer

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

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