The Basics Of Creating A Magento Module

Advertisement

A lot of community extensions (or modules) are available for the feature-rich open-source e-commerce solution Magento1, but what if they don’t quite work as you want them to? What if you could understand the structure of a Magento module a little better, to the point that you could modify it to suit your needs or, better yet, write your own module from scratch?

In this tutorial, we will introduce the coding of Magento in the form of a “Hello World”-style module. The goal of the module will be simply to write some information to a log file every time a product is saved. This very basic module will allow us to cover a number of interesting topics, including:

  • The app/code directories,
  • The structure and creation of a Magento module,
  • Event observers,
  • Logging.

Before We Begin

This tutorial assumes that you already have an installation of Magento up and running, either locally or on a development server, that you can add new files to. The version of Magento that you use doesn’t really matter, because we will be covering fundamental aspects that exist across all versions and editions: Community, Professional and Enterprise.

Disable the Cache

This is one of the first lessons a Magento developer should learn: disable the cache! You can do this by going to Admin Panel > System > Cache Management > Select All > Actions: Disable > Submit.

While very good at boosting performance in a production environment, the cache is a developer’s enemy. Leave it enabled at your peril! Every Magento developer I have met has on more than one occasion spent an hour or so wondering why their latest update is not showing up, only to find that Magento is still displaying the version of the website that it conveniently cached earlier that day.

The app/code Directory

The brains of Magento can be found in individual modules inside the app/code directory, which is split in to three areas: core, community and local.

Core

The app/code/core directory contains all of the functionality for products, categories, customers, payments, etc. Until you know what you are doing (and even afterwards), keep app/code/core off limits because these files should not be modified.

Magento is structured in such a way that you can alter the functionality of any of these core files without modifying them directly, which ensures that your application remains upgrade-proof. By all means, look in order to better understand how Magento works, but do not touch.

Community

As the name suggests, app/code/community is where you will find modules that have been provided by third parties (i.e. not Magento’s core team). Hundreds of modules are available through Magento Connect, and when you install them through the built-in “Package Manager,” this is where they end up.

Local

Magento ships with an empty app/code/local directory, ready for you to add bespoke modules for your own Magento installation. This is where we will be working for the duration of this tutorial.

Structuring Our Directory

Open your favorite editor, and navigate to app/code/local to add some new directories and files.

Module Namespace

The first directory we will create is a “namespace.” This can be called anything you like, but the convention is some form of the name of the company or module’s author. Magento uses “Mage” as its namespace. Here at Ampersand Commerce, we use “Ampersand.” For this tutorial, we will use “SmashingMagazine” as our namespace. So, create the directory app/code/local/SmashingMagazine.

Module Name

For the next directory, we will give our module a descriptive name. The module we are creating will write log entries each time a product is saved, so a logical name would be LogProductUpdate. Create the directory app/code/local/SmashingMagazine/LogProductUpdate.

We should now have the following directory structure for our module. These directory and file names are case-sensitive, so capitalize where appropriate.

app
  - code
      - local
          - SmashingMagazine
              - LogProductUpdate

Configuring Our Module

Next, we will begin to configure our module. The configuration files belong inside our module in a directory named etc, so let’s create that along with a new XML file: app/code/local/SmashingMagazine/LogProductUpdate/etc/config.xml. This XML file will inform Magento of the location of the files in our module, as well as many other things, such as version number and events to observe. For now, we will create a simple config.xml file, which contains comments that explain the meaning of each section.

<?xml version="1.0" encoding="UTF-8"?>

<!-- The root node for Magento module configuration -->
<config>

    <!--
        The module's node contains basic
        information about each Magento module
    -->
    <modules>

        <!--
            This must exactly match the namespace and module's folder
            names, with directory separators replaced by underscores
        -->
        <SmashingMagazine_LogProductUpdate>

            <!-- The version of our module, starting at 0.0.1 -->
            <version>0.0.1</version>

        </SmashingMagazine_LogProductUpdate>

    </modules>

</config>

Activating Our Module

The next step is to inform our Magento installation that our module exists, which we do by creating a new XML file in app/etc/modules. The name of this XML file can be anything you like, since Magento will read all XML files in this directory and will be interested only in the content. However, by convention we should give the file and module the same name. Let’s create app/etc/modules/SmashingMagazine_LogProductUpdate.xml with the following content:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <SmashingMagazine_LogProductUpdate>

            <!-- Whether our module is active: true or false -->
            <active>true</active>

            <!-- Which code pool to use: core, community or local -->
            <codePool>local</codePool>

        </SmashingMagazine_LogProductUpdate>
    </modules>
