0

I'm trying to use a product repository to fetch a list of products. I want to fetch based on multiple filters (category_id, name, description and short description), combined with an OR criteria, but things don't seem to be working.

the idea is to search for a keyword in all product's names, descriptions and short descriptions also in categories names here is my code

        $categoriesCollection = $this->categoryFactory->create();
        $categoriesCollection->addAttributeToFilter('name', array(array('like' => '%' . $term . '%')));
        $categoriesIds = $categoriesCollection->exportToArray();

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

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

        $filter3 = $this->filterBuilder->setField('short_description')
            ->setValue("%$term%")
            ->setConditionType('like')
            ->create();

        $filter4 = $this->filterBuilder->setField('category_id')
            ->setValue(array_keys($categoriesIds))
            ->setConditionType('in')
            ->create();

        $filter_group1 = $this->filterGroupBuilder
            ->addFilter($filter1)
            ->addFilter($filter2)
            ->addFilter($filter3)
            ->addFilter($filter4)
            ->create();

        $search_criteria = $this->searchCriteriaBuilder->setFilterGroups([$filter_group1])
            ->setPageSize($pageSize)->setCurrentPage($currentPage)->create();


        $list = $this->getList($search_criteria);

the filter works fine when I remove the others filters except category, but doesn't work when all filters are combined as above.

Is there a problem in my code? and how can I achieve this?

4
  • try with the field name category_ids instead of category_id
    – Marius
    Jan 18, 2022 at 15:53
  • I got this error Column not found: 1054 Unknown column 'e.category_ids' in 'where clause'
    – mshifo
    Jan 18, 2022 at 16:00
  • sorry then. I just took a shot in the dark
    – Marius
    Jan 18, 2022 at 16:01
  • filter by category_id works fine when i remove the other filters but not when combined
    – mshifo
    Jan 18, 2022 at 16:01

0

Your Answer

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