8/02/2010

create Magento blocks and add into layout

The block is called from app/code/local/my_pack/my_module/controllers/IndexController.php file.

public function indexAction()
{
//Get current layout state
$this->loadLayout();

$block = $this->getLayout()->createBlock(
'Mage_Core_Block_Template',
'your_block_name',
array('template' => 'my_pack/your_file_name.phtml')
);

$this->getLayout()->getBlock('content')->append($block);

$this->renderLayout();
}

7/20/2010

create custom module in Magento

Steps to create custom module in Magento :
1) Create directory app\code\local\mymodulepack. Here ‘mymodulepack’ will be the new directory folder where you can create modules.
2) Create module named ‘MyModule’ in above defined directory path. This will be your module directory which will contain required files like models, blocks, controllers etc.
3) Now add your new module xml file in app/etc/modules/ mymodulepack _All.xml. Consider that _All.xml will be used to add all new module definitions to the same file instead of cluttering the etc/modules folder with many files.
4) Following defined code is used to inform Magento that there is an active module (you can turn it off from here by setting ‘active’ to false). Also that it is located in the ‘local’ code pool.
<?xml version="1.0"?>
 <config>
   <modules>
     <mymodulepack_MyModule>
       <active>true</active>
       <codePool>local</codePool>
     </mymodulepack_MyModule>
    </modules>
</config>
5) Now you can enable or disable this module at Admin -> System -> Advanced. Here you can see your new module ‘Mymodulepack_MyModule’ listed there with status enable.
    Note : Please disable the cache from admin section to view the module.
6) And now create the block php class file at app/code/local/mymodulepack/MyModule/Block/Myview.php which contains a function that returns the string ‘Hello World’.
<?php
  class mymodulepack_MyModule_Block_Myview extends Mage_Core_Block_Template
{
    public function __construct()
    {
        parent::__construct();
        $this->setTemplate('mymodule/myview.phtml');      
    }
    protected function _prepareLayout()
    {
        parent::_prepareLayout();
    }
}
?>
7) Now  make a config.xml file in app\code\local\mymodulepack\MyModule\etc directory. This is done to enter configuration information for the module.
<?xml version="1.0" encoding="iso-8859-1"?>
<config>  
    <modules>
        <mymodulepack_MyModule>
            <version>0.1.0</version>
        </mymodulepack_MyModule>
    </modules>  
     <blocks>
          <MyModule>
               <rewrite>
                  <MyModule>mymodulepack_MyModule_Block_Myview</MyModule>
               </rewrite>
          </MyModule>
     </blocks>
    <frontend>
     <routers>
            <MyModule>
                <use>standard</use>
                <args>
                    <module>mymodulepack_MyModule</module>
                    <frontName>MyModule</frontName>
                </args>
            </MyModule>
        </routers>
        <layout>
            <updates>
                <MyModule>
                         <file>MyModule.xml</file>
                </MyModule>
            </updates>
           </layout>
    </frontend>
    <global>
        <helpers>
            <extras>
                <class>mymodulepack_MyModule_Helper_Data</class>
            </extras>
        </helpers>
    </global>
</config>
8) Create the php class file at app/code/local/mymodulepack/MyModule/ Helper / Data.php
<?PHP
class  mymodulepack_MyModule_Helper_Data extends Mage_Core_Helper_Abstract {
}
?>
9) Now we need to create a template file which uses the View Block Class to display the message “Hello World”. Create a template (phtml) called myview.phtml in the following directory:
       app/design/frontend/default/your theme/template/mymodule/myview.phtml.
<div>
<span style=" color:#0000FF; font:Arial, Helvetica, sans-serif;">
<bold><?php
echo "Hello World";
?></bold>
</span>
</div>
10)The module is now ready for use.
Where do I put this block? Should I create new mymodule.xml in theme/layout?
You can view the output by two ways.
1) {{block type=”mymodulepack_mymodule/view”  template=”mymodule/myview.phtml” }}
    call this block in page.xml file
2) Create xml file in app/design/frontend/default/your theme/layout/MyModule.xml
<?xml version="1.0"?>
<layout>
    <default>
       <reference name="content">
            <action method="setTemplate"><template>page/2columns-right.phtml</template></action>
        </reference>
        <reference name="content">
            <block type="core/template" name="mymodule" template="mymodule/myview.phtml" />
        </reference>
    </default>
</layout>
Note : Set the position of your module as per your requirement,
1.    <reference name="right">
2.    <reference name="left">
3.    <reference name="content">

7/16/2010

Override Navigation block

1.    Create mymodulepack  folder in local folder.

        Path:   C:\wamp\www\triangeli\app\code\local\mymodulepack

2.    Create catalog folder inside mymodulpack namespace.
3.    Create block, etc, helper folder inside Catalog folder.
4.    Create Navigation.php file in block folder. Override class and write functions inside this file.

<?php

class mymodulepack_Catalog_Block_Navigation extends Mage_Catalog_Block_Navigation

{
  //write the function here,which add new functionality or override the     original code
  public function override()
    {
      //write the code here
    } 
}

