Quote

Create a new module in magento

Module Declaration:
Create app/etc/modules/M4U_NewModule.xml and write below code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
<?xml version="1.0"?>
<config>
         <modules>
<!-- declare CompanyName_NewModule module -->
           <M4U_NewModule>
<!-- this is an active module -->
                  <active>true</active>
<!-- this module will be located in app/code/local code pool -->
                  <codePool>local</codePool>
<!-- specify dependencies for correct module loading order -->
                  <depends>
                             <Mage_ExistingModule />
                  </depends>
<!-- declare module's version information for database updates -->
                  <version>0.1.0</version>
           </M4U_NewModule>
    </modules>
</config>

Here is example of create new M4U_customer module to override existing Mage_Customer module and M4U_Credit module to create new credit module.

1
2
3
4
5
6
7
8
9
10
11
12
<config>
    <modules>
        <M4U_Customer>
           <active>true</active>
           <codePool>local</codePool>
        </M4U_Customer>
        <M4U_Credit>
           <active>true</active>
           <codePool>local</codePool>
        </M4U_Credit>
    </modules>
</config>

Our new module will be called NewModule.

Replace all instances of ‘NewModule’ with name of your module and ‘newmodule’ with simplified code, that contains only alphanumeric characters and underscore.

To make this tutorial most concise, it’s implied that mentioned folders will be created when needed.

Module Configuration:

Create Model, Helper, coltrollers, Block, sql and etc folder under app/code/local/M4U/NewModule/ and configure app/code/local/M4U/NewModule/etc/config.xml like below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?xml version="1.0"?>
   <config>
       <modules>
           <M4U_NewModule>
                 <version>0.1.0</version>
           </M4U_NewModule>
       </modules>
<!-- define Front end controller and template  -->
       <frontend>
<!-- define Front end controller  -->
           <routers>
               <newmodule>
                   <use>standard</use>
                   <args>
                          <module>M4U_NewModule</module>
                          <frontName>newmodule</frontName>
                   </args>
               </newmodule>
           </routers>
<!-- define Front end template xml -->
           <layout>
               <updates>
                    <newmodule>
                         <file>newmodule.xml</file>
                    </newmodule>
                </updates>
              </layout>
           </frontend>  
<!-- define blocks, models, helpers in global area -->
            <global>
               <models>
                  <newmodule>
                         <class>M4U_NewModule_Model</class>
                  </newmodule>
               </models>
               <blocks>
                  <newmodule>
                          <class>M4U_NewModule_Block</class>
                  </newmodule>
               </blocks>
               <helpers>
                  <newmodule>
                          <class>M4U_NewModule_Helper</class>
                  </newmodule>
               </helpers>
<!-- declare resource setup for new module -->
               <resources>
                    <newmodule_setup>
<!-- specify that this resource is a setup resource and used for upgrades -->
                       <setup>
<!-- which module to look for install/upgrade files in -->
                           <module>M4U_NewModule</module>
                       </setup>
<!-- specify database connection for this resource -->
                       <connection>
<!-- do not create new connection, use predefined core setup connection -->
                           <use>core_setup</use>
                       </connection>
                    </newmodule_setup>
                    <newmodule_write>
                         <connection>
                              <use>core_write</use>
                         </connection>
                    </newmodule_write>
                    <newmodule_read>
                         <connection>
                               <use>core_read</use>
                         </connection>
                    </newmodule_read>
                </resources>
           </global>
      </config>

Adapter Class:
To obtain module functionality you can write necessary method in various classes as magento convention.
1. Create necessary model class in app/code/local/M4U/NewModule/Model/
2. Create Block in app/code/local/M4U/NewModule/Block/
3. Create Helper in app/code/local/M4U/NewModule/Helper/
4. Place necessary updated database sql file in app/code/local/M4U/NewModule/sql/

Frontend Template Define:

1. Define page layout in app/design/frontend/M4U/default/layout/NewModule.xml

1
2
3
4
5
6
7
8
9
10
11
12
<?xml version="1.0"?>
   <layout version="0.1.0">
     <newmodule_index_usercredit>
        <reference name="root">
            <action method="setTemplate"><template>page/1column.phtml</template></action>
        </reference>
        <update handle="customer_account"/>
        <reference name="content">
            <block type="newmodule/blockClassName" name="usercredit" template="newmodule/usercredit.phtml"/>
        </reference>
     </newmodule_index_usercredit>
