Three approaches to removing blocks in Magento

Blocks in Magento

Blocks are a way by which Magento organizes both visual and functional features in Magento, and creates a modular way to manage these features. When you set up a store there are likely certain blocks that you will want to remove, add or re-position. Some blocks can be turned off and on in administration console. Others will likely require code changes.

Get rid of the callout dog

The typing callout dog is often one of the first thing to go when building a real store.

The first thing we want do is enable Template Path Hints

Once Template Path Hints are enabled, we can go back the front page and we will see that the block name hint indicates that the template that generates this block can be found in template file frontend/base/default/template/callouts/left_col.phtml. Refer to the image at left with template file name and location shown in white letters on red background.

Next we have to discover which layout file(s) use the left_col.phtml template file. Here we can use a tool like grep in Linux or WinGrep for Windows to scan the layout files.

Either way, we will discover that the layout file that references left_col.phtml can be found in app\design\frontend\base\default\layout\catalog.xml

Creating a custom theme in Magento

Generally, we don't want to make changes in vanilla Magento code. That is, we don't want to modify code provided by Magento. It is safer to put modified code in your custom theme location. This allows us to override vanilla Magento templates and layouts. Creating a custom theme makes upgrades simpler and allows us to revert back to default look and behavior when needed. Creating a custom theme is beyond the scope of this article, however there are good explanations of the process at www.fishpig.co.uk and magentocommerce.com If you have created a custom theme called mytheme, you will want to copy these files.

\app\design\frontend\base\default\layout\catalog.xml to
\app\design\frontend\default\mytheme\layout\catalog.xml
        and
\app\design\frontend\default\mytheme\template\callouts\left_col.phtml to
\app\design\frontend\base\default\template\callouts\left_col.phtml

before you make any changes. For the rest of the article I will refer to files in the core Magento locations.

Back to the Dog

Now we will turn our attention back to removing the dog callout block from the left column.

Option One – remove content from left_col.phtml

This is is the brute force option. We can simply remove the block with the dog by removing the following lines in /app/design/frontend/base/default/template/callouts/left_col.phtml (or in your custom theme) that generate the callout block.

<div>
    <div>
        <?php if(strtolower(substr($this->getLinkUrl(),0,4))==='http'): ?>
            <a href="<?php echo $this->getLinkUrl() ?>" title="<?php echo $this->__($this->getImgAlt()) ?>">
        <?php elseif($this->getLinkUrl()): ?>
            <a href="<?php echo $this->getUrl($this->getLinkUrl()) ?>" title="<?php echo $this->__($this->getImgAlt()) ?>">
        <?php endif; ?>
            <img src="<?php echo $this->getSkinUrl($this->getImgSrc()) ?>"<?php if(!$this->getLinkUrl()): ?> title="<?php echo $this->__($this->getImgAlt()) ?>"<?php endif; ?> alt="<?php echo $this->__($this->getImgAlt()) ?>" />
        <?php if($this->getLinkUrl()): ?>
        </a>
        <?php endif ?>
    </div>
</div>

Template files are generally small snippets of regular html and php code that generate visual layout for your site. They are re-usable in that they can be called from different layout files. Although effective, deleting unwanted code is not the preferred method.

Option Two – modifying the layout file

A second and better option is to remove references to left_col.phtml from the layout file. We have already discovered that the layout file that references left_col.phtml is app\design\frontend\base\default\layout\catalog.xml. If we open this file, we will find the following code close to the top of the file.

        <!-- <reference name="left">
            <block type="core/template" name="left.permanent.callout" template="callouts/left_col.phtml">
                <action method="setImgSrc"><src>images/media/col_left_callout.jpg</src></action>
                <action method="setImgAlt" translate="alt" module="catalog"><alt>Our customer service is available 24/7. Call us at (555) 555-0123.</alt></action>
                <action method="setLinkUrl"><url>checkout/cart</url></action>
            </block>
        </reference>  -->

We have used the XML <!- – comment - -> syntax to comment out the code from the catalog.xml file.

Option Three – the magic local.xml file

Magento offers us another option to add and remove code on the page. This gives us the ability to alter layout files without actually modifying any existing files. We can use the local.xml file to reference and remove blocks added in other layout files. This file is placed in the app\design\frontend\base\default\layout\local.xml. If a local.xml file is found, it is loaded last. This feature permits us to affect the default layout of a theme without having to change the theme’s layout XML files. This also allows us to keep much of our custom layout changes in one place for easy reference.

<?xml version="1.0" encoding="UTF-8"?>
  <layout>
    <default>    
    <!-- Remove callouts -->
    <reference name="left">
        <remove name="left.permanent.callout"/>        
   </reference>
    </default>
</layout>