0

When i search "cocacola" product it search 1 product which is "cocacola" but when i search "cola" then no product found. i want search "cola" and it should a product but right now no product found. i want use %like% condition. I Want to add this functionality in header Search.

View Example

In Magento 1 Search work like https://app.hyfy.io/v/abfCoSnOT5n/
In Mahento 2 Search work like https://app.hyfy.io/v/ab3EtzbNT5n/

So Magento 2 Search Not Working Like Magento 1.

2
  • Hi Sanket, thank you for posting. Adding examples of what you tried so far, will help a lot. You are currently asking for a whole feature which seldom yields answers
    – Sander Mangel
    Mar 8 '18 at 9:45
  • In magento 1 there are functionality for search in admin like , combine and like combine but there are no more option for magento 2 so i can not use like condition for searching product Mar 8 '18 at 9:54
1

source : https://alanstorm.com/magento_2_understanding_object_repositories/

I think you are looking for something like this :

Verbose way :

//create our filter
$filter = $this->objectManager->create('Magento\Framework\Api\Filter');
$filter->setData('field','name');
$filter->setData('value','%coca%');
$filter->setData('condition_type','like');

//add our filter(s) to a group
$filter_group = $this->objectManager->create('Magento\Framework\Api\Search\FilterGroup');
$filter_group->setData('filters', [$filter]);

//add the group(s) to the search criteria object
$search_criteria = $this->objectManager->create('Magento\Framework\Api\SearchCriteriaInterface');
$search_criteria->setFilterGroups([$filter_group]);

//query the repository for the object(s)
$repo = $this->objectManager->get('Magento\Catalog\Model\ProductRepository');                        
$result = $repo->getList($search_criteria);
$products = $result->getItems();
foreach($products as $product)
{
    echo $product->getSku(),"\n";
}

Less verbose :

//create our filter
$filter = $this->objectManager
->create('Magento\Framework\Api\FilterBuilder')
->setField('name')
->setConditionType('like')
->setValue('%coca%')
->create();

//add our filter(s) to a group
$filter_group = $this->objectManager
->create('Magento\Framework\Api\Search\FilterGroupBuilder')
->addFilter($filter)
->create();
// $filter_group->setData('filters', [$filter]);

//add the group(s) to the search criteria object
$search_criteria = $this->objectManager
->create('Magento\Framework\Api\SearchCriteriaBuilder')
->setFilterGroups([$filter_group])
->create();

You can find all the condition type here : vendor/magento/framework/Api/CriteriaInterface.php

2
  • Thanks for Answer but i am not getting it. Can you explain in detail that in which file and which function i need to add code? Also i am using magento version 2.0.16. Also there is not any code in "vendor/magento/framework/Api/CriteriaInterface.php" file Mar 8 '18 at 9:19
  • Well give me more explanation of what you need because you ask me to do a full module without giving me any information.. for the version 2.0.16 i think you don't have the repository so you need to use the factory. About the questions where to put the code : In any php function of your module. Just inject the criteria or factory module and its over. Going to give you an example later this week end if i have time Mar 9 '18 at 9:40

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.