Page 1 of 1

show category title on product page

Posted: Fri Nov 13, 2009 2:35 am
by matt6805
I've been trying to adjust the product.php controller to allow me the ability to display the product category title on an individual product tpl template. I find it odd that OpenCart doesn't do this by default. I know there's a way to do it, and I've seen a few threads on this board that are asking the same question, but none of them have been answered.

Can anyone help, or point me in the right direction? Thank you in advance!

Regards,
Matt

Re: show category title on product page

Posted: Fri Nov 13, 2009 2:59 am
by Qphoria
In all controllers the breadcrumb builds its path for the product. So just set a variable to the last path in the for loop in catalog/controller/product/product.php

Something like

FIND:

Code: Select all

if ($category_info) {
	$this->document->breadcrumbs[] = array(
	'href'      => $this->model_tool_seo_url->rewrite($this->url->http('product/category&path=' . $path)),
	'text'      => $category_info['name'],
	'separator' => $this->language->get('text_separator')
	);
}
AFTER, ADD:

Code: Select all

$category_title = $category_info['name'];

Then change:
$this->document->title = $product_info['name'];
to
$this->document->title = $category_title . ' - ' . $product_info['name'];

Re: show category title on product page

Posted: Fri Nov 13, 2009 4:00 am
by matt6805
Thank you for the quick response. I altered the product.php controller as you suggested, but then how do i call the value in the tpl? I tried echoing $category_title, but that didn't work.

I should also mention that I don't want this in the title tag. I'm trying to include the category name in the body somewhere, perhaps as an H1 tag. So, I'm not sure if $this->document->title will allow me to do that?

Regards,
Matt

Re: show category title on product page

Posted: Sat Mar 27, 2010 3:49 pm
by dramony
Qphoria wrote:In all controllers the breadcrumb builds its path for the product. So just set a variable to the last path in the for loop in catalog/controller/product/product.php

Something like

FIND:

Code: Select all

if ($category_info) {
	$this->document->breadcrumbs[] = array(
	'href'      => $this->model_tool_seo_url->rewrite($this->url->http('product/category&path=' . $path)),
	'text'      => $category_info['name'],
	'separator' => $this->language->get('text_separator')
	);
}
AFTER, ADD:

Code: Select all



$category_title = $category_info['name'];

Then change:
$this->document->title = $product_info['name'];
to
$this->document->title = $category_title . ' - ' . $product_info['name'];
output on my title bar:

Code: Select all

- product name
the category title is missing.

Or can this be:
Store Name - category - product name

What do you think?

Re: show category title on product page

Posted: Mon Apr 05, 2010 11:58 pm
by Noman
Hi Guys

I need something similar to the above one.

Is there any way to include Category and Subcategory names in text format somewhere down in Product description box?

We have a product, which belongs to many categories and subcategories [Brands and Models]. Our Search has "Search in product descriptions" enabled by default. But Search won't find anything if we don't include the full name in the Product Name

What we have now:
Acer -> Aspire 3000 - Universal 3G Laptop Modem [this modem belongs to many categories and subcats]

Search won't find anything if I try only Acer Aspire 3000.

Adding automatically Acer -> Aspire 3000 [main category and subcategory names] to the product description field in Admin would help.

I know, this one isn't easy. Thank you.

Re: show category title on product page

Posted: Mon Sep 05, 2011 6:50 am
by dmonco
So, what about the answer? Did somebody do that? Did somebody add category title to product page?
Please, explain how to do that.

Re: show category title on product page

Posted: Sat Nov 12, 2011 12:57 am
by nec2
@dmonco
See post by Chones http://forum.opencart.com/viewtopic.php?t=13347

My modified a little bit modified code (not using SEO url) :

Code: Select all

<?php
   $this->load->model('catalog/category');
   $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 = (HTTP_SERVER . 'index.php?route=product/category&path=' . $category_id); ?>
   Category : <a href="<?php echo $caturl; ?>"><?php echo $category_info['name']; ?></a><br />
<?php  }  ?>

Re: show category title on product page

Posted: Tue Nov 15, 2011 2:04 am
by dklnd
Qphoria wrote:In all controllers the breadcrumb builds its path for the product. So just set a variable to the last path in the for loop in catalog/controller/product/product.php

Something like

FIND:

Code: Select all

if ($category_info) {
	$this->document->breadcrumbs[] = array(
	'href'      => $this->model_tool_seo_url->rewrite($this->url->http('product/category&path=' . $path)),
	'text'      => $category_info['name'],
	'separator' => $this->language->get('text_separator')
	);
}
AFTER, ADD:

Code: Select all

$category_title = $category_info['name'];

Then change:
$this->document->title = $product_info['name'];
to
$this->document->title = $category_title . ' - ' . $product_info['name'];
What do you mean with 'after - add' do you add the code in the lines or where do I put the code? How will the total code look like?

Thx!

Re: show category title on product page

Posted: Fri Nov 18, 2011 5:27 pm
by dklnd
please? anyone?

Re: show category title on product page

Posted: Thu Jan 05, 2012 2:16 pm
by BBMleaf
@nec - thanks, your little work around still works

Re: show category title on product page

Posted: Thu Jan 19, 2012 6:33 am
by jeoob
Hmm it doesnt work for us for some reason on 1.5.1.3 .... I triple checked it, but the title tag still just has the product name in it. did something change in this version?

Re: show category title on product page

Posted: Fri Mar 02, 2012 6:24 am
by eleven28
nec2 wrote:@dmonco
See post by Chones http://forum.opencart.com/viewtopic.php?t=13347

My modified a little bit modified code (not using SEO url) :

Code: Select all

<?php
   $this->load->model('catalog/category');
   $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 = (HTTP_SERVER . 'index.php?route=product/category&path=' . $category_id); ?>
   Category : <a href="<?php echo $caturl; ?>"><?php echo $category_info['name']; ?></a><br />
<?php  }  ?>
Worked like a charm for me in 1.5.1.3! Thank you.

Re: show category title on product page

Posted: Tue Mar 05, 2013 11:00 pm
by yubnub
hi,

I realize this is an old post - however I wanted to use Q's code but on ver 1.5.5.1

the code basically works and does return the page title exactly as planned - however i do get a error in my log :

Code: Select all

2013-03-05 14:49:53 - PHP Notice:  Undefined variable: category_info in /var/www/vhosts/xxxxx.co.uk/httpdocs/store/vqmod/vqcache/vq2-catalog_controller_product_product.php on line 226
Does anyone know what tweaks would be needed to fix the error?

many thanks in advance,

anthony

Re: show category title on product page

Posted: Sun Mar 24, 2013 6:25 am
by beattie
@eleven28
Just a minor tweak to get this working in OC version 1.5.5.1:
added the prefix oc_ to product_to_category

Code: Select all

<?php
       $this->load->model('catalog/category');
       $query = $this->db->query("SELECT category_id
       FROM oc_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 = (HTTP_SERVER . 'index.php?route=product/category&path=' . $category_id); ?>
       Category : <a href="<?php echo $caturl; ?>"><?php echo $category_info['name']; ?></a><br />
    <?php  }  ?>