Magento 1.9. Need help with product name
-
29-12-2020 - |
Pregunta
I'm trying to get product name in title tag on catalog-product-view presentation in page/block/html/head.php file. Tried both this:
$product = Mage::registry('current_product');
if($product && $product->getId()){
$title = Mage::getStoreConfig('design/head/title_prefix') . ' ' . $product->getName() . ' ' . Mage::getStoreConfig('design/head/title_suffix');
}
and this
$currentUrl = Mage::helper('core/url')->getCurrentUrl();
$url = Mage::getSingleton('core/url')->parseUrl($currentUrl);
$path = $url->getPath();
$oRewrite = Mage::getModel('core/url_rewrite')->setStoreId(Mage::app()->getStore()->getId())
->loadByRequestPath(ltrim($path,'/'));
$pId = $oRewrite->getProductId();
if($pId > 0) {
$title = Mage::getStoreConfig('design/head/title_prefix') . ' ' . $pId . ' ' . Mage::getStoreConfig('design/head/title_suffix');
}
both not working. Except this I've another modificators for title tag such as category or (for home page) default title.
Any help or explanation, why listed above snippets didn't work will be appreceated. Thanks in advance.
UPD:
Moved all logic into themename/template/page/html/head.phtml with following rules:
<?php
$routeName = Mage::app()->getRequest()->getRouteName();
$identifier = Mage::getSingleton('cms/page')->getIdentifier();
$category = Mage::registry('current_category');
$parentCategory = Mage::getModel('catalog/category')->load($category->getParentId());
$product = Mage::registry('current_product');
if ($category && !$product) {
if($parentCategory !== "Default Category") {
echo '<title>' . Mage::getStoreConfig('design/head/title_prefix') . ' ' . $parentCategory->getName() . ' ' . $category->getName() . ' ' . Mage::getStoreConfig('design/head/title_suffix') . '</title>';
} else {
echo '<title>' . Mage::getStoreConfig('design/head/title_prefix') . ' ' . $category->getName() . ' ' . Mage::getStoreConfig('design/head/title_suffix') . '</title>';
}
}
if($product) {
echo '<title>' . Mage::getStoreConfig('design/head/title_prefix') . ' ' . $product->getName() . ' ' . Mage::getStoreConfig('design/head/title_suffix') . '</title>';
}
if($routeName == 'cms' && $identifier == 'home') {
echo '<title>' . $this->getTitle() . '</title>';
}
?>
But still at catalog-product-view page get only category title.
UPD2: Probably here is another possible way to get result I need - sql query to massive update products meta-title? Final result I need to get is following string for categories: "default shop name" + "|" + "parent category name" + "category/brand name"
and for products "default shop name" + "|" + "product name"
Solución
As you want to change title for only categories and product pages, then instead of /app/code/core/Mage/Page/Block/Html/Head.php
you should override below files
app/code/core/Mage/Catalog/Block/Category/View.php app/code/core/Mage/Catalog/Block/Product/View.php
In this file probably you may need to add an else
statement to
if ($title) {
$headBlock->setTitle($title);
}
Under _prepareLayout
layout function like below
if ($title) {
$headBlock->setTitle($title);
}
else
{
$headBlock->setTitle('Title New '. $product->getName()); //you can change value in setTitle() according to your needs
}
Otros consejos
Update the title in head.phtml (/app/design/frontend/default/Your-theme/template/page/html/head.phtml)
add
<?php if($product = Mage::registry('current_product')){
$metaTitle = $product->getName();
}
else{
$metaTitle = $this->getTitle();
}
?>
replace
<title><?php echo $this->getTitle() ?></title>
with
<title><?php echo $metaTitle ?></title>
Also you can try below:-
extend below file:
/app/code/core/Mage/Page/Block/Html/Head.php
in local folder:-
/app/code/local/Mage/Page/Block/Html/Head.php
and use below code according you logic
public function setTitle($title)
{
if($current_product = Mage::registry('current_product')){
$proName = $current_product->getName();
}
if($proName){
$this->_data['title'] = Mage::getStoreConfig('design/head/title_prefix') . ' ' . $proName
. ' ' . Mage::getStoreConfig('design/head/title_suffix');
} else {
$this->_data['title'] = Mage::getStoreConfig('design/head/title_prefix') . ' ' . $title
. ' ' . Mage::getStoreConfig('design/head/title_suffix');
}
return $this;
}
Hope that may help you.