The hreflang & canonical URL features in the thirtybees core is a good feature, we were using some modules when were in prestashop. After migration we found its part of core and we dont need any module for canonical URLs / hreflang URLs. But found some pages have wrong URLs like cms pages and we found that the correct URLs are only generated for Product, Category, Manufacturer & Supplier pages. We have fixed the issue by overriding
override/classes/controller/FrontController.php
```
<?php
class FrontController extends FrontControllerCore
{
/**
* Generates html for additional seo tags.
*
* @return string html code for the new tags
*
* @since 1.0.0
*
* @version 1.0.0 Initial version
*/
public function getSeoFields()
{
$content = '';
$languages = Language::getLanguages();
$default_lang = Configuration::get('PS_LANG_DEFAULT');
switch ($this->php_self) {
case 'product': // product page
$id_product = (int) Tools::getValue('id_product');
$canonical = $this->context->link->getProductLink($id_product);
$hreflang = $this->getHrefLang('product', $id_product, $languages, $default_lang);
break;
case 'category':
$id_category = (int) Tools::getValue('id_category');
$content .= $this->getRelPrevNext('category', $id_category);
$canonical = $this->context->link->getCategoryLink((int) $id_category);
$hreflang = $this->getHrefLang('category', $id_category, $languages, $default_lang);
break;
case 'manufacturer':
$id_manufacturer = (int) Tools::getValue('id_manufacturer');
$content .= $this->getRelPrevNext('manufacturer', $id_manufacturer);
$hreflang = $this->getHrefLang('manufacturer', $id_manufacturer, $languages, $default_lang);
if (!$id_manufacturer) {
$canonical = $this->context->link->getPageLink('manufacturer');
} else {
$canonical = $this->context->link->getManufacturerLink($id_manufacturer);
}
break;
case 'supplier':
$id_supplier = (int) Tools::getValue('id_supplier');
$content .= $this->getRelPrevNext('supplier', $id_supplier);
$hreflang = $this->getHrefLang('supplier', $id_supplier, $languages, $default_lang);
if (!Tools::getValue('id_supplier')) {
$canonical = $this->context->link->getPageLink('supplier');
} else {
$canonical = $this->context->link->getSupplierLink((int) Tools::getValue('id_supplier'));
}
break;
case 'cms':
$id_cms = (int) Tools::getValue('id_cms');
$hreflang = $this->getHrefLang('cms', $id_cms, $languages, $default_lang);
if (!Tools::getValue('id_cms')) {
$canonical = $this->context->link->getPageLink('cms');
} else {
$canonical = $this->context->link->getCMSLink((int) Tools::getValue('id_cms'));
}
break;
default:
$canonical = $this->context->link->getPageLink($this->php_self);
$hreflang = $this->getHrefLang($this->php_self, 0, $languages, $default_lang);
break;
}
// build new content
$content .= '<link rel="canonical" href="'.$canonical.'">'."\n";
if ($hreflang) {
foreach ($hreflang as $lang) {
$content .= "$lang\n";
}
}
return $content;
}
/**
* creates hrefLang links for various entities.
*
* @param string $entity name of the object/page to get the link for
* @param int $idItem eventual id of the object (if any)
* @param array $languages list of languages
* @param int $idLangDefault id of the default language
*
* @return string html of the hreflang tags
*
* @since 1.0.0
*
* @version 1.0.0 Initial version
*/
public function getHrefLang($entity, $idItem, $languages, $idLangDefault)
{
foreach ($languages as $lang) {
switch ($entity) {
case 'product':
$lnk = $this->context->link->getProductLink((int) $idItem, null, null, null, $lang['id_lang']);
break;
case 'category':
$lnk = $this->context->link->getCategoryLink((int) $idItem, null, $lang['id_lang']);
break;
case 'manufacturer':
if (!$idItem) {
$lnk = $this->context->link->getPageLink('manufacturer', null, $lang['id_lang']);
} else {
$lnk = $this->context->link->getManufacturerLink((int) $idItem, null, $lang['id_lang']);
}
break;
case 'supplier':
if (!$idItem) {
$lnk = $this->context->link->getPageLink('supplier', null, $lang['id_lang']);
} else {
$lnk = $this->context->link->getSupplierLink((int) $idItem, null, $lang['id_lang']);
}
break;
case 'cms':
if (!$idItem) {
$lnk = $this->context->link->getPageLink('cms', null, $lang['id_lang']);
} else {
$lnk = $this->context->link->getCMSLink((int) $idItem, null, null, $lang['id_lang']);
}
break;
default:
$lnk = $this->context->link->getPageLink($entity, null, $lang['id_lang']);
break;
}
// append page number
if ($p = Tools::getValue('p')) {
$lnk .= "?p=$p";
}
$links[] = '<link rel="alternate" href="'.$lnk.'" hreflang="'.$lang['iso_code'].'">';
if ($lang['id_lang'] == $idLangDefault) {
$links[] = '<link rel="alternate" href="'.$lnk.'" hreflang="x-default">';
}
}
return $links;
}
}
```
Please note that we have added only the CMS page links, please suggest or modify the missing page's codes here and please consider to fix this in the next releases.
Question
Gofenice
The hreflang & canonical URL features in the thirtybees core is a good feature, we were using some modules when were in prestashop. After migration we found its part of core and we dont need any module for canonical URLs / hreflang URLs. But found some pages have wrong URLs like cms pages and we found that the correct URLs are only generated for Product, Category, Manufacturer & Supplier pages. We have fixed the issue by overriding
override/classes/controller/FrontController.php
``` <?php
class FrontController extends FrontControllerCore {
} ``` Please note that we have added only the CMS page links, please suggest or modify the missing page's codes here and please consider to fix this in the next releases.
Thanks,
3 answers to this question
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now