Jump to content

echo Search Details When a User's Search is Caught Using Hooks


Recommended Posts

Hello  :)

I'm coding a test module targeted at retail stores. In my module, when a user queries through the search bar I need to echo the following

  • Search term
  • Search results (first 100 products semicolon separated)

I've already coded the methods for those and set them to be fired by an actionSearch hook but the results are not displayed.

I have found that action hooks cannot deal with php echos. But I still couldn't find a proper way to do it. 

I'll post all the relevant code here. 

Any help is much much appreciated.

P. S. : I can't deal with templates since the module will be targeted towards various customer sites.

public function hookactionSearch($params)
{
	$this->displaySearchDetails();
}

// Module install method
public function install()
{
  if (Shop::isFeatureActive())
    Shop::setContext(Shop::CONTEXT_ALL);

  if (!parent::install() ||
    !$this->registerHook('actionSearch')
  )
    return false;

  return true;
}

// Display search details
private function displaySearchDetails()
{
	$searchTerm = $this->getSearchTerm();
	if (empty($searchTerm)) {
		return;
	}
	echo '<b>Search Details : </b><br>';
	$this->displaySearchTerm($searchTerm);
	$this->displaySearchResultsProdIds($searchTerm);
}

// Get the search term
private function getSearchTerm()
{
	$searchTerm = Tools::getValue(self::SEARCH_REQ_PARAM_KEY);
	$trimmedSearchTerm = trim($searchTerm);
	return $trimmedSearchTerm;
}

// Display search term
private function displaySearchTerm($searchTerm)
{
	echo 'Search Term : ' . $searchTerm . '<br>';
}

// Display search results first 100 products semicolon separated product ids
private function displaySearchResultsProdIds($searchTerm)
{
	$searchLimitOffset = 1;
	//Set search results limit to the catalog size
	$searchLimit = count(Product::getProducts($this->context->language->id, 1, PHP_INT_MAX, 'name', 'ASC'));
	$resultsArr = Search::find($this->context->language->id, $searchTerm, $searchLimitOffset, $searchLimit);
	$searchResultsArr = $resultsArr[self::SEARCH_RESULTS_ARR_KEY];
	$prodIdArr = array();
	$resultsCount = count($searchResultsArr);
	for ($i=0; ($i<$resultsCount && $i<100); $i++) {
		$singleSearchResultArr = $searchResultsArr[$i];
		if (isset($singleSearchResultArr[self::PROD_ID_PARAM_KEY]))
		{
			array_push($prodIdArr, $singleSearchResultArr[self::PROD_ID_PARAM_KEY]);
		}
	}

	$semiColonSeparatedProdIds = implode(self::SEMICOLON_STR, $prodIdArr);

	echo 'Search Results Product Ids : ' . $semiColonSeparatedProdIds . '<br>';
}
Link to comment
Share on other sites

First, what version of Prestashop are we discussing?  It's possible the solution is different.

 

The actionSearch hook seems pretty useless.  It is called after the search results have been obtained, but the hook is not provided with the actual results of the search.  The hook also does not accept any data in return, so other than knowing that the search occurred and what the query was, there does not seem to be a way to alter the results or alter what is displayed.

 

Perhaps there is a different hook that would allow you to manipulate the results.

 

Now you said you want to echo data, however I assume you mean you want to include data in the search results that the end user would see?

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...