Unleash the Full Potential of Your PrestaShop Store

Welcome to MyPresta.eu – Your reliable partner in the e-commerce world. Find innovative modules that will take your business to a new level.

Category name in meta title on product page

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

 

Zdjęcie autora: Milosz Myszczuk

Artykuł napisany przez Milosza Myszczuka, eksperta PrestaShop i oficjalnego moderatora społeczności PrestaShop. CEO i założyciel agencji interaktywnej VEKIA. Dowiedz się więcej.

If you like this article, support our work!

Comments