Category name in product title tag

  • Posts: 344
  • Thank you received: 3
9 years 7 months ago #251858

-- HikaShop version -- : 2.6.4
-- Joomla version -- : 3.6.2
-- PHP version -- : 7

Is it possible to change a little code so that the product/title tag will automatically include the name of the main category that it is in ?

And this only displays automatic if we leave the product page title empty.

I.e:

Product name - Category name - Site name
Spanish Gold ring - Gold Rings - Site name

I did the same thinh with Yootheme zoo component and this gave me better ratings in Google.
Even tough it wasn't a online store, but a local city guide.

is this possible ?

Please Log in or Create an account to join the conversation.

  • Posts: 200
  • Thank you received: 75
9 years 7 months ago #251867

Hi,

that is certainly possible.
If you use the default product page layout, you can edit the show_default view by going to "Display -> Views" (don't forget to select your template).

You can add the code near the top, for example right between lines 9 and 10:

defined('_JEXEC') or die('Restricted access');
?>

The following should do the trick, I added some comments for explanation:
//CHECK IF PAGE TITLE FIELD IS EMPTY
if(empty($this->element->product_page_title)){

$doc=JFactory::getDocument();
$sitename=JFactory::getApplication()->getCfg('sitename');
//THE PRODUCT INFO ONLY CONTAINS THE CATEGORY ID, NOT THE CATEGORY NAME, SO WE FIRST NEED TO LOAD THE CATEGORY CLASS
$categoryClass = hikashop_get('class.category');
$product_category = $categoryClass->get($this->element->category_id)->category_name;
//THEN WE CAN SET THE PAGE TITLE AS: PRODUCT NAME - PRODUCT CATEGORY - SITE NAME
$doc->setTitle($this->element->product_name.' - '.$product_category.' - '.$sitename);
}

Hope that helps. Kind regards,

Please Log in or Create an account to join the conversation.

  • Posts: 1119
  • Thank you received: 114
9 years 7 months ago #251871

Hi,

I think this line:

//THEN WE CAN SET THE PAGE TITLE AS: PRODUCT NAME - PRODUCT CATEGORY - SITE NAME
$doc->setTitle($this->element->product_name.' - '.$product_category.' - '.$sitename);
}

should be changed to:
//THEN WE CAN SET THE PAGE TITLE AS: PRODUCT NAME - PRODUCT CATEGORY - SITE NAME
$doc->setTitle($this->element->main->product_name.' - '.$product_category.' - '.$sitename);
}

Unless you would like to show selected variant too...

However I see another issue here for SEO. Not sure am I right but if you have 2 or 3 categories? As example: dress "ella" is in 2 categories like "new" and "dresses" and main category of course is "dresses" (as per canonical url) but if on selecting category in product edition page you would choose category "new" as second category it will be picked up as latest selected category (as example you would choose category "dress" and later product will be added to category "new" by mass action) and title will like this: dress "ella" - new - mysite but for real it should have "dress" instead "new" I think so. How this can be solved?

Thank you

Last edit: 9 years 7 months ago by kyratn.

Please Log in or Create an account to join the conversation.

  • Posts: 344
  • Thank you received: 3
9 years 7 months ago #251874

Tnx for reply. Excellent this code works perfect

I did a little change to the code so it displays not matter if the title field is empty or not.
....and if also added the code change Kyratn came up with.

It solved the problem with the products with variants. Tnx a lot.

So now the code looks like his

//WORKS NOT MATTER IF TITLE FIELD IS EMPTY OR NOT
if(($this->element->product_page_title)){

$doc=JFactory::getDocument();
$sitename=JFactory::getApplication()->getCfg('sitename');
//THE PRODUCT INFO ONLY CONTAINS THE CATEGORY ID, NOT THE CATEGORY NAME, SO WE FIRST NEED TO LOAD THE CATEGORY CLASS
$categoryClass = hikashop_get('class.category');
$product_category = $categoryClass->get($this->element->category_id)->category_name;
//THEN WE CAN SET THE PAGE TITLE AS: PRODUCT NAME - PRODUCT CATEGORY - SITE NAME
$doc->setTitle($this->element->main->product_name.' - '.$product_category.' - '.$sitename);
}