</layout>

2. Create template file app/design/frontend/M4U/default/template/newmodule/usercredit.phtml

Troubleshooting

  • Dont put your module in /Mage. It belongs in app/code/community/ or app/code/local/
  • Make sure your module’s first letter is capitlized. newmodule apparently will not work, it must start with a capital letter Newmodule.
  • If your module is not showing in configuration>advanced then check your config.xml
  • Make sure you clear the cache.

In this post i described general module customization theory of Magento. For more clarification you can see next post  “write HelloWord module” in this blog.

Create new module “HelloWorld” – in Magento

Display Top Level Category of Current Product

                     <!-- Display Top Category of Current Product -->
                        <?php
                        $category =Mage::registry('current_category');
                        $it =10;//Amount of iterations before script gives up    
                        if($category){
                                while($category->getLevel()!=2&& $it >0){
                                        $category = $category->getParentCategory();
                                        if(!$category){
                                                break;
                                        }
                                }
                                if($category){
                                        echo 'Top Level Category : <b>'.$category->getName().'</b>';
                                }
                                else{
                                        echo 'Cannot find parent category';
                                }
                        } 
                        ?>

                        <!-- End Display Top Category of Current Product -->

http://tny.cz/37a5a802

Show top level categories in magento

<?php
$obj = new Mage_Catalog_Block_Navigation();
$store_cats = $obj->getStoreCategories();
$current_cat  = $obj->getCurrentCategory();
$current_cat = (is_object($current_cat) ? $current_cat->getName() : '');

foreach ($store_cats as $cat) {
 if ($cat->getName() == $current_cat) {
  echo '<li><a href="'.$this->getCategoryUrl($cat).'">'.$cat->getName()."</a>\n<ul>\n";
  foreach ($obj->getCurrentChildCategories() as $subcat) {
   echo '<li><a href="'.$this->getCategoryUrl($subcat).'">'.$subcat->getName()."</a></li>\n";
  }
  echo "</ul>\n</li>\n";
 } else {
  echo '<li><a href="'.$this->getCategoryUrl($cat).'">'.$cat->getName()."</a></li>\n";
 }
}
?>

http://tny.cz/98479cb3

Display Top Level Categories and Current Categories SubCategories

<?php
/*
 * http://fishpig.co.uk - Magento Tutorials
 *
 * Display top level categories and
 * subcategories of the current category
 *
**/
?>
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
    <ul>
        <?php foreach($_categories as $_category): ?>
            <li>
                <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
                    <?php echo $_category->getName() ?>
                </a>
                <?php if ($currentCategory &amp;&amp; $currentCategory->getId() == $_category->getId()): ?>
                    <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
                    <?php $_subcategories = $_category->getChildrenCategories() ?>
                    <?php if (count($_subcategories) > 0): ?>
                        <ul>
                            <?php foreach($_subcategories as $_subcategory): ?>
                                <li>
                                    <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
                                        <?php echo $_subcategory->getName() ?>
                                    </a>
                                </li>
                            <?php endforeach; ?>
                        </ul>
                    <?php endif; ?>
                <?php endif; ?>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

Display Top Level Categories and ALL Subcategories

<?php
/*
 * http://fishpig.co.uk - Magento Tutorials
 *
 * Display top level categories and subcategories
 *
**/
?>
<?php $_helper = Mage::helper('catalog/category') ?>
<?php $_categories = $_helper->getStoreCategories() ?>
<?php $currentCategory = Mage::registry('current_category') ?>
<?php if (count($_categories) > 0): ?>
    <ul>
        <?php foreach($_categories as $_category): ?>
            <li>
                <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
                    <?php echo $_category->getName() ?>
                </a>
                <?php $_category = Mage::getModel('catalog/category')->load($_category->getId()) ?>
                <?php $_subcategories = $_category->getChildrenCategories() ?>
                <?php if (count($_subcategories) > 0): ?>
                    <ul>
                        <?php foreach($_subcategories as $_subcategory): ?>
                            <li>
                                <a href="<?php echo $_helper->getCategoryUrl($_subcategory) ?>">
                                    <?php echo $_subcategory->getName() ?>
                                </a>
                            </li>
                        <?php endforeach; ?>
                    </ul>
                <?php endif; ?>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

