You are here:  » View Category Hierarchy text and link


View Category Hierarchy text and link

Submitted by smartprice24 on Sat, 2017-05-06 13:44 in

Hi David.

I have updated to 16/10A and use the hierarchy categories. How can display the product category in text and url format, in serchresults, products, feautured product, etc, etc,.

Many thanks for support!

Submitted by support on Mon, 2017-05-08 08:08

Hi,

First add the following code to the end of includes/tapestry.php just before the closing PHP tag to add a new function to obtain the path and name for a category by ID:

  function tapestry_getCategoryInfo($categoryid)
  {
    global $config_databaseTablePrefix;
    global $tapestry_categoryInfo;
    if (!isset($tapestry_categoryInfo[$categoryid]))
    {
      $categories = array();
      $id = $categoryid;
      do {
        $sql = "SELECT name,parent FROM `".$config_databaseTablePrefix."categories_hierarchy` WHERE id = '".$id."'";
        database_querySelect($sql,$rows);
        array_unshift($categories,$rows[0]["name"]);
      } while($id = $rows[0]["parent"]);
      $path = implode("/",$categories);
      $name = array_pop($categories);
      $tapestry_categoryInfo[$categoryid] = array("path"=>$path,"name"=>$name);
    }
    return $tapestry_categoryInfo[$categoryid];
  }

And then wherever you have a `categoryid` value in context, you can display the category name and link to category results as follows, for example in html/product.php:

<?php
  
if ($product_main["categoryid"])
  {
    
$categoryInfo tapestry_getCategoryInfo($product_main["categoryid"]);
    print 
"<a href='".tapestry_indexHREF("category",$categoryInfo["path"])."'>".$categoryInfo["name"]."</a>";
  }
?>

(if you wanted to show the full path instead of just the sub-category name you can always display $categoryInfo["path"] instead of $categoryInfo["name"])

Elsewhere, for example within the loop for featured products (html/featured.php), search results / related products (html/searchresults.php) or each merchant in the price comparison table (html/prices.php) you would use $product in place of $product_main in the above code however, bear in mind that the `categoryid` field is not included by default in the related products re-query so this needs to be added by editing products.php and look for the following code at line 130:

      $sql2 = "SELECT id,name,normalised_name,image_url,description,price,rating,MIN(price) AS minPrice, COUNT(id) AS numMerchants FROM `".$config_databaseTablePrefix."products` WHERE name IN (".$in.") GROUP BY name ORDER BY NULL";

...and REPLACE with:

      $sql2 = "SELECT *,MIN(price) AS minPrice, COUNT(id) AS numMerchants FROM `".$config_databaseTablePrefix."products` WHERE name IN (".$in.") GROUP BY name ORDER BY NULL";

Hope this helps!

Cheers,
David.
--
PriceTapestry.com

Submitted by smartprice24 on Mon, 2017-05-08 08:19

Perfect solution!
As always.
Thanks very much.

Giuseppe