2

I have created some custom attribute like city and I need to search how many products I have in a specific city using SearchCriteria.

The SearchCriteria I apply does not filter products on my city attribute and I always get all the products.

This is my code for getting a specific city Products:

   $objectManager =   \Magento\Framework\App\ObjectManager::getInstance();
    $filter_1 = $objectManager
        ->create('Magento\Framework\Api\FilterBuilder')
        ->setField('city')
        ->setConditionType('like')
        ->setValue("Chennai%")
        ->create();
    $search_criteria =$objectManager
        ->create('Magento\Framework\Api\SearchCriteriaBuilder')
        ->setFilterGroups([$filter_group])
        ->create();

    $repo =$objectManager->get('Magento\Catalog\Model\ProductRepository');
    $result = $repo->getList($search_criteria);
    $products = $result->getItems();
    echo count($products);

I can get all the products using the below filter:

   $filter_1 = $objectManager
    ->create('Magento\Framework\Api\FilterBuilder')
    ->setField('city')
    ->setConditionType('like')
    ->setValue("%")
    ->create();

Can you suggest me how to get products filtering on a specific attribute?

1
  • Where does $filter_group come from?
    – Pedru
    Aug 9, 2017 at 8:45

0

Your Answer

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