Display Top Level Categories Only

<?php
/*
 *
 * Display top level categories
 *
 * This code is compatible with 1.3 and up
 *
**/
?>
<?php $_helper       = Mage::helper('catalog/category') ?>
<?php $_categories   = $_helper->getStoreCategories() ?>
<?php if (count($_categories) > 0): ?>
    <ul class="category">
        <?php foreach($_categories as $_category): ?>
            <li>
                <a href="<?php echo $_helper->getCategoryUrl($_category) ?>">
                    <?php echo $_category->getName() ?>
                </a>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

Magento – SEO Friendly Breadcrumbs

When a product page is visited directly rather than navigating to it, Magento does not show breadcrumbs.

For example, if you copy a product URL, close your browser, open browser and paste the URL back into the address bar there’s NO BREADCRUMBS.

This is an issue as breadcrumbs can be extremely helpful for SEO and can also be helpful for customers navigating your site.

SOLUTION:
To show the breadcrumbs of a product page even if it is visited directly, copy the below code into the top of your breadcrumbs.phtml file (app/design/default/default/template_name/templates/page/html/breadcrumb.phtml)
<?php
if ($product = Mage::registry(‘current_product’)) {
$categories = $product->getCategoryCollection()->load();

if($categories) {
foreach ($categories as $category)
{
if($category) {
$category = Mage::getModel(‘catalog/category’)->load($category->getId());
break;
}
}
}
$lastCrumbName = $product->getName();
$lastCategoryAdjust = 0;
}
else {
if($category = Mage::registry(‘current_category’)) {
$lastCrumbName = $category->getName();
}
$lastCategoryAdjust = 1;
}
if($category) {
if($path = $category->getPath()) {
$path = explode(‘/’, $path);
$crumbs = array(‘home’ => array(‘label’ => ‘Home’,
‘title’ => ‘Home’,
‘link’ => Mage::getBaseUrl(Mage_Core_Model_Store::URL_TYPE_WEB),
‘first’ => true,
‘last’ => false
));
for($i = 2; $i < count($path) – $lastCategoryAdjust; $i++) {
$cur_category = Mage::getModel(‘catalog/category’)->load($path[$i]);
if($cur_category && $cur_category->getIsActive()) {
$crumbs[‘category’ . $path[$i]] = array(‘label’ => $cur_category->getName(),
‘title’ => $cur_category->getName(),
‘link’ => $cur_category->getUrl(),
‘first’ => false,
‘last’ => false
);
}
}
$crumbs[‘current’] = array(‘label’ => $lastCrumbName,
‘title’ => ”,
‘link’ => ”,
‘first’ => false,
‘last’ => true
);
}
}
if($crumbs && is_array($crumbs)): ?>
<div class=”breadcrumbs”>
<ul>
<?php foreach($crumbs as $_crumbName=>$_crumbInfo): ?>
<li class=”<?php echo $_crumbName ?>”>
<?php if($_crumbInfo[‘link’]): ?>
<a href=”<?php echo $_crumbInfo[‘link’] ?>” title=”<?php echo $this->escapeHtml($_crumbInfo[‘title’]) ?>”><?php echo $this->escapeHtml($_crumbInfo[‘label’]) ?></a>
<?php elseif($_crumbInfo[‘last’]): ?>
<strong><?php echo $this->escapeHtml($_crumbInfo[‘label’]) ?></strong>
<?php else: ?>
<?php echo $this->escapeHtml($_crumbInfo[‘label’]) ?>
<?php endif; ?>
<?php if(!$_crumbInfo[‘last’]): ?>
<span>/ </span>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>

http://tny.cz/c8b823b2

http://igozz.blogspot.in/2011/03/magento-seo-friendly-breadcrumbs.html

Add Tabbing to Magento Default

This is a repost from my other blog which doesn’t exist any longer. I am keeping it here for reference.

Tab view in Magento Default
Tab view in Magento Default

Earlier this week, someone asked me about how to put tabbed view into Default Theme. As tabbed view doesn’t exist in the Default Theme, it means we have to put the tab view logic and template files inside the Default Theme’s layout and skin folder. As a note, before we start making changes, please remember to backup those folder that we’re going to work on (template, layout and skin) so we can revert them back when something goes wrong.

