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!

Apply conditional order discounts using native cart rules. Create BOGO deals, buy N get M free, spen...
149.00 €

Sell optional checkout services your customers choose: gift wrapping, shipping insurance, express ha...
149.00 €

Charge handling, service or packaging fees only when your rules match. Conditional Order Fees adds a...
129.00 €

Control which shipping carriers customers see at checkout. Shipping Method Rules is a PrestaShop pol...
129.00 €