Page 1 of 1
Show all categories a product is in
Posted: Tue Apr 13, 2010 10:30 pm
by Chones
Hi,
I'm a bit of a newbie at PHP and OpenCart so thought I'd ask if anyone else had done this already before I attempt it myself.
I'm not using breadcrumbs and I have many products in more than one category. To aid navigation what I'd like to do is to show a list of all the categories a product belongs in.
I'm guessing an edit of the category module would be the best way to go about this, starting with getCategories($product_id), but that's about as far as I've got so far.
Any help would be most appreciated.
Re: Show all categories a product is in
Posted: Wed Apr 14, 2010 1:51 am
by Chones
Well, I had a stab at it, and it works, but it's not pretty. Am sure there must be a better way, but my programming skills aren't up to it just yet.
After connecting to the database I ran this:
Code: Select all
$get_cats_id = "SELECT category_id
FROM product_to_category
WHERE product_id = '".$product_id."'";
$get_cats_res = mysql_query($get_cats_id);
while ($cats = mysql_fetch_array($get_cats_res, MYSQL_ASSOC))
{
$cat_id = $cats['category_id'];
$get_cat_name = "SELECT name
FROM category_description
WHERE category_id = '".$cat_id."'";
$get_name_res = mysql_query($get_cat_name);
$name = mysql_fetch_array($get_name_res, MYSQL_ASSOC);
$get_cat_url = "SELECT keyword
FROM url_alias
WHERE query = 'category_id=".$cat_id."'";
$get_url_res = mysql_query($get_cat_url);
$url = mysql_fetch_array($get_url_res, MYSQL_ASSOC);
$get_par_id = "SELECT parent_id
FROM category
WHERE category_id = '".$cat_id."'";
$get_par_res = mysql_query($get_par_id);
$par = mysql_fetch_array($get_par_res, MYSQL_ASSOC);
$get_par_url = "SELECT keyword
FROM url_alias
WHERE query = 'category_id=".$par['parent_id']."'";
$get_purl_res = mysql_query($get_par_url);
$purl = mysql_fetch_array($get_purl_res, MYSQL_ASSOC);
echo "<a href=\"http://www.example.com/".$purl['keyword']."/".$url['keyword']."/\">".$name['name']."</a> ";
mysql_free_result($get_name_res);
mysql_free_result($get_url_res);
mysql_free_result($get_par_res);
mysql_free_result($get_purl_res);
}
mysql_free_result($get_cats_res);
If anyone knows a better way, and there must be a better way, I'd love to hear it.
Re: Show all categories a product is in
Posted: Wed Apr 14, 2010 3:09 am
by Qphoria
The concept is there.. You use external functions where you could use internal ones but no matter.. if it works, that is all that matters
Re: Show all categories a product is in
Posted: Thu Apr 15, 2010 2:54 am
by Chones
Cheers mate. Would love to find a more beautiful solution using internal functions but am not familiar with the functions yet. Still getting used to the cart.
Think it's great, but first time I've installed it. Is there a list of functions and how to use them, as there is with the Wordpress Codex? If so, I've missed it. If not, might be a useful thing to implement in the Wiki going forward.
Re: Show all categories a product is in
Posted: Fri Dec 03, 2010 12:52 am
by andyspartan
I realise this post is quite old but wanted to show a very simpler way of doing it
Put the following code in to catalog/model/category.php:
Code: Select all
public function getProductCategories($prodid) {
$query = $this->db->query("SELECT category_id FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . $prodid . "'";)
if($query->num_rows > 0) return $query->rows;
return false;
}
Then put this in your controller e.g category.php or product.php - obviously you will need to $this->load->model('catalog/category') if it is not loaded
Code: Select all
$prodcategories = $this->model_catalog_category->getProductCategories($myproductidhere);
$cats = array();
if($prodcategories) {
foreach($prodcategories as $prodcategory) {
$cats[] = $prodcategory['category_id'];
}
$this->data['cats'] = $cats;
}
Replace $myproductid here with the product ID you want to query
$this->data['cats'] will contain an array of all category id's assocatied with that product

Re: Show all categories a product is in
Posted: Thu Feb 17, 2011 2:44 am
by dry_flood
Hey
I'm struggling to get my head round this, does anyone know how to implement this so that it will show the categories a product belongs to on the product page.
So far I have added the following code:
catalog/model/catalog/category.php
Code: Select all
public function getProductCategories($prodid) {
$query = $this->db->query("SELECT category_id FROM " . DB_PREFIX . "product_to_category WHERE product_id = '" . $prodid . "'";)
if($query->num_rows > 0) return $query->rows;
return false;
}
catalog/controller/product/product.php
Code: Select all
$prodcategories = $this->model_catalog_category->getProductCategories($product_id);
$cats = array();
if($prodcategories) {
foreach($prodcategories as $prodcategory) {
$cats[] = $prodcategory['category_id'];
}
$this->data['cats'] = $cats;
Now I am assuming that I would need to call this in the product.tpl file but not sure how? am I close or completley off track?
Cheers
Gaz
Re: Show all categories a product is in
Posted: Sat Mar 05, 2011 3:24 am
by alex1
Thanks guys, this is what I'm looking for.
Anyone know what you need to do to display the returned categories in the template?
Re: Show all categories a product is in
Posted: Sat Mar 05, 2011 8:00 am
by Chones
I was bored so I rewrote my original code. Just stick this somewhere in your product.tpl file.
Code: Select all
<?php
$this->load->model('catalog/category');
$this->load->model('tool/seo_url');
$query = $this->db->query("SELECT category_id
FROM product_to_category
WHERE product_id = '".$product_id."'");
$prodcategories = $query->rows;
foreach($prodcategories as $prodcategory){
$category_id = $prodcategory['category_id'];
$category_info = $this->model_catalog_category->getCategory($category_id);
$caturl = $this->model_tool_seo_url->rewrite(HTTP_SERVER . 'index.php?route=product/category&path=' . $category_id); ?>
<a href="<?php echo $caturl; ?>"><?php echo $category_info['name']; ?></a>
<br />
<?php } ?>
Re: Show all categories a product is in
Posted: Sat Mar 05, 2011 9:41 am
by openmycart.com
yeah, it's seem we no longer need controller files, very simple
Re: Show all categories a product is in
Posted: Sat Mar 05, 2011 10:00 am
by Chones
I like the MVC structure a lot, but when you want to make small changes to a template, I think this way is best - no original files altered - but that's just me

Re: Show all categories a product is in
Posted: Sat Mar 05, 2011 3:32 pm
by alex1
Chones, that worked perfectly!
Thank you!
I'm curious what your site looks like... mind throwing out a link?
Re: Show all categories a product is in
Posted: Sat Mar 05, 2011 3:52 pm
by Chones
I didn't use that for any shop in the end, which is why I hadn't updated the code until now. Here's the shop I was going to use it for though:
http://scarletandjones.com
Re: Show all categories a product is in
Posted: Sun Mar 06, 2011 2:49 pm
by alex1
Chones,
Would it be an easy change to include the full breadcrumb, instead of just the last category?
Re: Show all categories a product is in
Posted: Sun Mar 06, 2011 4:38 pm
by Chones
It should be reasonably easy. The problems start if you have a sub-category in more than one category, but I guess you could just use the first one. I'll look into it. Or you could try yourself, you'll probably need to start with
Code: Select all
$query = $this->db->query("SELECT parent_id
FROM category
WHERE category_id = '".$category_id."'");
$parentcategories = $query->rows;
although parent_id may already be included in $category_info so you may not have to do this.
Re: Show all categories a product is in
Posted: Thu Apr 28, 2011 12:38 am
by adriankoooo
Chones, if you have a little time please try to modify the code to show subcategories too. I have products in subcategories, and in subcategory-subcategory too.
Re: Show all categories a product is in
Posted: Mon Aug 01, 2011 2:31 am
by miste83
Sometimes it shows me this error:
Undefined index: name in ..catalog/view/theme/xxxx/template/product/product.tpl
in this line:
<td><a href="<?php echo $caturl; ?>"><?php echo html_entity_decode($category_info['name'], ENT_QUOTES, 'UTF-8'); ?></a><br />
Re: Show all categories a product is in
Posted: Fri Jan 20, 2012 4:09 am
by designbuyers
do you have xml vQmod version ?
It's working only for 1 category product