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>';
}