Jump to content

variable meta title


ilario

Recommended Posts

public function hookdisplayPageTitle()
 {
  $page_title = '';
  $id_lang = (int)$this->context->language->id;
  $page = $this->context->smarty->tpl_vars['page']->value['page_name'];
  if (Tools::getIsset('id_category') && $page == 'category')
  {
   $id_cat = Tools::getValue('id_category');
   $category = new Category($id_cat);
   $page_title = $category->name[$id_lang];
   $page_title = explode(' ', $page_title);
  }
  else if (Tools::getIsset('id_product') && $page == 'product')
  {
   $id_prod = Tools::getValue('id_product');
   $product = new Product($id_prod);
   $page_title = $product->name[$id_lang];
   $page_title = explode(' ', $page_title);
  }
   
  else
   $page_title = $this->context->controller->$page->meta_title;
  
  $this->context->smarty->assign(
   array('page_title' => $page_title)
  );
  return $this->display(__FILE__, 'owlpagetitle.tpl');
 }

thanks for the answer,

but it works in half.

I attach the code to it, it works with all page - cms, but with other page, contact , cart, login etc. does not show the title

Link to comment
Share on other sites

ok. I've tested and this is what is working for me.
I think the best way to do it is like this
 
From a FrontController (MyController extends FronController)

$page_name = $this->getPageName();
$meta_tags = Meta::getMetaTags($this->context->language->id, $page_name); 
print_r($meta_tags); 

will print

Array
(
[meta_title] => Cart
[meta_description] =>
[meta_keywords] =>
)

I called this from CartController that's why the title is Cart
 
 

 

Second solution using Smarty vars

print_r($this->context->smarty->tpl_vars['page']);

will print

Smarty_Variable Object
(
[value] => Array
(
[title] =>
[canonical] =>
[meta] => Array
(
[title] => Cart
[description] =>
[keywords] =>
[robots] => index
)

[page_name] => cart
[body_classes] => Array
(
[lang-en] => 1
[lang-rtl] =>
[country-US] => 1
[currency-USD] => 1
[layout-full-width] => 1
[page-cart] => 1
[tax-display-disabled] => 1
)

[admin_notifications] => Array
(
)

)

[nocache] =>
[scope] => 0
)

If you ask me the first solution is much more clean because you're not forced to rely on context and  you can get meta information about other pages

 

Let me know if this is working

Link to comment
Share on other sites

the first solution does not work as an error, the second prints all the variables, but in English, can you add a language parameter?

Smarty_Variable Object ( [value] => Array ( [title] => [canonical] => [meta] => Array ( [title] => My account [description] => [keywords] => [robots] => index ) [page_name] => my-account [body_classes] => Array ( [lang-it] => 1 [lang-rtl] => [country-IT] => 1 [currency-EUR] => 1 [layout-full-width] => 1 [page-my-account] => 1 [tax-display-enabled] => 1 ) [admin_notifications] => Array ( ) ) [nocache] => [scope] => 0 )

thanks

Link to comment
Share on other sites

What error did the first code return?

The second code is based on $this->context and if the context is set for English then the English title will be returned. If you call the controller after you change the language it will show the title in that language?

 

 

Btw, from there did you execute the code?

Link to comment
Share on other sites

1) What error did the first code return?

first code freezes page load

 

 

 

 

2) The second code is based on $this->context and if the context is set for English then the English title will be returned. If you call the controller after you change the language it will show the title in that language?

 

​italiano  id2

 

<Btw, from there did you execute the code?

modulo this is code complete

 

 

<?php
/**
* 2007-2017 PrestaShop
*
* NOTICE OF LICENSE
*
* This source file is subject to the Academic Free License (AFL 3.0)
* that is bundled with this package in the file LICENSE.txt.
* It is also available through the world-wide-web at this URL:
* http://opensource.org/licenses/afl-3.0.php
* If you did not receive a copy of the license and are unable to
* obtain it through the world-wide-web, please send an email
* to [email protected] so we can send you a copy immediately.
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future. If you wish to customize PrestaShop for your
* needs please refer to http://www.prestashop.com for more information.
*
*  @author    PrestaShop SA <[email protected]>
*  @copyright 2007-2017 PrestaShop SA
*  @license   http://opensource.org/licenses/afl-3.0.php  Academic Free License (AFL 3.0)
*  International Registered Trademark & Property of PrestaShop SA
* 

*/
if (!defined('_CAN_LOAD_FILES_'))
 exit;
class OwlPagetitleoverride extends Module
{
 private $temp_url = '{owlpagetitle_url}';
 private $html;
 private $settings_default;
 private $wt_manu_config;
 private $config;
 public function __construct()
 {
  $this->name = 'owlpagetitle';
  $this->tab = 'front_office_features';
  $this->version = '1.0.0';
  $this->author = 'owltheme';
  $this->bootstrap = true;
  parent::__construct();
  $this->displayName = $this->trans('OWL Page Title');
  $this->description = $this->trans('Get page title');
 }
 public function install()
 {
  if (!parent::install() || !$this->registerHook('displayPageTitle'))
   return false;
  return true;
 }
 public function hookdisplayPageTitle()
 {
  $page_title = '';
  $id_lang = (int)$this->context->language->id;
  $page = $this->context->smarty->tpl_vars['page']->value['page_name'];
  if (Tools::getIsset('id_category') && $page == 'category')
  {
   $id_cat = Tools::getValue('id_category');
   $category = new Category($id_cat);
   $page_title = $category->name[$id_lang];
   $page_title = explode(' ', $page_title);
  }
  else if (Tools::getIsset('id_product') && $page == 'product')
  {
   $id_prod = Tools::getValue('id_product');
   $product = new Product($id_prod);
   $page_title = $product->name[$id_lang];
   $page_title = explode(' ', $page_title);
  }
  else 
​/******** here code for other page ***********/


  $this->context->smarty->assign(
   array('page_title' => $page_title)
  );
  return $this->display(__FILE__, 'owlpagetitle.tpl');
 }
}
Link to comment
Share on other sites

 public function hookdisplayPageTitle()
 {
  $page_title = '';
  $id_lang = (int)$this->context->language->id;
  $page = $this->context->smarty->tpl_vars['page']->value['page_name'];
  echo($id_lang." : "+$page);

see what $id_lang and $page prints? if the information is ok try

$meta_tags = Meta::getMetaTags($id_lang, $page);
Link to comment
Share on other sites

ok perfect

thank you very very much

 

$meta_tags = Meta::getMetaTags($id_lang, $page);
  print_r($meta_tags['meta_title']);

 

 

but I only have a small problem, I have three pages: contact-us cart my account, which I can not translate, even the title in the bar is always in English

Link to comment
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
×
×
  • Create New...