Hey. I myself am not a programmer, but an SEO, and I’m still not at the highest level in the code, so I rested on such a problem. It is necessary to make a multilingual hreflang markup on prestashop. I noticed that the prestashop base module "Language Picker" is almost completely sharpened. I corrected a little and got:

{foreach from=$languages key=k item=language name="languages"} {if $language.iso_code != $lang_iso} {assign var=indice_lang value=$language.id_lang} {if isset($lang_rewrite_urls.$indice_lang)} <link href="{$lang_rewrite_urls.$indice_lang|escape:htmlall}" rel="alternate" hreflang="{$language.iso_code|escape:'html':'UTF-8'}"> {else} <link href="{$link->getLanguageLink($language.id_lang)|escape:htmlall}" rel="alternate" hreflang="{$language.iso_code|escape:'html':'UTF-8'}"> {/if} {/if} {if $language.iso_code != $lang_iso} {/if} {/foreach} 

BUT! the problem is that I did not take into account the fact that the function was made so that the first line would not generate a link (there was no output of the link to the current page, so that the page would not refer to itself - for search robots).

 <link href="{$lang_rewrite_urls.$indice_lang|escape:htmlall}" rel="alternate" hreflang="{$language.iso_code|escape:'html':'UTF-8'}"> 

there is also php, but I don’t see the generation change parameters there (either they aren’t there or I don’t know them yet)

 protected function _prepareHook($params) { $languages = Language::getLanguages(true, $this->context->shop->id); if (!count($languages)) return false; $link = new Link(); if ((int)Configuration::get('PS_REWRITING_SETTINGS')) { $default_rewrite = array(); if (Dispatcher::getInstance()->getController() == 'product' && ($id_product = (int)Tools::getValue('id_product'))) { $rewrite_infos = Product::getUrlRewriteInformations((int)$id_product); foreach ($rewrite_infos as $infos) $default_rewrite[$infos['id_lang']] = $link->getProductLink((int)$id_product, $infos['link_rewrite'], $infos['category_rewrite'], $infos['ean13'], (int)$infos['id_lang']); } if (Dispatcher::getInstance()->getController() == 'category' && ($id_category = (int)Tools::getValue('id_category'))) { $rewrite_infos = Category::getUrlRewriteInformations((int)$id_category); foreach ($rewrite_infos as $infos) $default_rewrite[$infos['id_lang']] = $link->getCategoryLink((int)$id_category, $infos['link_rewrite'], $infos['id_lang']); } if (Dispatcher::getInstance()->getController() == 'cms' && (($id_cms = (int)Tools::getValue('id_cms')) || ($id_cms_category = (int)Tools::getValue('id_cms_category')))) { $rewrite_infos = (isset($id_cms) && !isset($id_cms_category)) ? CMS::getUrlRewriteInformations($id_cms) : CMSCategory::getUrlRewriteInformations($id_cms_category); foreach ($rewrite_infos as $infos) { $arr_link = (isset($id_cms) && !isset($id_cms_category)) ? $link->getCMSLink($id_cms, $infos['link_rewrite'], null, $infos['id_lang']) : $link->getCMSCategoryLink($id_cms_category, $infos['link_rewrite'], $infos['id_lang']); $default_rewrite[$infos['id_lang']] = $arr_link; } } $this->smarty->assign('lang_rewrite_urls', $default_rewrite); } return true; } 

The question is how to replace $ lang_rewrite_urls. $ Indice_lang or how to rewrite, so that the current page will be displayed Either score and call the current page through a new request

The output should receive:

 <link href="http://site.com/ru/" rel="alternate" hreflang="ru"> <link href="http://site.com/uk/" rel="alternate" hreflang="uk"> 

And displays only alternative (provided that the page http://site.com/ru/ ):

 <link href="http://site.com/uk/" rel="alternate" hreflang="uk"> 

thanks for the help

  • So, bright minds of programming, any suggestions? - Makar

3 answers 3

The easiest option is to do an additional check on the current url. in Smarty there is a variable $ smarty.get.page displays the full URL to the page.

 {foreach from=$languages key=k item=language name="languages"} {if $language.iso_code != $lang_iso} {assign var=indice_lang value=$language.id_lang} {if isset($lang_rewrite_urls.$indice_lang)} {if $lang_rewrite_urls.$indice_lang|escape:htmlall != $smarty.get.page } <link href="{$lang_rewrite_urls.$indice_lang|escape:htmlall}" rel="alternate" hreflang="{$language.iso_code|escape:'html':'UTF-8'}"> {else} <p>PAGE NAME</p> {/if} {else} {if $link->getLanguageLink($language.id_lang)|escape:htmlall != $smarty.get.page } <link href="{$link->getLanguageLink($language.id_lang)|escape:htmlall}" rel="alternate" hreflang="{$language.iso_code|escape:'html':'UTF-8'}"> {else} <p>PAGE NAME</p> {/if} {/if} {/if} {if $language.iso_code != $lang_iso} {/if} {/foreach} 

You can also try using $ smarty.server.REQUEST_URI instead of $ smarty.get.page - then the comparison will not be with the full URL, but part of it without the domain name

  • No, it does not work. All the same, displays only the <link href="{$link->getLanguageLink($language.id_lang)|escape:htmlall}" rel="alternate" hreflang="{$language.iso_code|escape:'html':'UTF-8'}"> - Makar
  • Thanks for the advice. Made such a crooked crutch - screwed the code <link href="http://{$smarty.server.HTTP_HOST}{$smarty.server.REQUEST_URI|escape:htmlall}" rel="alternate" hreflang="{$language.iso_code|escape:'html':'UTF-8'}"> - Makar

Please see the first one that Google provided on request, did it fit ?: https://www.prestashop.com/forums/topic/328680-hreflang-and-canonical-links-creation-on-prestashop-16/

  • In fact, it is, but in practice the situation is different. Presta has already laid all the code that is needed to generate the hreflang, only they did not make the conclusion. Here the guy just added / rewrote 1-2 lines of code and wants 15 euros. I see that some kind of variable is blocking the generation of this 1st line, but I cannot reach - which one - Makar

As a result, made such a crutch:

  <link href="http://{$smarty.server.HTTP_HOST}{$smarty.server.REQUEST_URI|escape:htmlall}" rel="alternate" hreflang="{$language.iso_code|escape:'html':'UTF-8'}"> /Генерация кода к текущей странице/ {foreach from=$languages key=k item=language name="languages"} {if $language.iso_code != $lang_iso} {assign var=indice_lang value=$language.id_lang} {if isset($lang_rewrite_urls.$indice_lang)} <link href="{$lang_rewrite_urls.$indice_lang|escape:htmlall}" rel="alternate" hreflang="{$language.iso_code|escape:'html':'UTF-8'}"> /здесь так и не работает ссылка текущей страницы — не генерируется/ {else} <link href="{$link->getLanguageLink($language.id_lang)|escape:htmlall}" rel="alternate" hreflang="{$language.iso_code|escape:'html':'UTF-8'}"> /вызов всех альтернативных страниц языков/ {/if} {/if} {if $language.iso_code != $lang_iso} {/if} {/foreach} 

I found this plugin, which more correctly displays Hreflang https://github.com/faktiva/prestashop-seo-tk