Post by Chones » Tue Apr 13, 2010 10:30 pm

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.

http://scarletandjones.com/
http://sharpdressedman.co.uk/
http://coffincompany.co.uk/
http://horsesculptures.co.uk/
If I've helped you out, why not buy me a beer? http://craigmurray.me.uk


User avatar
Active Member

Posts

Joined
Wed Mar 24, 2010 9:07 pm
Location - London

Post by Chones » Wed Apr 14, 2010 1:51 am

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.

http://scarletandjones.com/
http://sharpdressedman.co.uk/
http://coffincompany.co.uk/
http://horsesculptures.co.uk/
If I've helped you out, why not buy me a beer? http://craigmurray.me.uk


User avatar
Active Member

Posts

Joined
Wed Mar 24, 2010 9:07 pm
Location - London

Post by Qphoria » Wed Apr 14, 2010 3:09 am

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

Image


User avatar
Administrator

Posts

Joined
Tue Jul 22, 2008 3:02 am

Post by Chones » Thu Apr 15, 2010 2:54 am

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.

http://scarletandjones.com/
http://sharpdressedman.co.uk/
http://coffincompany.co.uk/
http://horsesculptures.co.uk/
If I've helped you out, why not buy me a beer? http://craigmurray.me.uk


User avatar
Active Member

Posts

Joined
Wed Mar 24, 2010 9:07 pm
Location - London

Post by andyspartan » Fri Dec 03, 2010 12:52 am

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 :)

Newbie

Posts

Joined
Fri Dec 03, 2010 12:33 am

Post by dry_flood » Thu Feb 17, 2011 2:44 am

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

New member

Posts

Joined
Tue Dec 14, 2010 4:33 am

Post by alex1 » Sat Mar 05, 2011 3:24 am

Thanks guys, this is what I'm looking for.

Anyone know what you need to do to display the returned categories in the template?

Active Member

Posts

Joined
Sat Oct 16, 2010 9:49 am

Post by Chones » Sat Mar 05, 2011 8:00 am

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  }  ?>

http://scarletandjones.com/
http://sharpdressedman.co.uk/
http://coffincompany.co.uk/
http://horsesculptures.co.uk/
If I've helped you out, why not buy me a beer? http://craigmurray.me.uk


User avatar
Active Member

Posts

Joined
Wed Mar 24, 2010 9:07 pm
Location - London

Post by openmycart.com » Sat Mar 05, 2011 9:41 am

yeah, it's seem we no longer need controller files, very simple

Find and get many various of opencart modules, themes, mods, etc for your opencart store at http://www.openmycart.com/oc/, OPENCART SITE customization and Maintenance supports at here


User avatar
Active Member

Posts

Joined
Tue Oct 12, 2010 4:47 am


Post by Chones » Sat Mar 05, 2011 10:00 am

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 ;)

http://scarletandjones.com/
http://sharpdressedman.co.uk/
http://coffincompany.co.uk/
http://horsesculptures.co.uk/
If I've helped you out, why not buy me a beer? http://craigmurray.me.uk


User avatar
Active Member

Posts

Joined
Wed Mar 24, 2010 9:07 pm
Location - London

Post by alex1 » Sat Mar 05, 2011 3:32 pm

Chones, that worked perfectly!

Thank you!

I'm curious what your site looks like... mind throwing out a link?

Active Member

Posts

Joined
Sat Oct 16, 2010 9:49 am

Post by Chones » Sat Mar 05, 2011 3:52 pm

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

http://scarletandjones.com/
http://sharpdressedman.co.uk/
http://coffincompany.co.uk/
http://horsesculptures.co.uk/
If I've helped you out, why not buy me a beer? http://craigmurray.me.uk


User avatar
Active Member

Posts

Joined
Wed Mar 24, 2010 9:07 pm
Location - London

Post by alex1 » Sun Mar 06, 2011 2:49 pm

Chones,

Would it be an easy change to include the full breadcrumb, instead of just the last category?

Active Member

Posts

Joined
Sat Oct 16, 2010 9:49 am

Post by Chones » Sun Mar 06, 2011 4:38 pm

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.

http://scarletandjones.com/
http://sharpdressedman.co.uk/
http://coffincompany.co.uk/
http://horsesculptures.co.uk/
If I've helped you out, why not buy me a beer? http://craigmurray.me.uk


User avatar
Active Member

Posts

Joined
Wed Mar 24, 2010 9:07 pm
Location - London

Post by adriankoooo » Thu Apr 28, 2011 12:38 am

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.

Active Member

Posts

Joined
Thu Mar 03, 2011 6:52 am


Post by miste83 » Mon Aug 01, 2011 2:31 am

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 />

Newbie

Posts

Joined
Fri Dec 17, 2010 8:24 pm

Post by designbuyers » Fri Jan 20, 2012 4:09 am

do you have xml vQmod version ?

It's working only for 1 category product

OpenCart :
The best Open Source Cart for eCommerce


User avatar
Active Member

Posts

Joined
Tue Nov 09, 2010 4:25 pm
Who is online

Users browsing this forum: No registered users and 98 guests