3

I want to retrieve the search term in my Observer for use elsewhere but I cant seem to find out how to extract the data.

So far I have tried this (search term is Football):

events.xml:

<event name='abstract_search_result_load_before'>
        <observer
           name='Path\To\This\Search.php'
           instance='Path\To\This\Search.php'
        />
</event>

Search Observer:

public function execute(\Magento\Framework\Event\Observer $observer) {
    $this->myhelper ->log('search');

    $event = $observer->getEvent();
    $collection = $event->getData('collection');
    $search = $collection->getSearchCriteria();

    $this->myhelper->log('************' . var_export($search, true));
}

Produces:

main.INFO: search [] []
main.INFO: ************Magento\CatalogInventory\Model\ResourceModel\Stock\Status\StockStatusCriteria::__set_state(array(
   'data' => 
  array (
    'fields' => 
    array (
      'list' => 
      array (
      ),
    ),
    'filters' => 
    array (
      'list' => 
      array (
      ),
    ),
    'orders' => 
    array (
      'list' => 
      array (
      ),
    ),
    'criteria_list' => 
    array (
      'list' => 
      array (
      ),
    ),
    'initial_condition' => true,
    'products_filter' => '7',
    'website_filter' => '1',
  ),
   'mapperInterfaceName' => 'Magento\\CatalogInventory\\Model\\ResourceModel\\Stock\\Status\\StockStatusCriteriaMapper',
)) [] []
2
  • 1
    Do you realize that this is a Magento\CatalogInventory\Model\ResourceModel\Stock\Status\StockStatusCriteria? Magento 2 uses SearchCriteria objects in many places, not just the catalog search. So in this case, there is no "search term". Apr 11, 2016 at 19:23
  • Right got you! Do you know of a way to get the search term? It must be retrievable surely? Apr 11, 2016 at 19:27

2 Answers 2

4

As stated in the comments, Magento 2 uses SearchCriteria objects in many places, not just the catalog search. So in this case, there is no "search term".

Which event you should use depends on what you actually want to do. But on a search request you should be able to retrieve the current query like this at any time:

public function __construct(\Magento\Search\Model\QueryFactory $queryFactory)
{
    $this->queryFactory = $queryFactory;
}

public function execute(\Magento\Framework\Event\Observer $observer)
{
    $searchTerm = $this->queryFactory->get()->getQueryText();
}

Because the search result controller uses a shared instance of Magento\Search\Model\Query which is managed by the QueryFactory instance.

9
  • Thank you seems like it will work, however I am listening to events which triggers specific Observers any idea which event is based to hook into, im trying different ones but sometimes its firing 10+ times on an add to basket event. Apr 11, 2016 at 20:04
  • Maybe controller_action_postdispatch_catalogsearch_result_index works for you? It's called after the search request has been processed. Apr 11, 2016 at 20:28
  • The event seems to respond but its always returning null even with a search term being used. Dont really understand why trying to look through magento source to figure it out! Apr 11, 2016 at 21:21
  • I tested it. Looks like you have to inject QueryFactory instead of Query and then call $this->queryFactory->get()->getQueryText() because the shared instance is managed by this factory, not the object manager itself. This is the clean way. But if you are using the postdispatch event, you could also use $event->getRequest()->getParam('q'). Apr 15, 2016 at 12:02
  • @ChrisEdwards I updated the answer accordingly. Apr 15, 2016 at 12:03
0

Is this what you are looking for?

Mage::helper('catalogSearch')->getQuery()->getQueryText();
1
  • That is for magento 1, I need it for magento2 :/ Apr 11, 2016 at 19:21

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.

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