</config>

Sanity Check: Is The Module Enabled?

We now have a fully functional module that is enabled in Magento. It doesn’t do anything, but it is a valid module. This is our first opportunity to see whether we have correctly configured everything so far. If we log into the Magento admin panel and navigate to System > Configuration > Advanced > Advanced and view the “Disable Modules Output” listing, we should see our SmashingMagazine_LogProductUpdate module listed as enabled. If it is not listed, then something has gone wrong, so carefully run through the steps up to this point again. This is usually when new Magento developers discover the cache!

Our module’s structure now looks like this:

app
  - code
      - local
          - SmashingMagazine
              - LogProductUpdate
                  - etc
                      - config.xml

  - etc
      - modules
          - SmashingMagazine_LogProductUpdate.xml

Defining An Event Observer

Event observers are extremely powerful and are one of the cleanest ways to extend Magento’s functionality without having to rewrite or override any core methods or classes. We want to observe the event that Magento dispatches just after a product is saved, so the code for the event we are interested in is catalog_product_save_after. Determining which event code to use when defining a new observer requires a basic understanding of Magento’s model layer, which is beyond the scope of this tutorial. Don’t worry, though: we’ll cover it another time!

We now need to modify our config.xml to include the event observer definition:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <SmashingMagazine_LogProductUpdate>
            <version>0.0.1</version>
        </SmashingMagazine_LogProductUpdate>
    </modules>

    <!-- Configure our module's behavior in the global scope -->
    <global>

        <!-- Defining an event observer -->
        <events>

            <!-- The code of the event we want to observe -->
            <catalog_product_save_after>

                <!-- Defining an observer for this event -->
                <observers>

                    <!--
                        Unique identifier within the
                        catalog_product_save_after node.
                        By convention, we write the module's
                        name in lowercase.
                    -->
                    <smashingmagazine_logproductupdate>

                        <!-- The model to be instantiated -->
                        <class>smashingmagazine_logproductupdate/observer</class>

                        <!-- The method of the class to be called -->
                        <method>logUpdate</method>

                        <!-- The type of class to instantiate -->
                        <type>singleton</type>

                    </smashingmagazine_logproductupdate>

                </observers>

            </catalog_product_save_after>

        </events>

    </global>

</config>

Configuring Our Model’s Directory

In the event observer defined above, we made reference to a model that we have not yet created. We need to inform Magento where to find models in our module by updating config.xml with the following:

<?xml version="1.0" encoding="UTF-8"?>
<config>
    <modules>
        <SmashingMagazine_LogProductUpdate>
            <version>0.0.1</version>
        </SmashingMagazine_LogProductUpdate>
    </modules>

    <!-- Configure our module's behavior in the global scope -->
    <global>

        <!-- Defining models -->
        <models>

            <!--
                Unique identifier in the model's node.
                By convention, we put the module's name in lowercase.
            -->
            <smashingmagazine_logproductupdate>

                <!--
                    The path to our models directory, with directory
                    separators replaced by underscores
                -->
                <class>SmashingMagazine_LogProductUpdate_Model</class>

            </smashingmagazine_logproductupdate>

        </models>

        <events>
            <catalog_product_save_after>
                <observers>
                    <smashingmagazine_logproductupdate>
                        <class>smashingmagazine_logproductupdate/observer</class>
                        <method>logUpdate</method>
                        <type>singleton</type>
                    </smashingmagazine_logproductupdate>
                </observers>
            </catalog_product_save_after>
        </events>

    </global>

</config>

Creating An Observer Model

We will now create the model to be instantiated when the event is dispatched. Create a new PHP file in app/code/local/SmashingMagazine/LogProductUpdate/Model/Observer.php with the following content:

<?php
/**
 * Our class name should follow the directory structure of
 * our Observer.php model, starting from the namespace,
 * replacing directory separators with underscores.
 * i.e. app/code/local/SmashingMagazine/
 *                     LogProductUpdate/Model/Observer.php
 */
class SmashingMagazine_LogProductUpdate_Model_Observer
{
    /**
     * Magento passes a Varien_Event_Observer object as
     * the first parameter of dispatched events.
     */
    public function logUpdate(Varien_Event_Observer $observer)
    {
        // Retrieve the product being updated from the event observer
        $product = $observer->getEvent()->getProduct();

        // Write a new line to var/log/product-updates.log
        $name = $product->getName();
        $sku = $product->getSku();
        Mage::log(
            "{$name} ({$sku}) updated",
            null,
            'product-updates.log'
        );
    }
}

We’re done! Try it out.