5.    Create config.xml file inside etc folder.

<?xml version="1.0" encoding="iso-8859-1"?>
<config>
  <modules>
    <mymodulepack_Catalog>
      <version>0.1.0</version>
    </mymodulepack_Catalog>
  </modules>
  <global>
    <blocks>
      <catalog>
        <rewrite>
          <class>mymodulepack_Catalog_Block</class>
        </rewrite>
      </catalog>
    </blocks>
    <helpers>
      <catalog>
        <rewrite>
          <class>mymodulepack_Catalog_Helper</class>
        </rewrite>
      </catalog>
    </helpers>
  </global>
</config>

6.    Create Data.php file inside Helper folder.

<?php
class mymodulepack_Catalog_Helper_Data extends Mage_Core_Helper_Abstract
{
  
}
?>
7.    To “activate” my new module “mymodulepack”:
In the file app\etc\local.xml reference mymodulepack under the global scope:
<config>
    <global>
        <blocks>
         <catalog>
           <rewrite>
              <navigation>mymodulepack_Catalog_Block_Navigation</navigation>
           </rewrite>
         </catalog>
        </blocks>
        <install>
            <date><![CDATA[Thu, 17 Dec 2009 11:50:52 +0000]]></date>
        </install>
        <crypt>
            <key><![CDATA[d89edae607842ce91b0e36456faed63e]]></key>
        </crypt>
        <disable_local_modules>false</disable_local_modules>
        <resources>
            <db>
                <table_prefix><![CDATA[]]></table_prefix>
            </db>
            <default_setup>
                <connection>
                    <host><![CDATA[localhost]]></host>
                    <username><![CDATA[root]]></username>
                    <password><![CDATA[]]></password>
                    <dbname><![CDATA[pizzaman]]></dbname>
                    <active>1</active>
                </connection>
            </default_setup>
        </resources>
        <session_save><![CDATA[files]]></session_save>
    </global>
    <admin>
        <routers>
            <adminhtml>
                <args>
                    <frontName><![CDATA[admin]]></frontName>
                </args>
            </adminhtml>
        </routers>
    </admin>
</config>

8.    Disable cache from admin section.

Magento's logging (Debugging)

The output of  Logs written to the var/log/system.log file, which also includes the rest of the system logging output. Logging must be enabled for output to be written to this file.
You can enable logging in the Magento admin panel as,
System -> Configuration - > Developer -> Log Settings and setting 'Enabled' to True.

eg. Mage :: log("I am inside the function");

Mage::log Access the system logger
Mage::getModel Access a system model object
Mage::getStoreConfig Access the system configuration settings


6/22/2010

Override core block in magento

The simplest way to override a core block is to simply copy the file into the corresponding /app/code/local/ folder, and Magento automatically uses that version of the file without an XML/config changes needed.

So just copy /app/code/core/Mage/Checkout/Block/Cart/Item/Renderer.php into /app/code/local/Mage/Checkout/Block/Cart/Item/ and be sure to refresh the cache.

MVC pattern of Magento files & folder structure

/ app - is where the application resides

/ app / etc - is global configuration den

/ app / code - is where modules have installed their models and controllers

/ app / code / core - are core team developed or certified modules

/ app / code / community - are community contributed modules

/ app / code / local - are local customizations

/ app / code / core / Mage - magento default namespace

/ app / code / core / Mage / {Module} - module root

/ app / code / core / Mage / {Module} / etc - module configuration

/ app / code / core / Mage / {Module} / controllers - controllers provided by module

/ app / code / core / Mage / {Module} / Block - Block logic classes

/ app / code / core / Mage / {Module} / Model - Object Models provided by module

/ app / code / core / Mage / {Module} / Model / Mysql4 - Resource Models provided by module

/ app / code / core / Mage / {Module} / sql - sql installation and upgrade files between module versions

/ app / code / core / Mage / {Module} / sql / {resource} / - resource model specific upgrades

/ app / code / core / Mage / {Module} / sql / {resource} / {type}-{action}-{versions}.(sql|php) - resource update files. example: mysql4-upgrade-0.6.23-0.6.25.sql

/ app / design - is location of design packages (layouts, templates, translations)

/ app / design / frontend - frontend design

/ app / design / adminhtml - HTML admin panel design

/ app / design / {area} / {package} / {theme} - theme customizations

/ app / design / {area} / {package} / {theme} / layout - .xml files that define block structure for different cases in website flow

/ app / design / {area} / {package} / {theme} / template - .phtml (html with php tags) templates

/ app / design / {area} / {package} / {theme} / locale - Zend_Translate compatible translation files for package/theme

/ app / locale - locale files

/ app / locale / {locale (en_US)} - Zend_Translate compatible translation files for modules

/ skin / {area} / {package} / {theme} / - is where design package css and images are

/ lib - are libraries such as Zend and Varien

/ js - are javascripts smile

/ media - uploaded files (product images, pdf documents, etc)

/ tests - Unit tests (not done yet)

/ var - temporary files

/ includes - contains config.php