Modify product.module to support taxonomy terms

I wanted to use the default layout of the product module to display store items, but I also wanted it to support taxonomy terms, so that I could easily group my items.

I basically modified the theme_product_view_collection() function to grab term arguments and then added some complexity to the sql. I'm not totally sure that I got the sql right, but it's functional for me.

This setup allows the calls for all products:

http://yoursite.com/product

and then by various terms:

http://yoursite.com/product/term/1

http://yoursite.com/product/term/1,2,3

Here's the modified function:

function theme_product_view_collection() {

	// should these be variables?
	$columns  = 3;
	$rows     = 5;

	// find out which terms are being passed in
	$tids = array(arg(2));

	foreach ($tids as $index => $tid) {
      $term = taxonomy_get_term($tid);
      $tree = taxonomy_get_tree($term->vid, $tid, -1, $depth);
      $descendant_tids[] = array_merge(array($tid), array_map('_taxonomy_get_tid_from_term', $tree));
    }
    $str_tids = implode(',', call_user_func_array('array_merge', $descendant_tids));

	$query = 'SELECT DISTINCT(n.nid) FROM {node} n ';
	$query .= 'INNER JOIN {ec_product} p ON n.nid = p.nid ';

	// if tids are coming in, do this sql
	if ($str_tids) {
		$query .= 'INNER JOIN {term_node} tn ON n.nid = tn.nid ';
		$query .= 'WHERE tn.tid IN (' . $str_tids . ') AND n.status = 1 ORDER BY n.sticky DESC, n.created DESC';
		}
	else {
		$query .= 'WHERE n.status = 1 ORDER BY n.sticky DESC, n.created DESC';
		}

	//fetch the products
  $result = pager_query(db_rewrite_sql($query), $rows * $columns, 0);


  $output = '';
  for ($i = 0; $node = db_fetch_object($result); $i++) {

    if ($i % $columns == 0) {
      $output .= '';
    }

    $node = node_load(array('nid' => $node->nid));
    $teaser = true;
    $page   = false;

    $node->body = str_replace('', '', $node->body);
    if (node_hook($node, 'view')) {
      node_invoke($node, 'view', $teaser, $page);
    }
    else {
      $node = node_prepare($node, $teaser);
    }
    node_invoke_nodeapi($node, 'view', $teaser, $page);

    $output .= '\n";

    if ($i % $columns == $columns - 1) {
      $output .= "\n";
    }
  }

  if ($i % $columns != 0) {
    $output .= "\n";
  }
  $output .= '

'. l($node->title, "node/$node->nid") ."

$node->teaser
'; if ($pager = theme('pager', NULL, $rows * $columns, 0)) { $output .= $pager; } return $output; }

What this probably needs is:

1) sql clean up.
2) output of term titles
3) review of how i'm formating urls and letting me know if i'm doing this right

Download patch here

Post new comment

The content of this field is kept private and will not be shown publicly.
  • You can use Markdown syntax to format and style the text. Also see Markdown Extra for tables, footnotes, and more.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <blockquote> <pre>
  • Lines and paragraphs break automatically.

More information about formatting options