The directory structure for our completed module should now look like this:

app
  - code
      - local
          - SmashingMagazine
              - LogProductUpdate
                  - Model
                      - Observer.php

                  - etc
                      - config.xml

  - etc
      - modules
          - SmashingMagazine_LogProductUpdate.xml

Now that our module is complete, it’s time to try it out! Log into the Magento admin panel, create or update a product in your catalog, and then check the var/log folder to see your product-updates.log file populated.

If nothing appears or the directory does not exist, ensure that the correct permissions are set to allow Magento to write to this directory, and that logging is enabled in Admin Panel > System > Configuration > Developer > Log Settings > Enabled.

This basic tutorial is meant to give you an overall understanding of how Magento modules work. After completing this tutorial, spend some time exploring the Magento modules in app/code/core and see if you now have a better idea of how it all works.

Feel free to view or download the source code2 (Github).

We welcome any questions and would love to hear any feedback in the comments area below.

Footnotes

  1. 1 http://ampersandcommerce.com/what-is-magento/
  2. 2 https://github.com/josephmcdermott/SmashingMagazine-MagentoFirstModule

↑ Back to topShare on Twitter

Advertising
  1. 1

    David McDermott

    March 1, 2012 1:30 pm

    I find it weird that I have similar credentials, the same last name, am in the same line of work, and have dealt with Magento websites.

    Go figure. Family trees ftw.

    2
  2. 3

    Once you grasp the basics of what’s happening here you can really speed up by looking at the free module creator extension.

    I’m currently battling my through my first Magento module and I’ve really jumped in at the deep end; front end, back end screens, judicious use of ajax and dynamically creating products on the fly to add to orders.

    It’s unreal how different it is to other ecommerce software.

    0
    • 4

      Joseph McDermott

      March 2, 2012 2:55 am

      To which ‘free module creator extension’ are you referring? Anything that assists a developer in getting started with Magento is great, but like you say it is important that the effort is made to understand what is actually happening behind the scenes rather than getting in to the habit of using an automated tool every time.

      0
    • 5

      There is a promising plugin for eclipse that helps with module creation and Magento coding

      0
    • 6

      You don’t want to speed up creating modules when creating your first module. The first couple of modules you’re learning and you should not speed things up. Once you’ve created a couple you can switch to “module creators” to gain some time.
      That’s what I would do.

      0
  3. 7

    Note to self: continue to code plugins for WordPress and never look again at that megacumbersome way of doing custom things in Magento

    0
    • 8

      Great article! Yes it may seem cumbersome to some but there’s a reason things are the way they are in Magento. Once you learn, it is a very powerful system.

      0
    • 9

      Andres Hermosilla

      March 1, 2012 10:00 am

      I agree with you @Ozh, Magento is very cumbersome. Many people (the 100’s of sub200 product sites) use it when they should be using a lighter system.

      0
    • 10

      Joseph McDermott

      March 2, 2012 2:55 am

      I can understand this point of view, however as @Tim Glaesel points out Magento is not something that you can just dive straight in to and get instant results with your custom development. It is however a framework that, once your understanding grows, is extremely powerful, and the goal of these articles I am writing is to provide you with that understanding. This is the first of a series.

      2
    • 11

      Exactly my thought when first looking at Magento aswell, really confused, coming from writing plugins for WP, Joomla, vBulletin and others..
      Now I “have to” write a Magento module for a client, so better get started.

      Thanks for the good guide, I loaded up 3-4 different “starter guides” for writing Magento modules, and this one is definently the one with best content!

      I really love the clarity of WordPress Codex, can’t seem to find something similar for Magento. Is there such a thing? Just having a clear list of possible events to hook would be lovely.

      0
  4. 13

    Moldován Eduárd

    March 2, 2012 2:25 am

    I like this article. But I can’t help saying something something about evangelizing magento. Nobody should do that. It one of the worst built PHP systems I have ever seen.
    I think we should not teech people to use something which is not good enough. I understand that it has a goal, that many people are using magento, but for one and only reason: there is nothing similar. But this doesn’t make Magento good…
    Maybe my opinion about it very bad, and I do not want to offend anybody, there is probably lots of work behind magento, but still.
    Wouldn’t the goal of a opensource/free/whatever product be ease of use? Or at least one of its goals. Magento is anything but that.

    While writing my comment, I saw Tim Glaesel’s comment, which is similar to mine, One big difference: I think Magento isn’t fit for anything. Its ‘powerful’ architecture temds to make things too complicated, in such way, that if one needs something powerful, it might be easier to write one from scratch.

    0
    • 14

      If you know Magento enough good, you can create huge powerful store in 2 weeks. since I am using magento I am able to create 3 times more projects monthly than before. And they work just perfect for my clients.

      -1
  5. 15

    I find Magento to be a pig on most common web server setups. I wish it wasn’t, because I love the reporting and features.

    0
    • 16

      Magento is a bit of a resource hog, but with the right setup you can get decent levels of performance out of it.

      As always with complex web applications caching is everything. There are various ways you can use caching in and around Magento to help with performance. The Enterprise edition also has a full page cache built in.

      Another common mistake is for people to try and serve their static assets (jpg,css,etc) from a heavyweight apache process.

      0
  6. 17

    We are giving up on Magento for many reasons..

    How about giving some love to a truly flexible ecommerce solution like Drupal eCommerce?

    -1
  7. 18

    Magento is a horrible system. It’s bloated and generally un-maintainable. Keep in mind with Mage, if you start with a version and start installing modules, you’re not going to be upgrading regularly.

    And if you don’t upgrade regularly, upgrades will break in general.

    -1
    • 19

      Anyway, websites have a lifetime of maximum 2-4 years. So, then you have to redesign the layout and change or make a major system upgrade if you want to stay in the game :).

      0
      • 20

        My god, the last thing you want is your *framework* dictating your front-end refresh cycle!

        0
  8. 21

    Nice article.Really useful tutorial.Thank you for sharing.

    0
  9. 22

    Hi Joe,

    I appreciate your ambition to provide some insight into magento and I understand this is by far not an exhaustive tutorial. Personally, I would have hoped for a little bit of advice for new developers outlining what projects it is suitable for – and more importantly for which projects (and budgets) magento should possibly not be your first choice.

    Assuming that a developer has a very good understanding of design patterns, the zend framework and the internal architecture, magento is a very powerful system. I would like to stress, however, that magento comes with an enormous overhead and a poor documentation at best. Seeing how popular word-press appears to be here on smashing magazine, I think it’s important to point out that a word-press developer will have the most frustrating experience of their life if they plan to jump in at the deep end and want to make the transition to magento for a tiny $10k e-commerce project. Having said that, if you feel comfortable reverse-engineering complex systems and like to look at magento as a framework rather than a shop out of the box, you will definitely get a lot out of it.

    -Tim

    0
    • 23

      Joseph McDermott

      March 2, 2012 2:57 am

      I will certainly keep in mind for future articles a ‘Getting Started Guide’, outlining what projects and budgets Magento is best for. Being open-source, its really the Magento developers and agencies working with Magento every day who create ‘documentation’. On Smashing Magazine there is a gap in the amount of articles about WordPress versus articles about Magento. By creating a pool of knowledge here, I hope to allow for developers to be able to make informed choices.

      0
  10. 24

    Great Article!

    Looking forward to see more Magento articles on SmashingMag. :)

    0
  11. 25

    Fantastic article, I’d love to see more Magento articles like this on here! Will there be more advanced tutorials coming up? Thanks!

    0
  12. 26

    Finally! I will pay to see even more articles about Magento! So popular, and so sucky documentation!

    0
  13. 27

    Great article ! WP and Magento are the main platforms I work with…. It would be great if Magento had a separate section on Smashing Mag, just like WP has .

    Waiting for more !

    0
  14. 28

    nice

    0
  15. 29

    I literally did a little dance when I saw this in my RSS feed!
    Can I request a lesson on widgets too?

    0
  16. 30

    I think Magento is a very bad idea for most.

    1) It is far too heavy on resources, a good system doesn’t need a high spec server to run a small site. For larger sites it require HUGE investment

    2) It is far too complicated. A ‘turtles all the way down’ approach is very good as long as the stack of turtles doesn’t go so deep there is no light to see them. It is very hard to see what is going on in Magento

    3) Documentation is absolute rubbish. The number of times I wanted to do something and found no info on the Magento sites, in the forums or on the web in general, often there would be lots of I’m having this problem too comments against a post with no one having any solutions.

    4) Updates are a nightmare. The system was designed to make updates easy, this is not how I see the reality.

    0
    • 31

      Please give me an alternative which is equivalently flexible to create practically any eCommerce site, and that will allow me just as Magento, create a powerful Web Store in 2 weeks. … I will gladly switch.

      0
      • 32

        I agree that there are very few good ecomerce systems out there, and in the open source world I don’t think there are any at all!

        That doesn’t make Magento a good system though.

        We have retreated to our own bespoke systems as a result after some disastrous flirting with Mag.

        Can you make a FULLY BESPOKE site in Magento in 2 months let alone 2 weeks? By that I mean 100% custom layouts (HTML built from scratch &c.), modules etc … leave all alone, use plugins and themes as they are and of course you can do in 2 weeks, there are plenty of systems that only take about 2 days to set up if all you are doing is monkeying around with a CSS and adding a blog module, even OS commerce can do that.

        0
    • 33

      Magento is indeed a resource hog, and like almost all e-commerce systems, has dud native documentation, (although that’s solved by the plethora of quality 3rd party documentation around). Horses-for-courses though: Magento is not designed for the toy-tinkerer market like OSCommerce, BigCommerce or Prestashop and its complexity means it has cut out 1000s of amateurs and outsourced 3rd world developers who aren’t up to its highly abstracted design concepts. For me, that’s a good thing – the high technical barrier to entry means reputable developers who understand and work with Magento’s design concepts produce add-on modules that are of high quality – far less hit and miss than other software.

      Any upgrade issues are the fault of the module developers – if you stick to Magento design principles, upgrades are seamless. Magento, unlike OSCommerce et al is not for your mom-and-pop store and shouldn’t be compared to software out there that meets those needs. Small sites are best served by sticking to something simpler, Magento is not really an appropriate solution for a small online store, and is not in competition with them.

      1
  17. 34

    Joseph McDermott

    March 6, 2012 12:48 pm

    A couple of comments have been made about Magento being a ‘nightmare’ to upgrade. This is not the case, as long as your custom modules are approached in such a way that will allow for these upgrades.

    For example, rather than directly modifying files in app/code/core, add event observers to achieve the same result. Instead of directly modifying template files, create child blocks that can be unobtrusively included in the page.

    These are the techniques that I will be introducing in upcoming articles, and by adding your custom functionality in this way, you will soon find that Magento upgrades are something to look forward to rather than to fear.

    1
  18. 35

    Hello,

    I’m a lead dev in a team developing a big e-commerce solution.
    Magento is the more “complete” solution for e-commerce but customizing it is a real nightmare.

    The architecture is old school and they don’t respect standards of MVC.
    Magento is a real crap ! It’s based on Zend but they use Zend as librairies and not as a MVC base.

    The doc is poor and you’d better dive into the “code” to understand …

    First and last project for me with Magento !
    They plan to release Magento2 for early 2013 if it’s not a real MVC I give up and wait for a real solutiion :/

    My 2 cents

    0
  19. 36

    Hello
    I followed the steps mentioned above, but once I update/create a product I am finding this warning in the system.log

    2012-03-09T12:14:23+00:00 ERR (3): Warning: include(SmashingMagazine_LogProductUpdate_Model_Observer.php) [function.include]: failed to open stream: No such file or directory in /path_to_my_magento_installation/includes/src/Varien_Autoload.php on line 93
    2012-03-09T12:14:23+00:00 ERR (3): Warning: include() [function.include]: Failed opening ‘SmashingMagazine_LogProductUpdate_Model_Observer.php’ for inclusion (include_path=’path_to_my_magento_installation/includes/src:.:/opt/lampp/lib/php’) in /path_to_my_magento_installation/includes/src/Varien_Autoload.php on line 93

    Have set all permissions proper for the files. Is there anything else I am missing here ?

    0
    • 37

      Hi Midhun, thank you for trying the tutorial and sorry for the delay in getting back to you! I have attached the source code to the bottom of the article for you to compare with what you have, and try to identify the problem. Please let me know how you get on.

      0
      • 38

        Getting the same result with the zip file provided:

        2012-11-26T07:32:46+00:00 ERR (3): Warning: include(SmashingMagazine_LogProductUpdate_Model_Observer.php): failed to open stream: No such file or directory in /var/www/magento/includes/src/Varien_Autoload.php on line 93
        2012-11-26T07:32:46+00:00 ERR (3): Warning: include(): Failed opening ‘SmashingMagazine_LogProductUpdate_Model_Observer.php’ for inclusion (include_path=’/var/www/magento/includes/src:.:/usr/share/php:/usr/share/pear’) in /var/www/magento/includes/src/Varien_Autoload.php on line 93

        0
    • 39

      I have the same issue as Midhun, and I’ve checked everything and all is correct (as far as the code) – I am using 1.7.0.2 and got the exact same error. Can you please let me know what I am missing? Thanks

      0
  20. 41

    I’m looking for a good ecommerce solution. I’ve been trying several things such as

    Yahoo stores – I find it to cookie cutter in looks. The RTML isn’t documented very well and custom templates can run into the thousands.

    Cre Store – Comes incomplete. You have to buy modules to get functionality.

    Oscommerce – Much like Cre store comes incomplete and you have to add modules.

    Magento – It looks promising except I am having issues to get it installed. Very little support on this.

    Dashcommerce – The Asp functions would be great, but I also have install issues with it as well.

    It is becoming very frustrating to say the least.
    |I’d like to build a large Website with dynamic modules, nested navigation and other features. Magento is too expensive and GoDaddy Website Tonight doesn’t let a Website scale very well. What do you recommend that is free or affordable? Possibly like Drupal or Joomla but easier to use?
    Looking for a tool or platform to build a dynamic site as easy as WordPress but with more power to it, not a hard coded site. Thanks in advance for your help!

    0
  21. 42

    this is very useful article
    but i want to start execution indexAction of IndexControler from module in magento.
    plz help me
    and send email me this solution plz.
    thanks.

    0
    • 43

      Absolutely brilliant article Joseph – thanks so much for giving us the basics on module creation which is pretty difficult to do on your own, or via looking at the code yourself. Unfortunately, no matter the quality of the article, you’ll always get a few stupid questions like the one above. I have no idea why these people ask these questions (vague question, do my work for me, plz help) , surely they’d realise no-one can possibly answer them, or would be bothered doing their jobs for them. They kind of the put the ‘3rd’ in ‘world’.

      -1
  22. 44

    Is this tutorial still working for the 1.7.0.1 community version?

    I tried to create a custom module, there wasn’t a local folder (I created one) and now I have the same names as the article, same code, and I can’t see the module listen in System->Configuration->Advanced->Advanced.

    I already tried to change the “Current Configuration Scope” combo without any results and double checked the article.

    I’m trying this in a Xampp server.

    0
    • 45
    • 46

      Paul, how did you solve this? I’m having the same issue…

      ** update **
      Actually, it appears that I was wrong, I didn’t have the same issue. There is an arrow that you have to click to expand the modules. It’s not expanded by default…a little end user stupidity mixed with some poor UX for this section = facepalm :)

      0
  23. 47

    Sometimes we need to download files as attachment in PHP. This code can ideally be called on a hyperlink.

    $filename = “myImage.jpg”;

    if(file_exists($filename)) {

    header(“Content-disposition: attachment; filename={$filename}”);

    //Tell the filename to the browser

    header(‘Content-type: application/octet-stream’);

    //Stream as a binary file! So it would force browser to download

    readfile($filename);

    //Read and stream the file

    }else{

    echo “Sorry, the file does not exist!”;

    }

    ——————–

    Source:

    http://phphelp.co/2012/06/13/how-to-stream-a-file-as-attachment-in-php/

    OR

    http://addr.pk/a988

    0
  24. 48

    This works great! Thanks. I appreciate the comments in the XML. I think I’ve gone through almost every module creation tutorial on the net. Magento takes a long time to learn but it seems limitless as far as being extensible. Our site is up and running on another platform and from a developer standpoint there are always things you want to do but you find yourself writing PHP hacks and re uploading databases just to get things accomplished especially with proprietary software that has little support and limited functionality.

    0
  25. 49

    This works great! Thanks. I appreciate the comments in the XML. I think I’ve gone through almost every module creation tutorial on the net. Magento takes a long time to learn but it seems limitless as far as being extensible. Our site is up and running on another platform and from a developer standpoint there are always things you want to do but you find yourself writing PHP hacks and re uploading databases just to get things accomplished especially with proprietary software that has little support and limited functionality.

    0
  26. 50

    Joseph,

    thank you for this article, even though it is rather simple, it was helpful to me in creating my first Magento module. I am unable to understand the motivation for all the Magento bashing in the previous comments. Having worked with WordPress and Drupal before, I don’t find Magento’s learning curve to be different from those other CMS/frameworks. Agreed, it’s architecture is very different compared to most other PHP web apps, but doing serious work with any CMS or framework requires reading, learning and experimenting. After playing around with Magento for just a couple of days I find its code to be very well structured. Just as WordPress was never meant to power webshops, Magento is not designed to be a CMS either.

    // Claus

    0
  27. 51

    neat tutorial, there’s a couple of problems with the code

    – in the xml files the capitalisation of references to the php class should be the same as the directory names, filenames (it isnt)

    – in the observer registration the value doesnt work as it described, it does work when set to SmashingMagazine_LogProductUpdate_Model_Observer

    1
    • 52

      Hi Dancl, thank you for the feedback.

      The XML class references which are not capitalised are in fact the ‘alias’ or ‘short name’ for the model directory, which can actually be whatever you like. As long as the folder structure and case is correct in the definition of config/global/models/{alias}/class/Xxxx_XxxxXxxx. Whatever you used for {alias} here, you can use throughout your XML files to refer to the Model path.

      The observer method you mentioned, smashingmagazine_logproductupdate/observer, will use the Magento model autoloader to replace the model ‘alias’ with the correct class name from the model definition above, ie. in simple terms it will actually become SmashingMagazine_LogProductUpdate_Model/observer, and auto-capitalize the parts after the slash to become SmashingMagazine_LogProductUpdate_Model_Observer, which is correct.

      Please let me know if I have completely missed your point :)

      0
      • 53

        The config xml file should read like below. I use Magento 1.9 version


        0.0.1

        SmashingMagazine_LogProductUpdate_Model


        SmashingMagazine_LogProductUpdate_Model_Observer
        logUpdate
        singleton

        0
  28. 54

    Hi!

    It’s great, but 8 months later it’s the only one… :^O

    0
    • 55

      The next articles in the series are on the way very soon!
      Currently in the final proofing stage :)

      0
      • 56

        Ough, cool! I’m trying to make a new shipping module, and.. it’s killing me :)) I hope, you will continue, this ‘module story’ :) thanks! anyway, magento is impressive stuff, but little bit hard to ‘find the way’ . :)

        0
  29. 57

    This is best tutorial. After reading this to understand i have used module creator and it is really fantastic to use module creator if u follow guideance written by this guy
    http://www.techflirt.com/how-to-createn-custom-magento-module-step-by-step-tutorial/

    0
  30. 58

    Hi Joseph, I must say that your guide is just Superb for people who are new to magento. This will be the basis for people who don’t know where to begin to build their own magento modules :)
    Our website also offer free Magento extensions and all are 100% open source. I recommend people who’re interested in building a new magento extension may visit us to get our free extensions for to research more.
    magestore.com/magento-extensions.html

    0
  31. 59

    Excellent article. If your product-updates.log file fails to show up yo have two options.
    1. Make sure you have your Mage in dev mode by adding this to your .hosts file: SetEnv MAGE_IS_DEVELOPER_MODE

    or

    2. force log entry by changing this line :
    Mage::log(
    “{$name} ({$sku}) updated”,
    null,
    ‘product-updates.log’
    );
    to
    Mage::log(
    “{$name} ({$sku}) updated”,
    null,
    ‘product-updates.log’,
    true
    );

    0
  32. 61

    This was nice start, I was hoping to get a look at some more development guides. Something that helps build a front end, backend. A small data entry/retrieval program, even a guestbook would be nice.

    Something that gets the foot in the door.

    0
  33. 62

    Hi,
    It’s compatible with the version 1.7.0.2?, because i’d uploaded to my site, but i can’t see the module, i’d flushed the cache, but still. Any ideas?

    Regards

    0
  34. 64

    A great quickie walkthrough for newbs, thanks! Just the starter I needed. Much appreciated.

    0
  35. 65

    Thanks for creating such a nice tutorial.

    0
  36. 66

    Thank You its help to start with magento to me…. :)

    0
  37. 67

    One minor detail. Enable logging in “Configuration > Developer > Log settings” otherwise you won’t see log file in your magento installation

    0
  38. 68

    It really helped me to understand and write my first piece of code in Magento. Thank u :)

    0
  39. 69

    I followed this tutorial and all seems very clear. But I receive an error:

    Fatal error: Call to a member function getName() on a non-object in …/xxx.xxx.nl/httpdocs/…/app/code/local/…/LogProductUpdate/Model/Observer.php on line 21

    The Varien_Event_Observer was not passed as the first parameter I guess. I am using Magento 1.7.0.2

    Thanx

    0
  40. 70

    I am new to magento and coded my first module ever using this easy to understand article. Thank you for this great article :) . Keep it up

    0
  41. 71

    Great article. Thank you for making this.

    0
  42. 72

    Hi, a newb here…. Can you recommend any really good books to help get me up to Senior speed ASAP? Thanks!!

    0
  43. 73

    Thanks for the tutorial, it helped me understand how to create a magento module. And I just created my first one based on the information here.

    0
  44. 74

    hello Joseph McDermott,
    i want an ajax based magento module that will check zip code based delivery of product.Please help…

    0
  45. 75

    Thanks for the tutorial. I had to run through it 2 or 3 times to finally get it to work, but the moment it did, it felt great. haha.

    0
  46. 76

    Hrishikesh Mishra

    January 3, 2014 9:10 am

    Thanks, its a great example. Please provide some more example which explore the depth of magento.

    Regards,
    Hrishikesh Mishra

    0
  47. 77

    Why do we need to create our own module although by default we are getting the magento module?

    0
  48. 78

    This example is very very simple and useful to beginners like me. Thanks lot! And we need more and more simple example with execution flow.

    0
  49. 79

    Your article has inspired me to search and implement more of such Magento modules.
    Thanks for this trigger!!

    0
  50. 80

    Hi,
    Followed all instructions and everything is visible in backend in delivery methods. However this method is not showing in front end. Any ideas? Do I need to enable compilation?

    0
  51. 81

    In 1.7.x the model definition in config.xml needs to use only the module name, without the namespace, like this: <logproductupdate>

    But anyway, thanks a lot for this article, it helped me most from those I could find;)

    0
  52. 82

    Grateful to you……… helped a lot

    0
  53. 83

    Thank you so much for this tutorial.

    0
  54. 84

    The amount of “developers” complaining that Magento sucks because they lack the knowledge and developer skills to actually learn a new framework that ins’t as simple as WordPress boggles my mind… If you can’t dedicate the time to learning a complex and powerful enterprise ecommerce platform, then don’t use a complex and powerful enterprise class ecommerce platform for your dinky mom and pop websites for your aunt’s friend. This isn’t something you use to learn on a new client like a WordPress theme you downloaded and cut/paste some elements around and call yourself a developer, which is what 90% of the people complaining on here are exactly.

    0
    • 85

      Buddy, it isn’t about “learning a new framework”. The community edition of Magento has real problems and bugs that hinder development. The caching system is a fucking nightmare, and magento doesn’t have real name spacing. I’m not even going to talk about the verbosity of the “configuration based mvc” that it claims is so powerful. It lacks real name spacing, so all the configuration is proprietary bullshit that has to be memorized, otherwise creating an extension is a frustrating nightmare. These are real problems, so the bullshit about “developers not wanting to learn complex frameworks” is exactly that, bullshit.

      0
  55. 86

    This is by far the best article regarding basic magento structure and theory there is on the internet. The diagrams for file structure really helped.

    0
  56. 87

    Hi,
    Followed all instructions and everything is visible in backend in delivery methods. However this method is not showing in front end. Any ideas? Do I need to enable compilation?

    1
  57. 88

    Stephen Richards

    July 1, 2014 3:46 pm

    Hi,

    It may be worth placing a side note in the tutorial to check Magentos ‘var/log’ directory or specify the full Magento path as I spent 20 minutes re-doing the tutorial looking in the OS /var/log :)

    For an experienced Magnento user it may be obvious that Magento would write to magento/var/log but as I come from a Linux background, I assume /var/log when I read var/log ;)

    Ste,

    0
    • 89

      Stephen Richards

      July 1, 2014 3:49 pm

      “or specify the full Magento path”

      You did do that so ignore that comment I made previously :)

      0
  58. 90

    Glad to know this,
    Now i have a basic idea on working flow of Events.
    ‘Sometimes simple clarifications gives global knowledge ‘ :)

    0
  59. 91

    This approach does not work with 1.9.0.1 the module will never show up in advanced admin settings.

    0
  60. 92

    Great Help …!!!

    i want some change in total price of each group calculation
    i want to calculate each grouped product price as

    sub total 1 = product 1 price x quantity x custom value
    can you tell me how to do this ?

    thank you in advance

    0
  61. 93

    Great article gave me a good idea on every basic steps in magento module development.It works fine for me what is there in the described code.i tried to rewrite the code with my own package and module name, it want work.most probably i followed each and every line of the coding.one thing i want to figure it out is, What is “” tag in the config.xml after tag.It will be great pleasure if you can help me to figure out my error.
    Thank you.

    0
  62. 94

    Great article gave me a good idea on every basic steps in magento module development.It works fine for me what is there in the described code.i tried to rewrite the code with my own package and module name, it want work.most probably i followed each and every line of the coding.one thing i want to figure it out is, What is “catalog_product_save_after” tag in the config.xml It will be great pleasure if you can help me to figure out my error.
    Thank you.

    0
  63. 95

    Thanks Joseph,

    A very useful Article.

    Can you throw some light on how to put your extensions on the magento market place after development?

    Regards.

    0
  64. 96

    Where can I get a list of all the events?

    I checked here http://www.magentocommerce.com/wiki/5_-_modules_and_development/reference/magento_events , but surprisingly the event catalog_product_save_after is not defined in this list!.

    Thanks.

    0

↑ Back to top