But I have one small question.


I am using a plugin called Title Manager plugin to display the site name. I don't want to disable this. So now my site name is displaying two times after one another.

Any advice on how to remove the "site name" from the above code ?

Tnx

Last edit: 9 years 7 months ago by river.

Please Log in or Create an account to join the conversation.

  • Posts: 344
  • Thank you received: 3
9 years 7 months ago #251875

However I see another issue here for SEO. Not sure am I right but if you have 2 or 3 categories? As example: dress "ella" is in 2 categories like "new" and "dresses" and main category of course is "dresses" (as per canonical url) but if on selecting category in product edition page you would choose category "new" as second category it will be picked up as latest selected category (as example you would choose category "dress" and later product will be added to category "new" by mass action) and title will like this: dress "ella" - new - mysite but for real it should have "dress" instead "new" I think so. How this can be solved?

Yes I discovered the same

I think best solution would be if the code selected is first category to the left, and not the last one added after update.

But im not a coder, so any advice ?

Last edit: 9 years 7 months ago by river.

Please Log in or Create an account to join the conversation.

  • Posts: 200
  • Thank you received: 75
9 years 7 months ago #251876

Hi,

note that you can't always use

$this->element->main->product_name
since it is only defined for products with variants, not for products that have no variant.

So if you have products both with and without variants then you would need to take care of both situations and the correct solution would be:
if (hikashop_getCID('product_id')!=$this->element->product_id && isset ($this->element->main->product_name))
	$doc->setTitle($this->element->main->product_name.' - '.$product_category.' - '.$sitename);
else
	$doc->setTitle($this->element->product_name.' - '.$product_category.' - '.$sitename);
}

Regarding SEO, if you use canonical URL's it's not going to be a problem for SEO. The only thing that would change is the title of your page as it appears in search engines. So the product might indeed first show in search engines as dress "ella" - dresses - mysite, and if at some point you add a second category "new", then it might start showing up as dress "ella" - new - mysite.

As far as I can see, only the latest category is retrieved with the product info, so if you always want the first category to show it is possible, but I guess you would need to do a database query. The hikashop_product_category table stores all the categories, so you can search for all the entries matching the current product_id and return the category_id corresponding with the lowest product_category_id, which will be the first category of the product.

Kind regards,

The following user(s) said Thank You: kyratn

Please Log in or Create an account to join the conversation.

  • Posts: 1119
  • Thank you received: 114
9 years 7 months ago #251899

Hi,

Thanks for replay GW and you solution. As my all product use variants I didn't think about that but I hope it will help @river.

About SEO. Well, yes if you use canonical there should be no worries. Google robots will understand but I see issue. As from my knowledge title should be unique as per product or category. So if product will start to have different titles it could hurt search results. Example: having 2 products with same title will rank lower then product with one title for same keyword. So If indexed product will get different category title later up on search in google it will be lower ranked... Unless your page rank is high with google and robots index your site asap after new product added.
I may be wrong but this is what I tho.

My php skills not so good as your GW but would it be possible to exclude category? Like category with name "NEW"...

Thanks

Please Log in or Create an account to join the conversation.

  • Posts: 200
  • Thank you received: 75
9 years 7 months ago #251925

@river: if you want to remove the site name from the code, you can delete this line:

$sitename=JFactory::getApplication()->getCfg('sitename');
and change the line where you set te page title to this:
$doc->setTitle($this->element->main->product_name.' - '.$product_category);

@kyratn: using the above code your product is not going to have multiple titles, it will still be one unique title. It would just change over time if you add a new category. So as I explained in my last post your product may show up in search results as dress "ella" - dresses - mysite, and when you add a new category and a Google crawler comes along it will pick up the new title and your product will then show as dress "ella" - new - mysite, but that does not mean it has multiple titles.

However, this doesn't matter so much anymore, since it it is in fact quite easy to get all the categories on the product page. I didn't look carefully yesterday, but all the categories of the product can simply be found in $this->categories, so if you always want the first category, you can just grab it by using for example reset($this->categories);.

