2

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"

  • I Don't understand what are you trying to do, by default Magento adds page title based on current page – Piyush Feb 8 '18 at 5:34
  • 1
    I trying to set different titles for categories, subcategories and poducts. by defeault Magento, probably, do this, but in real case I see only for some of products page titles equal to their names. For most of them titles set to category and subcategory. Probably, problem in way, what used to add products in magento – Kirill K Feb 8 '18 at 5:43
0

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
}
  • this version gave same results. Probably, here need to add some filtration to get exactly product name, not meta-title. Checked on product, which have no added meta-title info - all changes, made in product/view.php have no affect, magento return only parent category and subcategory of this product, not his actual name – Kirill K Feb 8 '18 at 6:58
  • did you added else statement in this? I have tested it and it is working when product does not have meta title – Piyush Feb 8 '18 at 7:00
  • $category = $this->getCurrentCategory(); $parentCategory = Mage::getModel('catalog/category')->load($category->getParentId()); $product = Mage::registry('current_product'); if (!$product) {if($parentCategory) { $headBlock->setTitle('<title>' . $parentCategory->getName() . ' ' . $category->getName() . ' ' . '</title>'); } else { $headBlock->setTitle('<title>' . $category->getName() . '</title>');} for category – Kirill K Feb 8 '18 at 7:04
  • and $product = Mage::registry('current_product'); if ($product) { $headBlock->setTitle('<title>' . Mage::getStoreConfig('design/head/title_prefix') . ' ' . $product->getName() . ' ' . Mage::getStoreConfig('design/head/title_suffix') . '</title>'); } for product – Kirill K Feb 8 '18 at 7:04
  • You don't have to put your whole custom code in it, just need to add an else statement , please refer my updated answer – Piyush Feb 8 '18 at 7:08
0

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>
0

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.

  • probably upd section of question can help to determine, what I'm doing wrong. – Kirill K Feb 8 '18 at 5:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.