Tran Dinh Khanh
Tran Dinh Khanh

Reputation: 115

Magento - Cannot customize header block by custom module

First, I can easily change header template by

<reference name="header">
      <action method="setTemplate"><template>customizer/header.phtml</template></action>
</reference>

But, I want to add some functions to header block, so I try to extend:

class My_Customizer_Block_Header extends Mage_Page_Block_Html_Header

So I change my custom layout xml to

<reference name="header">
      <block type="customizer/header" name="header" as="header" template="customizer/header.phtml" />
</reference>

Now, $this->getTemplate() returns null, ($this->hasData('template') returns null as well)

Even if I change xml to

<reference name="header">
      <block type="customizer/header" name="header" as="header">
             <action method="setTemplate"><template>customizer/header.phtml</template></action>
      </block>
</reference>

it still doesn't work.

I also try to extend Mage_Core_Block_Template but it doesn't work.

I don't know why I cannot set custom template for header by custom module. Someone asked here but the answer is not clear enough to me.

Note:

Thank you.

Upvotes: 0

Views: 804

Answers (1)

Dharmen Gunawat
Dharmen Gunawat

Reputation: 46

You Can Just Create An Module For this Purpose

<?xml version="1.0"?>
<config>
    <modules>
        <My_Customheader>
          <version>0.1.0</version>
        </My_Customheader>
    </modules>
    <global>
       <helpers>
         <customheader>
            <class>My_Customheader_Helper</class>
         </customheader>
    </helpers>
    <blocks>
        <customheader>
           <class>My_Customheader_Block</class>
        </customheader>
    </blocks>
  </global>   
 </config>

Extend My_Customheader_Block_Html_Header extends Mage_Core_Block_Template

Create Directory and File in This Path app\code\local\My\Customheader\Block\Html\Header.php

<?php class My_Customheader_Block_Html_Header extends Mage_Core_Block_Template
{

 public function _construct()
{
  //change template path
    $this->setTemplate('customheader/html/header.phtml');
}

public function mycustomfunction(){
   //Write you logic here
 }

/**
 * Check if current url is url for home page
 *
 * @return true
 */
public function getIsHomePage()
{
    return $this->getUrl('') == $this->getUrl('*/*/*', array('_current'=>true, '_use_rewrite'=>true));
}

public function setLogo($logo_src, $logo_alt)
{
    $this->setLogoSrc($logo_src);
    $this->setLogoAlt($logo_alt);
    return $this;
}

public function getLogoSrc()
{
    if (empty($this->_data['logo_src'])) {
        $this->_data['logo_src'] = Mage::getStoreConfig('design/header/logo_src');
    }
    return $this->getSkinUrl($this->_data['logo_src']);
}

public function getLogoSrcSmall()
{
    if (empty($this->_data['logo_src_small'])) {
        $this->_data['logo_src_small'] = Mage::getStoreConfig('design/header/logo_src_small');
    }
    return $this->getSkinUrl($this->_data['logo_src_small']);
}

public function getLogoAlt()
{
    if (empty($this->_data['logo_alt'])) {
        $this->_data['logo_alt'] = Mage::getStoreConfig('design/header/logo_alt');
    }
    return $this->_data['logo_alt'];
}

/**
 * Retrieve page welcome message
 *
 * @deprecated after 1.7.0.2
 * @see Mage_Page_Block_Html_Welcome
 * @return mixed
 */
public function getWelcome()
{
    if (empty($this->_data['welcome'])) {
        if (Mage::isInstalled() && Mage::getSingleton('customer/session')->isLoggedIn()) {
            $this->_data['welcome'] = $this->__('Welcome, %s!', $this->escapeHtml(Mage::getSingleton('customer/session')->getCustomer()->getName()));
        } else {
            $this->_data['welcome'] = Mage::getStoreConfig('design/header/welcome');
        }
    }

    return $this->_data['welcome'];
}
}

Change in page.xml file for call block file at line 91 from

<block type="page/html_header" name="header" as="header">

to

<block type="customheader/html_header" name="header" as="header">

Create Directory and file app\design\frontend\yourtheme\default\template\customheader\html\header.phtml

Write Code in header.phtml file

Thanks Hope It will be helpfull for your

Upvotes: 1

Related Questions