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',
)) [] []
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".