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"