Jump to content
ItalianBoutiqueShoes

Search and tag pages: add search text or tag on title and description meta tag

Recommended Posts

Hi Daniele,

 

You can create the file override/classes/Meta.php and insert the following:

<?php

class Meta extends MetaCore
{
    public static function getMetaByPage($page, $id_lang)
    {
        $search_translations = array(1 => "Search results for query", 2 => "Recherche résultats pour requête");
        $tag_translations = array(1 => "Search results for tag", 2 => "Recherche résultats pour tag");
        
        if ($page == 'search' && Tools::getValue('search_query'))
            return array('title' => Tools::getValue('search_query'), 'description' => (isset($search_translations[$id_lang]) ? $search_translations[$id_lang] : $search_translations[1])." '".Tools::getValue('search_query')."'");
        elseif ($page == 'search' && Tools::getValue('tag'))
            return array('title' => Tools::getValue('tag'), 'description' => (isset($tag_translations[$id_lang]) ? $tag_translations[$id_lang] : $tag_translations[1])." '".Tools::getValue('tag')."'");
        else
            return parent::getMetaByPage($page, $id_lang);
    }
}

This code will override the meta data entered on the Preferences > SEO & URL for the "search" page with the search phrase for the meta title and meta description. The meta description will have some translated text in front of the search phrase. You can add more translations as needed by adding another language ID and translation to the $search_translations and $tag_translations variables.

 

You may need to delete cache/class_index.php before the override will work, depending on your PrestaShop version. I hope this helps.

Share this post


Link to post
Share on other sites

Thank you for the compliment. :)

 

Unfortunately, I'm currently out of work, since the last guy I worked for had a baby and is now too busy to provide me with steady work. I'm going to answer some questions on the forums again to see if I can drum up some more work. I'd prefer to have one person who can give me regular work and can afford to pay me. It's better than returning to the days of doing many small projects and never knowing who will pay you and who won't. 

Share this post


Link to post
Share on other sites

Change the code above use the native PHP function ucfirst() to make the first character uppercase:

 

Example:

$foo = 'bonjour tout le monde!';
$foo = ucfirst($foo);  

Share this post


Link to post
Share on other sites

 

Change the code above use the native PHP function ucfirst() to make the first character uppercase:

 

Example:

$foo = 'bonjour tout le monde!';
$foo = ucfirst($foo);  

Can you please show me where to add this code? I always get an HTTP 500 error when I add the code.

Share this post


Link to post
Share on other sites

I have this code in override/classes/Meta.php

 

<?php

class Meta extends MetaCore
{
    public static function getMetaByPage($page, $id_lang)

    {
      
        $x = "Results search for ";

      
        if ($page == 'search' && Tools::getValue('search_query'))

            return array('title' => ($x).' "'.Tools::getValue('search_query').'"', 'description' => ($x)." '".Tools::getValue('search_query')."'");

        elseif ($page == 'search' && Tools::getValue('tag'))

            return array('title' => Tools::getValue('tag'), 'description' => ($x)." '".Tools::getValue('tag')."'");

        else

            return parent::getMetaByPage($page, $id_lang);
    }
}

 

How can I add translate $x to another languages in Admin Panel >Localizations>Translations?

How can I add string to translate in php file this method doesn't work:   $this->l('Results search for ');

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×