1

I want to set default meta title for product page,category page and for cms page. If I'm not setttin title in admin panel I want to set my default title. But I don't know how to make this. I try override getMetaTitle but this does not work for all titles, if I set title in panel admin too.

Do you have any hints?

3

Event

<controller_action_layout_generate_blocks_after>
   <observers>
       <change_category_page_title>
             <class><Module>/observer</class>
             <method>setMetaTitle</method>
       </change_category_page_title>
   </observers>
</controller_action_layout_generate_blocks_after>

Observer

public function setMetaTitle(Varien_Event_Observer $observer)
    {
        $head = $observer->getLayout()->getBlock('head');
        if($head->getTitle()=='')
        {
            if(Mage::registry('current_category'))
            {
                $head->setTitle('< default title for category >');
            }
            else if(Mage::registry('current_product')) 
            {
                $head->setTitle('< default title for product >');
            }
            else if($this->getRequest()->getModuleName()=='cms')
            {
                $head->setTitle('< default title for cms >');
            }
        }
    }
5
  • Thanks, this works! Now I need get this product and this category. I want to set title current product name, or category name. Any hints?
    – Miuzik
    Apr 5 '17 at 9:47
  • Ok, I find this. I must getName from Mage::register('current_product').
    – Miuzik
    Apr 5 '17 at 10:11
  • Yes @Miuzik if you find this answer helpful then mark it as accepted to close the question Apr 5 '17 at 10:29
  • I have another problem. When I set default title in panel admin this observer not change titles.
    – Miuzik
    Apr 5 '17 at 11:42
  • first check what you are getting in $head->getTitle() for title not added from admin. May be its not blank @Miuzik Apr 5 '17 at 12:13
0

I change method, now is ok. Thanks for help!

public function setMetaTitle(Varien_Event_Observer $observer)
{
    $head = $observer->getLayout()->getBlock('head');

        if($category = Mage::registry('current_category'))
        {
            if($category->getMetaTitle() == '') {
                $title = $category->getName();
                $head->setTitle($title);
            }

        }
        else if($product = Mage::registry('current_product'))
        {
            if($product->getMetaTitle() == '') {
                $collectionName = $product->getCollectionName();
                $productName = $product->getName();
                $title = $productName . ' ' . $collectionName;
                $head->setTitle($title);
            }
        }
    }

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.