7 replies [Last post]
pjriot's picture
Joined: 12/14/2008
Juice: 41
Was this information Helpful?

Hi Folks,

I'm wondering if anyone can help me with a very broad problem description. Pointers on where to start would be much appreciated!

The problem:

First of all I'm helping my sister put up an online store for beauty products. I'm using Taxonomies to specify certain product properties such as Brand name and Complaint. (a complaint could be "Poor Skin" for a moisturiser product for example)

So, my catalog menu is working fine, but I'd really like to have "Shop By Brand" & "Shop By Usage" (complaint) menus.

The solutions I'm trying to get to grips with are:

1. When I click on a brand a db search would return a list of subcategories applicable to that brand from the catalog taxonomy. I have *no* idea how to make this work though.

2. Add sub terms to the Brands taxonomy for example. So that when I click on a brand name, the sub terms (equivalent to the catalog terms: Skincare, Face etc.) would be displayed. This way is probably easier, but I'd really like to keep the brands taxonomy for brands only.

Can anyone give me some general pointers on how to accomplish this? Should views help? So far in my tinkering with views I've had trouble getting anything useful from it. Is there a good tutorial about that could help?

Again, apologies for the breadth of the question. Any help at all would be much appreciated!

pjriot's picture
Joined: 12/14/2008
Juice: 41
Re: Menus based on Taxonomy terms

Ack, so for approach 2. it looks like I just needed a deeper understanding of the $term variable:

$vid = 2; /* <---- put correct vocabulary ID here */
$terms = taxonomy_get_tree($vid);
print "<ul>";
foreach ( $terms as $term ) {
    $tcount = taxonomy_term_count_nodes($term->tid);
    print "<li>".
    l($term->name,'taxonomy/term/'.$term->tid, array('title' => $tcount." products in ".$term->name)).
    print "</li>";
} /* end foreach */
print "</ul>";

It looks like I can play with that to create a menu based on the $term->depth

Is this the best way to accomplish what I want?

pjriot's picture
Joined: 12/14/2008
Juice: 41
Re: Re: Menus based on Taxonomy terms

Hmm, for the first one, I think what I'm asking should be achievable with views...

- Use the catalog as your base dataset
- Pass the brand name (taxonomy term) as an argument
- Filter the catalog by brand name

Does that make sense? Can views do that? I'm hitting the sack now, but I'll get stuck into this tomorrow.

I forgot to mention I'm working with UC2

denizengt@drupal.org's picture
Joined: 12/12/2008
Juice: 27
Re: Re: Re: Menus based on Taxonomy terms

Should be able to do that easily with views. I've done that before, outside an Ubercart context, but I don't see a difference.

pjriot's picture
Joined: 12/14/2008
Juice: 41
Re: Re: Re: Re: Menus based on Taxonomy terms

In case anyones interested I created a simple view called testview that took the Taxonomy: Term ID as an argument. This was for accessing a given term. The only solution I could find for generating the menu was to do it manually.

So in a nutshell, this code accepts a brand tid, searches for all products belonging to that brand (tid), finds out where the products sit in the catalog and generates a menu *specifically for that brand*.

I'm guessing there's a better way to do this but damned if I know...

function get_catalog_by_term($brand) {
  $terms = array(0 => $brand);
  $menu = array();
  foreach($terms as $term) {
  $result = db_query('SELECT t.nid FROM {term_node} t WHERE t.tid = %d', $term);
while ($node = db_fetch_array($result)) {
$nodes[] = $node['nid'];

$catresult = db_query('SELECT t.tid FROM {term_node} t WHERE t.nid = %d AND t.tid IN (SELECT t.tid FROM {term_node} t INNER JOIN {term_data} d on t.tid = d.tid WHERE d.vid = 1)', $node['nid']);
while ($cat = db_fetch_array($catresult)) {
$categories[] = $cat['tid'];
$parents = taxonomy_get_parents_all($cat['tid']);
$menu = parentArray($menu, $parents, $node['nid']);
  return $menu;

function parentArray($menu, $parents, $nid) {
$parentArr = $menu;
$tmp = &$parentArr;
foreach(array_reverse($parents) as $parent) {
if(!isset($tmp[$parent->name . '-' . $parent->tid])) {
$tmp[$parent->name . '-' . $parent->tid] = array();
$tmp = &$tmp[$parent->name . '-' . $parent->tid];
return $parentArr;

function printArrayList($array, $brand) {
foreach($array as $key => $value) {
if(is_array($array[$key])) {
preg_match("/[0-9]+\z/", $key, $groups);
$tid = $groups[0];
$category = substr($key, 0, strlen($key) - (strlen($tid)+1));
print '<ul class="brandmenu"><li><a href="/testview/' . $tid . ',' . $brand . '">' . $category . '</a></li>';
printArrayList($array[$key], $brand);
print '</ul>';

from my page.tpl.php I call them like so:

$arr = get_catalog_by_term('115');
printArrayList($arr, '115');

Just looking at this now, I'm not sure why I have a foreach loop in the first function, I'm sure there was a good reason but can't remember atm...

Anyway,this might come in useful for someone.

Jez's picture
Joined: 02/06/2009
Juice: 12
Re: Re: Re: Re: Re: Menus based on Taxonomy terms

Do you mean something like the menu at www.ladybird-toys.co.uk? For this site I used taxonomy menu to generate the menu, dhtml menu for the sliding effect and views to create the pages linked from the menu. The menu passes arguments vid and tid to views to generate the page.

Hope this helps


mac's picture
Joined: 02/04/2009
Juice: 13
Re: Re: Re: Re: Re: Re: Menus based on Taxonomy terms

I am not sure I got 100% what your need is. Am I right in saying that what you want to do is:

1. Selecting a set of nodes starting from the "producer" or the "problem"
2. Getting your menus organised by "problem" (if you selected them by "producer" or by "producer" (if you selected by "problem")


The easiest way that comes to my mind is to use taxonomy_block or taxonomy_menu (the latter will fail to honour your URL alias, though) to generate a menu based on either "producer" or "problem", and a views filter (of which you can tweak the UI if you like) that when clicked will display only the subset of nodes meeting your needs.

Of course there are another gazillion ways to get the same result, from re-generating the menu dinamically to create a "pivot table" merging both taxonomies and using the terms as tags.

pjriot's picture
Joined: 12/14/2008
Juice: 41
Re: Re: Re: Re: Re: Re: Re: Menus based on Taxonomy terms

Thanks for the replies folks. I'm concentrating on another part of the site at the moment but I'll revisit this to see if your suggestions result in a simpler implementation. So far I'm happy with the hacking I've done.