You are here:  » Filter Categories to Show Category Hierarchy Name Instead of Datafeed Category Name


Filter Categories to Show Category Hierarchy Name Instead of Datafeed Category Name

Submitted by bat on Mon, 2016-10-24 09:31 in

Hi David,
In 15/09, I've solely been using Catgeory Hierarchy to sort out my products into categories. On the front end, I've noticed that the Filter dropdown for categories is then showing the original feed category name for the products instead of the Hierarchy category it belongs to.
How would I get the filter to show the name of the hierarchy category the products belong to instead?

Thanks

Submitted by support on Mon, 2016-10-24 10:19

Hi,

I know you're using a custom sidebar filters file from an earlier distribution so this just needs to be changed to use the Category Hierarchy. To do this, in your sidebar.php file, where you have the following code:

  $sql1 = "SELECT DISTINCT(category) FROM `".$config_databaseTablePrefix."products` WHERE ".$where." AND category <> '' ORDER BY category";
  if (database_querySelect($sql1,$rows1))
  {
    print "<div class='sidebartitle'><label form='cf'><strong>BY CATEGORY:</strong></label></div>";
    print "<div class='sidebarboxcontentcategory'>";
    foreach($rows1 as $row)
    {
      $checkboxId++;
      $checked = (in_array($row["category"],$categoryFilter)?"checked='checked'":"");
      print "<input id='cb".$checkboxId."' onchange='JavaScript:document.getElementById(\"refine\").submit();' type='checkbox' name='categoryFilter[]' value='".htmlentities($row["category"],ENT_QUOTES,$config_charset)."' ".$checked." /> <label for='cb".$checkboxId."'>".$row["category"]."</label><br />";
    }
    print "</div><br /><br />";
  }

...REPLACE with:

  $sql1 = "SELECT DISTINCT(categoryid) FROM `".$config_databaseTablePrefix."products` WHERE ".$where." AND categoryid > 0";
  if (database_querySelect($sql1,$rows1))
  {
    $categoryids = array();
    foreach($rows1 as $row)
    {
      $categoryids[] = $row["categoryid"];
    }
    $categories = tapestry_categoryHierarchyArray($categoryids);
    if ((count($categoryids) > 1) || count($categoryFilter))
    {
      print "<div class='sidebartitle'><label form='cf'><strong>BY CATEGORY:</strong></label></div>";
      print "<div class='sidebarboxcontentcategory'>";
      foreach($categories as $id => $path)
      {
        $checkboxId++;
        $checked = (in_array($id,$categoryFilter)?"checked='checked'":"");
        print "<input id='cb".$checkboxId."' onchange='JavaScript:document.getElementById(\"refine\").submit();' type='checkbox' name='categoryFilter[]' value='".$id."' ".$checked." /> <label for='cb".$checkboxId."'>".$path."</label><br />";
      }
      print "</div><br /><br />";
    }
  }

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by bat on Fri, 2017-02-10 14:46

This is great, thanks David.
Is there anyway to show just the final category the products sit in rather than the entire heirarchy path?

Also, can that final category also be used in other places where I usually use

<?php
 
print $product_main["category"]; 
?>
?

Submitted by support on Fri, 2017-02-10 15:28

Hello bat,

Sure - for the filters case, where you have this code following the replacement;

  print "<input id='cb".$checkboxId."' onchange='JavaScript:document.getElementById(\"refine\").submit();' type='checkbox' name='categoryFilter[]' value='".$id."' ".$checked." /> <label for='cb".$checkboxId."'>".$path."</label><br />";

...REPLACE with;

  $p = explode("/",$path);
  $path = array_pop($p);
  print "<input id='cb".$checkboxId."' onchange='JavaScript:document.getElementById(\"refine\").submit();' type='checkbox' name='categoryFilter[]' value='".$id."' ".$checked." /> <label for='cb".$checkboxId."'>".$path."</label><br />";

For use in html/product.php where you would have previously used;

<?php
 
print $product_main["category"];
?>

...use:

<?php
  $categories 
tapestry_categoryHierarchyArray(array($product_main["categoryid"]));
  
$p explode("/",$categories[$product_main["categoryid"]]);
  
$path array_pop($p);
  print 
$path;
?>

Or if you want to make it a link to the category results use;

<?php
  $categories 
tapestry_categoryHierarchyArray(array($product_main["categoryid"]));
  
$p explode("/",$categories[$product_main["categoryid"]]);
  
$path array_pop($p);
  print 
"<a href='".tapestry_indexHREF("category",$categories[$product_main["categoryid"]])."'>".$path."</a>";
?>

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by bat on Sun, 2017-02-12 19:17

Thanks David, this works a treat.
Where would I put the code for the dropdown filter which I use when viewed on mobiles?
Assuming it's html/searchfilters.php but can't pinpoint where to put

$p = explode("/",$path);
$path = array_pop($p);

Submitted by support on Mon, 2017-02-13 10:17

Hi,

Look for this line in the Category filter code;

  print "<option value='".$id."' ".$selected.">".$path."</option>";

...and REPLACE with:

  $p = explode("/",$path);
  $path = array_pop($p);
  print "<option value='".$id."' ".$selected.">".$path."</option>";

Cheers,
David.
--
PriceTapestry.com

Submitted by bat on Tue, 2017-03-07 22:55

If I wanted to include this final child category in the meta title/description, how would that be presented please?
In search.php, I have

if (isset($parts[1]))
    {
      switch($parts[0])
      {
        case "merchant":
          $header["title"] = "Buy products from ".$parts[1];
$header["meta"]["description"] = "Search results for ".$parts[1]." in the UK.";
          break;

but of course it's coming up with the entire path.
Thanks David

Submitted by support on Wed, 2017-03-08 08:54

Hi,

For the "category", case, have a go as follows:

        case "category":
          $cats = explode("/",$parts[1]);
          $lastcat = array_pop($cats);
          $header["title"] = "Buy products from ".$lastcat;
$header["meta"]["description"] = "Search results for ".$lastcat." in the UK.";
          break;

(change the surrounding text of course as required)

Cheers,
David.
--
PriceTapestry.com