The following should then be exactly what you guys were looking for:
$doc=JFactory::getDocument();
$sitename=JFactory::getApplication()->getCfg('sitename');
//GET THE FIRST CATEGORY OF THE PRODUCT
$product_category = reset($this->categories)->category_name;
//THEN WE CAN SET THE PAGE TITLE AS: PRODUCT NAME - PRODUCT CATEGORY - SITE NAME
if (hikashop_getCID('product_id')!=$this->element->product_id && isset ($this->element->main->product_name))
	$doc->setTitle($this->element->main->product_name.' - '.$product_category.' - '.$sitename);
else
	$doc->setTitle($this->element->product_name.' - '.$product_category.' - '.$sitename);


Hope that helps. Kind regards,

Last edit: 9 years 7 months ago by GW.

Please Log in or Create an account to join the conversation.

  • Posts: 1119
  • Thank you received: 114
9 years 7 months ago #251926

Hi,

Thanks for code update but unfortunately it doesn't work. I get error on this line:

$product_category = reset($this->categories)->category_name;

Call to undefined method hikashopCategoryClass::reset() .....

and category is not displayed....

Thanks for help

Kind Regards

Please Log in or Create an account to join the conversation.

  • Posts: 85681
  • Thank you received: 14042
  • MODERATOR
9 years 7 months ago #251959

Hi,

reset is a function of PHP. It's not possible that it doesn't exist.
And you error message indicates that you wrote $categoryClass->reset (or something like that) and not just reset. So please double check the code that you wrote.

Please Log in or Create an account to join the conversation.

  • Posts: 1119
  • Thank you received: 114
9 years 7 months ago #252036

Hi,

My bad, after i saw it doesn't work i have tried to modify it....

So this is the full code from line 1 until 23:

<?php
/**
 * @package	HikaShop for Joomla!
 * @version	2.3.0
 * @author	hikashop.com
 * @copyright	(C) 2010-2014 HIKARI SOFTWARE. All rights reserved.
 * @license	GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
 */
defined('_JEXEC') or die('Restricted access');

//CHECK IF PAGE TITLE FIELD IS EMPTY
if(empty($this->element->product_page_title)){

$doc=JFactory::getDocument();
$sitename=JFactory::getApplication()->getCfg('sitename');
//GET THE FIRST CATEGORY OF THE PRODUCT
$product_category = reset($this->categories)->category_name;
//THEN WE CAN SET THE PAGE TITLE AS: PRODUCT NAME - PRODUCT CATEGORY - SITE NAME
if (hikashop_getCID('product_id')!=$this->element->product_id && isset ($this->element->main->product_name))
	$doc->setTitle($this->element->main->product_name.' - '.$product_category.' - '.$sitename);
else
	$doc->setTitle($this->element->product_name.' - '.$product_category.' - '.$sitename);
}

And error on product page :
Notice: Trying to get property of non-object in ...../html/com_hikashop/product/show_default_tabs.php on line 17

line 17 is this:
$product_category = reset($this->categories)->category_name;

And category is not displayed in title...

Kind Regards

Last edit: 9 years 7 months ago by kyratn.

Please Log in or Create an account to join the conversation.

  • Posts: 200
  • Thank you received: 75
9 years 7 months ago #252059

Hi,

your error is in a file show_default_tabs, which is not one of the standard Hikashop page layouts (show_default, show_tabular, show_reversed) where the code I gave in my last post works fine. It looks like you've made other code alterations and the categories are not loaded as normal in the variable $this->categories.

I see for instance in this thread ( www.hikashop.com/forum/4-how-to/885271-h...gory-of-product.html ) you load the categories manually from the database and store the category names already in an array which you call $categories.
So if you're doing that, instead of using

$product_category = reset($this->categories)->category_name;
you should use this to get the first category
$product_category = $categories[0];

Hope that helps. Kind regards,

Please Log in or Create an account to join the conversation.

  • Posts: 1119
  • Thank you received: 114
9 years 7 months ago #252060

Ahh, you right. I am using my own view which is not hikashop default...

Thanks for help GW

Have a great day

Please Log in or Create an account to join the conversation.

Time to create page: 0.100 seconds
Powered by Kunena Forum