Here are the steps:

  1. Paste the following block of template code into Default Theme’s product view.phtml, right before the closing div, located in “app/design/frontend/default/default/template/catalog/product/view.phtml”:
    <?php echo $this->getChildHtml(‘info_tabs’); ?>

    and remove the following template logic block within the same file:

    <div class=“product-collateral”>
        <?php if ($_description = $this->getChildHtml(‘description’)):?>
            <div class=“collateral-box”>
                <div class=“head”>
                    <h4><?php echo $this->__(‘Product Description’) ?></h4>
                </div>
                <?php echo $_description ?>
            </div>
        <?php endif;?>
        <?php if ($_additional = $this->getChildHtml(‘additional’)):?>
            <div class=“collateral-box”>
                <div class=“head”>
                    <h4><?php echo $this->__(‘Additional Information’) ?></h4>
                </div>
                <?php echo $_additional ?>
            </div>
        <?php endif;?>
        <?php echo $this->getChildHtml(‘upsell_products’) ?>
        <?php echo $this->getChildHtml(‘product_additional_data’) ?>
    </div>
  2. Remove files and folders under “app/design/frontend/default/default/template/catalog/product/view” then copy back from “app/design/frontend/default/modern/template/catalog/product/view” into that folder.
  3. Paste the following block of layout tags into Default Theme’s catalog.xml, located in “app/design/frontend/default/default/layout/catalog.xml” within <catalog_product_view>, right below “<block type=”catalog/product_view” name=”product.info.addtocart” as=”addtocart” template=”catalog/product/view/addtocart.phtml”/>”
    <block type=“catalog/product_view_tabs” name=“product.info.tabs” as=“info_tabs” template=“catalog/product/view/tabs.phtml” >
        <action method=“addTab” translate=“title” module=“catalog”><alias>description</alias><title>Product Description</title><block>catalog/product_view_description</block><template>catalog/product/view/description.phtml</template></action>
        <action method=“addTab” translate=“title” module=“catalog”><alias>upsell_products</alias><title>We Also Recommend</title><block>catalog/product_list_upsell</block><template>catalog/product/list/upsell.phtml</template></action>
        <action method=“addTab” translate=“title” module=“catalog”><alias>additional</alias><title>Additional Information</title><block>catalog/product_view_attributes</block><template>catalog/product/view/attributes.phtml</template></action>
    </block>
  4. Paste the following block of layout tags into Default Theme’s tag.xml, located in app/design/frontend/default/default/layout/tag.xml
    <reference name=“product.info.tabs”>
        <action method=“addTab” translate=“title” module=“tag”><alias>tags</alias><title>Product Tags</title><block>tag/product_list</block><template>tag/list.phtml</template></action>
    </reference>

    within <catalog_product_view>, right below the following tags:

    <reference name=“product.info.additional”>
        <block type=“tag/product_list” name=“product_tag_list” before=“-“ template=“tag/list.phtml”></block>
    </reference>
  5. Now put some styling for the tabs, paste the following CSS to the end of boxes.css file located at “skin/frontend/default/default/css/boxes.css”
    /********************** Tabs */
    .tabs { margin-bottom:15px; border-bottom:1px solid #666; background: url(‘/joomlavue/../images/tabs_bg.gif’) repeat-x 0 100% #f2f2f2; }
    .tabs li { float:left; border-right:1px solid #a4a4a4; border-left:1px solid #fff; font-size:1.1em; line-height:1em; }
    .tabs li.first { border-left:0; }
    .tabs li.last { border-right:0; }
    .tabs a { display:block; padding:6px 15px; color:#444; }
    .tabs a:hover { background-color:#ddd; text-decoration:none; color:#444; }
    .tabs li.active a,
    .tabs li.active a:hover { background-color:#666; font-weight:bold; color:#fff; }
    /* Style */
    .outline-creator { border:1px solid #bbb; border-bottom-color:#666; background:#fff; }
    .col-main .padder{ padding-right:20px; }
    .layout-3columns .padder { padding:0; }
  6. Last step is to copy the background image from “skin/frontend/default/modern/images/tabs_bg.gif” into “skin/frontend/default/default/images” and you’re done.