You are here:  » Products that included and to other categories above search results


Products that included and to other categories above search results

Submitted by stevebi on Wed, 2015-04-01 05:53 in

Hello David,

Look what I was thinking since we all map our categories thanx to Category Hierarchy and category reverse Mapping

If it is possible at position user_searchresults_before.php to retrieve the following IF it is TRUE

Lets say someone searches for “Nike Running” products.

1) To receive at searchresults the products of the category with the biggest number of specific product

2) At user_searchresults_before.php to receive the other categories where you can find this product
“Found products also in categories:
Athletic kid shoes (280) athletic shorts (5) Athletic shocks (17)

Best Regards

Steve

Submitted by support on Wed, 2015-04-01 08:08

Hello Steve,

For sure - this is a popular feature of many ecommerce sites. Here is the code to add to html/user_searchresults_before.php to display "Top categories for your search":

<?php
  
print "<div class='row'>";
  print 
"<div class='small-12 columns'>";
  if (
$parts[0]!="category")
  {
    if (isset(
$config_useCategoryHierarchy) && $config_useCategoryHierarchy)
    {
      
$sql "SELECT categoryid,COUNT(id) as numResults FROM `".$config_databaseTablePrefix."products` WHERE ".$where.$priceWhere." AND categoryid <> '0' GROUP BY categoryid ORDER BY numResults DESC LIMIT 3";
      if (
database_querySelect($sql,$rows))
      {
        
$ids = array();
        foreach(
$rows as $row)
        {
          
$ids[] = $row["categoryid"];
        }
        
$categoryHierarchy tapestry_categoryHierarchyArray($ids);
        print 
"<p><strong>Top categories for your search:</strong></p>";
        print 
"<ul>";
        foreach(
$rows as $row)
        {
          
$href tapestry_indexHREF("category",$categoryHierarchy[$row["categoryid"]]);
          print 
"<li><a href='".$href."'>".str_replace("/"," / ",$categoryHierarchy[$row["categoryid"]])."</a></li>";
        }
        print 
"</ul>";
      }
    }
    else
    {
      
$sql "SELECT category,COUNT(id) as numResults FROM `".$config_databaseTablePrefix."products` WHERE ".$where.$priceWhere." AND category <> '' GROUP BY category ORDER BY numResults DESC LIMIT 3";
      if (
database_querySelect($sql,$rows))
      {
        print 
"<p><strong>Top categories for your search:</strong></p>";
        print 
"<ul>";
        foreach(
$rows as $row)
        {
          
$href tapestry_indexHREF("category",$row["category"]);
          print 
"<li><a href='".$href."'>".$row["category"]."</a></li>";
        }
        print 
"</ul>";
      }
    }
  }
  print 
"</div>";
  print 
"</div>";
?>

Above includes support for both normal category and category hierarchy.

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by stevebi on Wed, 2015-04-01 08:39

Hi

I have applied the code but Received the following errors and could not test it.
Just to point if you can apply the explode -1 filter so that the last subcategory to appear

Top categories for your search:
Notice: Undefined offset: 0 in /home/ulike/public_html/html/user_searchresults_before.php on line 16
Notice: Undefined offset: 0 in /home/ulike/public_html/html/user_searchresults_before.php on line 22

Cheers

S

PS if you prefer reply at email

Submitted by support on Wed, 2015-04-01 08:56

Sorry Steve, I forgot to exclude null values - corrected above.

Cheers,
David.
--
PriceTapestry.com