Magento Tips

Design customisation for CMS page:

Adding custom CSS class name in the body tag
<reference name="root">
    <action method="addBodyClass">
        <classname>new_class_name</classname>
    </action>
</reference>
Remove a block
<reference name="area_name">
    <remove name="block_name" />
</reference>

area_name: is the name of the section in a page layout. For example left, footer, header, etc….
block_name: is the name of the block you want to remove. For example, breadcrumbs, topMenu, etc….

Change the logo src and alternative text
<reference name="header">
    <action method="setLogoSrc">
        <src>images/new-logo.gif</src>
    </action>
    <action method="setLogoAlt" translate="alt" module="catalog">
        <alt>New alternative text....</alt>
    </action>
</reference>
Adding a static block to a CMS page
{{block type="cms/block" block_id="block_id"}}

Customise a block

Lets say that you would like to show a lists of new products in the home page. Then you should add the following code in the CMS homepage content:

{{block type="catalog/product_new" name="home.catalog.product.new" alias="product_homepage" template="catalog/product/new.phtml"}}

Now, what if we want to limit the product list based on a category. Then we need to pass a category id to the block class. We can do this by changing the above code to:

{{block type="catalog/product_new" name="home.catalog.product.new"
           alias="product_homepage" template="catalog/product/new.phtml"  category_id="4"}}

Now we can show a list of all new products in category ID 4. The above code will not work as we still need to modify the default block class with our new changes.

Extending the block class

  1. Create new module configuration file in app/etc/modules/Prefix_Catalog.xml

    <?xml version="1.0"?>
    <config>
        <modules>
            <Prefix_Catalog>
                <active>true</active>
                <codePool>local</codePool>
            </Prefix_Catalog>
        </modules>
    </config>
    
  2. Create a folder named Prefix inside app/code/local
  3. Create a folder inside the Prefix folder named Catalog
  4. Create a folder inside Catalog named etc. Then inside it create an xml file named config.xml
    <?xml version="1.0"?>
    <config>
        <modules>
            <Prefix_Catalog>
                <version>0.1</version>
            </Prefix_Catalog>
        </modules>
        <global>
        	<blocks>
                  <catalog>
                    <rewrite>
                            <product_new>Prefix_Catalog_Block_Product_New</product_new>
                    </rewrite>
                </catalog>
            </blocks>
        </global>
    </config>
    </config>
    
  5. Create another folder inside Catalog named Block, then another folder inside Block named Product
  6. Create a php file that will contain the block class
    class Prefix_Catalog_Block_Product_New extends Mage_Catalog_Block_Product_New
    {
        protected function _beforeToHtml()
        {
            // if there are no category id provided then use the parent method
            $categoryId = $this->getCategoryId();
            if (empty($categoryId)) {
                return parent::_beforeToHtml();
            }
    
            $beforeHtml = parent::_beforeToHtml();
    
            // filter product collection by category id
            $category = Mage::getModel('catalog/category')->load($categoryId);
            $collection = $this->getProductCollection()->addCategoryFilter($category);
            $this->setProductCollection($collection);
    
    	return $beforeHtml;
        }
    
        public function getCategoryId()
        {
        	return (int)$this->getData('category_id');
        }
    }
    

    As you can see from the code above, you can retrieve the category id by using the method “getData”. Also, we have extended the core class and override the method “_beforeToHtml”. We can add extra code to check if we have a category id passed to the block. Then we filter the product collection by that category id.

    You can pass any number of parameters to the block.

    {{block type="catalog/product_new" name="home.catalog.product.new" alias="product_homepage" template="catalog/product/new.phtml"  category_id="4" max_price="20.00"}}
    
    

    To retrieve the new parameter:

    $this->getData('max_price');
    
    

Hope these tips helped.

Leave A Reply

Comments

No comments yet, be the first to add one!

Tags