Today I want to show you how to add category name into meta title tag on each product page. This is great for seo purposes - which mean that your store will be shining like a star in google search results ;-) Unfortunately, by default PrestaShop core doesn't allow to add this in template file, so in this case we must modify Classes of the engine. We must edit the Meta.php class located in the /classes/Meta.php directory.
First step: create the script which will get category name from prestashop database:
$sql = 'SELECT id_category_default FROM `'._DB_PREFIX_.'product` WHERE id_product = '.(int)$id_product.''; $row_cat = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql); $cat=new Category($row_cat['id_category_default'],(int)$id_lang); $row['meta_title']=$row['meta_title']." ".$cat->name;
Second step: search for getProductMetas function
public static function getProductMetas($id_product, $id_lang, $page_name)
{
FUNCTION BODY HERE
}
Now we must paste the code for category name into the getProductMetas function, into the if condition:
if ($row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql))
{
------- HERE -------
if (empty($row['meta_description']))
$row['meta_description'] = strip_tags($row['description_short']);
return Meta::completeMetaTags($row, $row['name']);
}
Final getProductMetas function:
public static function getProductMetas($id_product, $id_lang, $page_name)
{
$sql = 'SELECT `name`, `meta_title`, `meta_description`, `meta_keywords`, `description_short`
FROM `'._DB_PREFIX_.'product` p
LEFT JOIN `'._DB_PREFIX_.'product_lang` pl ON (pl.`id_product` = p.`id_product`'.Shop::addSqlRestrictionOnLang('pl').')
'.Shop::addSqlAssociation('product', 'p').'
WHERE pl.id_lang = '.(int)$id_lang.'
AND pl.id_product = '.(int)$id_product.'
AND product_shop.active = 1';
if ($row = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql))
{
//THIS IS CODE FOR CATEGORY NAME
$sql = 'SELECT id_category_default FROM `'._DB_PREFIX_.'product` WHERE id_product = '.(int)$id_product.'';
$row_cat = Db::getInstance(_PS_USE_SQL_SLAVE_)->getRow($sql);
$cat=new Category($row_cat['id_category_default'],(int)$id_lang);
$row['meta_title']=$row['meta_title']." ".$cat->name;
if (empty($row['meta_description']))
$row['meta_description'] = strip_tags($row['description_short']);
return Meta::completeMetaTags($row, $row['name']);
}
return Meta::getHomeMetas($id_lang, $page_name);
}
Article written byMilosz Myszczuk, PrestaShop expert. CEO and founder of the VEKIA interactive agency. Learn more.
If you like this article, support our work!

Keep “Add to cart” visible on long product pages! Sticky Cart Bar FREE shows a slim, sticky stri...

Omnibus Directive Pro module for PrestaShop. It records price history and displays the lowest 30-day...
39.99 €

Free Shipping Progress Bar is a free PrestaShop module: show shoppers how much is left for free deli...

Free PrestaShop addon for EU Omnibus compliance. It automatically records price history and shows th...