Page 1 of 1

Add hierarchy category to search results in Products Module

Posted: Thu Oct 16, 2008 11:08 pm
by belgirl
Hi--I want to be able to add the hierarchy category to the search results when searching for products in the Products Module.  Right now it lists the products, but you have no idea what category or hierarchy they are from.  Any help would be greatly appreciated!

Re: Add hierarchy category to search results in Products Module

Posted: Mon Oct 20, 2008 12:52 pm
by belgirl
Anyone?  I would love to use the search module with the products module, but its pretty useless if it doesn't show the category in the search results...:(

Re: Add hierarchy category to search results in Products Module

Posted: Fri Jan 15, 2010 11:08 pm
by jukums
Hi,

I know I'm replying to an old post, but its for you, someone like me, who is looking for answers :)
This is how I added Hierarchy items to the search,


My Configuration

Code: Select all

CMS Version:
1.6.6
Installed Modules:
Search 1.6.1
Products 2.4.6
Here are some guidelines of how to; in my case product names are used as unique numbers therefore searching by hierarchy is a must

1. Product.module.php
1.a -  after function GetProduct($id) add another function

Code: Select all

  function GetHierarchy( $id )
  {
	if( !isset($this->_hierarchy_cache[$id]) )
	  {
		$db =& $this->GetDb();
		$query = "SELECT * FROM ".cms_db_prefix()."module_products_hierarchy
                     WHERE id = ?";
		$row = $db->GetRow( $query, array( $id ) );
		if(!$row) return FALSE;
		$this->_hierarchy_cache[$id] = $row;
	  }
	return $this->_hierarchy_cache[$id];
  }
1b - after function GetSearchableText, add another function

Code: Select all

  function GetSearchableText2($hierarchy_id) {
	if( !isset( $this->_hierarchy_cache[$hierarchy_id] ) )
	  {
		$this->GetHierarchy( $hierarchy_id );
	  }

	$results = array();
	$hierarchy =& $this->_hierarchy_cache[$hierarchy_id];

	$results[] = $hierarchy['name'];
	$results[] = $hierarchy['description']; // delete this line not to index hiearchy description
	
	return $results;
  }

2. function.admin_tools.php
2.a Change function products_SearchReindex, add following lines

Code: Select all

  $query = 'SELECT id FROM '.cms_db_prefix().'module_products_hierarchy';
  $result = &$db->Execute($query);
  
  while ($result && !$result->EOF)
    {
      $data = $mod->GetSearchableText2($result->fields['id']);
      $module->AddWords($mod->GetName(), $result->fields['id'], 'hierarchy', 
			implode(' ', $data ) );
      $result->MoveNext();
    }
2.b Add following lines to function products_SearchResult,

Code: Select all

  if ($attr == 'hierarchy')
    {
      $db =& $mod->GetDb();
      $q = "SELECT name FROM ".cms_db_prefix()."module_products_hierarchy WHERE
			      id = ?";
      $dbresult = $db->Execute( $q, array( $productid ) );
      if ($dbresult)
	{
	  $row = $dbresult->FetchRow();
	  
	  //0 position is the prefix displayed in the list results.
	  $result[0] = $mod->GetFriendlyName();
	  
	  //1 position is the title
	  $result[1] = $row['name'];
	  
	  //2 position is the URL to the title.	  
	  $aliased_title = strtolower(munge_string_to_url($row['name']));
	  $prettyurl = 'products/hierarchy/' . $productid.'/'.$returnid."/$aliased_title";

	  $result[2] = $mod->CreateLink('cntnt01', 'details', $returnid, '', array('productid' => $productid) ,'', true, false, '', true,$prettyurl);
	}
Sure CreateLink not being correct, as using pretty urls.

As I'm using a script to import data from the outer database, therefore if you need to add search content, file you need to modify is action.admin_add_hierarchy_item.php

after lines

Code: Select all

	    $query = 'INSERT INTO '.cms_db_prefix().'module_products_hierarchy
                    (name, parent_id, description, image, extra1, extra2) VALUES(?,?,?,?,?,?)';
	    $dbr = $db->Execute($query,array($name,$parent,$description,$image,$extra1,$extra2));
	    if( !$dbr ) { echo $db->sql.'<br/>'; die( $db->ErrorMsg() ); }
	    $new_id = $db->Insert_ID();
	    $this->UpdateHierarchyPositions();
you should add following lines:

Code: Select all

	$module =& $this->GetModuleInstance('Search');
	if ($module != FALSE)
	  {
		$module->AddWords($this->GetName(), $new_id, 'hierarchy', 
				  implode(' ', $this->GetSearchableText2($new_id) ));
	  }
last bit is not tested, therefore might need to be changed.


Hope this helps anyone,
J