10 concepts magento developers should know

  • Sharebar

Magento developers united

Over the years as a magento developer I have come across some excellent code, you know the kind of code that inspires you to be better, to do better and quite frankly be proud of being a developer.

However I have also seen a lot of code that in truth, well it makes me puke. It makes me so sad. I can only think of one reason… some developers just don’t RTFM… Magento’s core code and xdebug will show anything you need to know about Magento and its best practices.

So for such reason We are putting together a list of 10 simple concepts every magento developer should know to understand what Magento development is all about. These were chosen based on how weak the implementations I have seen are. So you won’t see EAV, API or mobile here because for better or for worse these are usually left untouched.

Show me some love…what are these concepts?

Magento Observers… the light at the end of the tunnel

Magento is an event driven application, which means that it uses the observer pattern a lot, Magento’s implementation of the design pattern could use some good refactoring but they still maintain a list of all the observers and Magento notifies all of them automatically of any state changes. In other words, the hack and slash concept of overriding and rewriting core’s functionality is not the only way to get anything done in Magento, if anything I feel compelled to say that it should be avoided unless you must rewrite how a piece of code actually works, like say fix a bug. Now we need more events being dispatched before Magento can remove some of the flexibility to change how some code works.

So here is some code to show you the full power of Magento’s events the observers:

Open: Mage_CatalogInventory_Model_Stock_Item and read the checkQuoteItemQty this function validates products being added to the cart. One word: Fantastic.

if (!$this->getIsInStock()) {
    $result->setHasError(true)
        ->setMessage($_helper->__('This product is currently out of stock.'))
        ->setQuoteMessage($_helper->__('Some of the products are currently out of stock'))
        ->setQuoteMessageIndex('stock');
    $result->setItemUseOldQty(true);
    return $result;
}

That means before a product is added to the cart I can verify all of its information without even knowing the product type or how magento handles products. Brilliant.

But this is just the tip of the iceberg and maybe one of the most mundane examples, consider it the hello world of the Magento events.

Here is a list of some events: http://ubuntuone.com/1EtlQBTZMmDkMuF0v9GBfT not sure where I got it from. The key to know is that no all events are hardcoded but some if no most of them are built on the fly…

Why is this the first concept I think magento developers should understand?

The more code reviews or Magento implementations I look at, be it community or enterprise from 2008 or 2012 have one thing in common: core modifications that are simply unnecessary. So maybe, just maybe by adding some firepower to the magento developers knowledge base we can eradicate core modifications once and for all.

Do you like this… well head over to the wiki and learn everything you can about Magento Observers.

Magento Collection… the abstraction layer

I think this tutorial should be more than enough to show you its power.

One of my major headaches with new developers is how they want to “load” inn Magento an object to get the data they need, consider the following, I want to get the brand attribute out of the product collection:

In my book, this is a very bad approach:

$collection = Mage::getModel('catalog/product')->getCollection();
foreach ($collection as $product){
      $product->load($product->getId()); //say what!?!!?
      echo $product->getBrand();
}

This is a bit much better:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToSelect('brand');
foreach ($collection as $product){
   echo $product->getData('brand');
}

So you see how awesome the collections are, consider the following example:

$collection = Mage::getModel('catalog/product')->getCollection();
$collection->addAttributeToFilter('type_id', array('eq'=>Mage_Catalog_Model_Product_Type::TYPE_SIMPLE));

Complain about Magento all day long if you want but see how powerful the data handler is and it is not even using Doctrine. The main problem with Magento is its flexibility (and keeping 300 objects in session :-/)

Magento layouts…

The best concept ever imagine by any framework developer. To me Magento’s layouts are the most amazing piece of engineering inside of Magento land and it should be well understood.

Why? When I do code reviews I look at 1 template, the most important template: catalog/product/view.phtml.

What’s the big deal? Well Magento built that template to accommodate the out of the box product types and any other that you could create. Most people don’t even know that the magento layouts can handle different product types, open the catalog.xml and be amazed. You can customize each product by knowing its type and you can style the pages differently without having to touch a single line of code in the template…. another brilliant idea.

But here is the one of the most amazing examples:

<reference name="product.info">
    <!-- <action method="unsetChild"><name>description</name></action> -->
    <action method="unsetChild"><name>additional</name></action>
    <block type="catalog/product_view_attributes" name="tab1" as="specs" template="catalog/product/view/tab1.phtml">
        <action method="addToParentGroup"><group>detailed_info</group></action>
        <action method="setTitle" translate="value"><value>Specs</value></action>
    </block>
    <block type="catalog/product_view_attributes" name="tab2" as="fabric" template="catalog/product/view/tab2.phtml">
        <action method="addToParentGroup"><group>detailed_info</group></action>
        <action method="setTitle" translate="value"><value>Fabric</value></action>
    </block>
</reference>

You don’t need to be a magician to know what that codes and how… do you know how many times I have had to fix magento’s product view template to remove some tabs

Magento adminhtml… uncovering the truth

Magento’s frontend OOP is weak when compared to the adminhtml. Your Admin FU should be high(6-8) before you can call yourself a Magento developer. All the beauty of Magento development happens in the admin. The frontend is awesome and all but most of the work you do is simple and repetitive.

Magento developers have problem grasping Grids and forms creation which is very odd because creating skeletons in the admin is very mechanical. What you do with it afterwards is really powerful.

Let me throw this out there too… renderers should be used only when there is no way to use SQL to format that the data or when the data is a very complex solution that is easier to compute in PHP.

Magento’s core library

From my point of view… Magento’s core library is under lib/Varien… wait what? Yeah that’s right. Magento is built on top of the Zend Framework but Magento’s best code is a set of classes outside of the code pools. What is so good about these classes? Well the autoload, the Varien Object (which must be destroyed) and other wonderful utilities reside there, you know drinking mojitos and margaritas.

One of my best pieces of code was built using the Varien_IO_File and Varien_Http_Client classes and before that, well let’s say I had some really messy s’mores in front of me.

Zend’s role in Magento… let’s practice some zen

By now you Magento is based on the Zend Framework, this is pretty significant because it means besides Magento’s own framework you have Zend’s. What this means to you is that you can now start using pretty much everything from Zend’s arsenal to get this done.
From Zend_Date to Zend_Log… do you like firebug and firephp, well guess what? You can use Zend Log to do some logging for you.

Magento’s controllers

I don’t think I know enough English to explain to you the simplicity of Magento’s controllers and how routing works.

However Magento developers should know that Magento has 2 main areas: frontend and admin and each one has a default controller for it. The CMS pages and no routes get their own as well.

Here is a good explanation for it

OK now here’s something you should always keep in mind, every controller has access to the Request and Response objects… don’t use the $_GET, $_POST or $_REQUEST arrays to get data instead use the $request->getServer(‘param’) to get the info you need, especially with $_GET params.

Magento’s cron

PHP is stateless, maybe that is not the right term, but essentially you cannot rely on it to do scheduled operations on the background. Some people use daemons but the most common way to schedule tasks is through cron jobs.
Now Magento supports them out of the box, right? Yes. Not only magento has a cron.php file it also allows you to add a crontab node to your modules configuration so you can schedule tasks as well.

This doesn’t mean that Magento out of the box runs them, it only means it supports them so you must ensure that your system has a cronjob entry for it.

Here is the syntax:

<crontab>
    <jobs>
        <catalogrule_apply_all>
            <schedule><cron_expr>0 1 * * *</cron_expr></schedule>
            <run><model>catalogrule/observer::dailyCatalogUpdate</model></run>
        </catalogrule_apply_all>
    </jobs>
</crontab>

Pretty simple right?

Magento’s full page caching

An overly complex implementation of full page caching but it gets the job done and what’s more compelling about it is that most developers don’t know it has crawling out of the box…. wait what? Yea you can have Magento’s FPC regenerate itself periodically and that it is worth a lot because cache invalidation is one of the biggest pain in the behind. Here is a simple way to get similar results, be warned it is not as robust. Of course you realize that it relies on the cron jobs being set up to actually get it done, right?

If you ever used a reverse proxy like varnish you know that holepunching is trivial but yields some unexpected results… Magento’s full page caching hole punching is even easier and well the results are more unexpected? Why is that? I assume is because 1-) how headers and caching work together and 2-) because some bugs in Magento’s implementation (can someone fix the breadcrumbs already)

There is an explanation here if you want to understand how Magento’s full page cache works.

Magento’s translation

I know, I know… Magento’s translation is not really translation but string replacements but you know what? It works and it is a lot better than you think but the main reason I love it is because you can have theme level translation and that knowledge there can be the difference between hours of frustration or 4-5 beers with your friends. Why? Well if you use translation you can start changing messages and labels all over the place without having to dig into templates or code… simply by having a csv file.

Bonus…

The Shell folder in the root contains 3 files that are very important:

  1. compiler.php
  2. log.php
  3. indexer.php

You should familiarize with them as much as possible.

The compiler in magento is not really “compiling code” but moving all the classes to a common location so its faster to find them and load them. Something I was trying to improve with SQLite3 which by the way I am getting better results now icon razz 10 concepts magento developers should know

… OK!?

There is a lot more to magento than a just a pair of paragraphs like these and you should definitely read more about it. Take xdebug and profile the hell out of it… write modules and you will start connecting all the dots in a very simple way.

Here are a few module ideas that you should… try to write yourself:

  • Create a module that write some red text after the product description without changing the templates
  • On simple products make the background black if the forecast for that region is a thunderstorm without changing the templates
  • Create a admin grid that let’s you define order history range and states

Hint: Magento core text list blocks are extremely useful and awesome
Comments and suggestions are appreciated it

VN:F [1.9.22_1171]
Rating: 8.0/10 (25 votes cast)
VN:F [1.9.22_1171]
Rating: +2 (from 12 votes)
10 concepts magento developers should know, 8.0 out of 10 based on 25 ratings

Author: Luis Tineo

Husband, Father, performance improvement junkie, biker and video gamer, Linux user and in my day job I'm a Systems Architect at Blue Acorn.

Share This Post On
  • http://www.mindinventory.com/magento.php Magento Ecommerce

    The ten concepts is very important for magento developers should know and that apply to develop website.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
    • http://www.kingletas.com Luis Tineo

      Love it! Anything I can do to help…

      VN:F [1.9.22_1171]
      Rating: 0.0/5 (0 votes cast)
      VN:F [1.9.22_1171]
      Rating: 0 (from 0 votes)
  • http://twitter.com/magento_tutoria Magento Tutorial

    I totally agree with you. These ten concepts are very important and they help me a lot in my job.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  • http://www.yireo.com/ Yireo

    Nice. If only the developers who don’t care about concepts could read this blog :)

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
    • http://www.kingletas.com Luis Tineo

      Wow, Yireo thank you so much, hearing this from one of the companies that inspired me means I am definitely doing something good.

      Thank you for stopping by

      VN:F [1.9.22_1171]
      Rating: 0.0/5 (0 votes cast)
      VN:F [1.9.22_1171]
      Rating: 0 (from 0 votes)
  • Luca

    very usefull code:

    $collection = Mage::getModel(‘catalog/product’)->getCollection();
    $collection->addAttributeToSelect(‘brand’);

    foreach ($collection as $product){
    echo $product->getData(‘brand’);
    }

    From which store it takes brand’s value? From admin (id=0)?
    What have i to add to load for example brand’s value from store id = 1?

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
    • Luis Tineo

      Hey Luca,

      That code will load the product collection from the default store. You can set the store you want to get products from in the collection by using the setStore method

      VA:F [1.9.22_1171]
      Rating: 0.0/5 (0 votes cast)
      VA:F [1.9.22_1171]
      Rating: 0 (from 0 votes)
  • Roshan Lal

    Nice article :)

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  • http://www.mindinventory.com/hire-magento-developers.php hire magento developer

    Magento is a event driven application, which indicates that it uses the viewer style a lot, Magento’s execution of the style design could use some excellent refactoring but they still sustain a record of all the experts and Magento informs all of them instantly of any condition changes.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  • Parvej Vohra

    My expectation
    after reading the article are fulfilled.Thanks for sharing such a wonderful
    post.for more knowledge- https://www.facebook.com/hiremagentodeveloper

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  • George

    Just a note:

    The bellow code:

    foreach ($collection as $product){
    $product->load($product->getId()); //say what!?!!?
    echo $product->getBrand();
    }

    if you try and run it on a store with lots of products, magento will try to create an object for each of the items that match the query. And with your setup, it will pull al products that are in the store. So all those objects, magento will try to put them into the memory :-). It is always a good idea to setPageSize(limit number of Products here)

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
    • Luis Tineo

      Hey George,

      That piece of code is listed as bad code – this is what I consider the correct way to do it:

      $collection = Mage::getModel(‘catalog/product’)->getCollection();
      $collection->addAttributeToSelect(‘brand’);

      foreach ($collection as $product){
      echo $product->getData(‘brand’);
      }

      Now you are right, if you have too many products, it would be good to limit the amount of products either by sizes or by types, either way when working with Collections & Resource collections in Magento is always good to think the same way you would if you were working with SQL directly

      VA:F [1.9.22_1171]
      Rating: 0.0/5 (0 votes cast)
      VA:F [1.9.22_1171]
      Rating: 0 (from 0 votes)
  • http://myvietnam-visa.com/ vietnamvisa

    Great post!

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  • http://magentodeveloper.org/ Hire Magento Developer

    Good job. I appreciate the way you explain it all. IF you are going to hire a magneto developer must take their test as if he/she able to cover the given points he/she is the expertise and will deliver a good result. Good points.

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  • web design orlando

    My
    expectation after reading the article are fulfilled.Thanks for sharing such a
    wonderful post.for more knowledge-

    web design orlando

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  • http://www.qbht.com/ HaroldWestbrook

    My
    expectation after reading the article are fulfilled.Thanks for sharing such a
    wonderful post.for more knowledge-

    website design dubai

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  • http://itharagroup.com/ LillyBracey

    My
    expectation after reading the article are fulfilled.Thanks for sharing such a
    wonderful post.for more knowledge-

    Jogging track

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  • Danny alfonzo Viana perez

    Hi all, I am creating a module for a Sex shop in magento 1.8.1, this module is just a warning that the page content is for over 18 years, I have everything ready exept capture the event when run the page home, and then routed to the path /age-verification, then when you accept that you are of age to save a cookie for 24 hours not to display verification during that time, I have everything ready but do not know how to use the Observe , what would be the event I look for?

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)
  • Surbhi Sheth

    Great information for developer! Appreciate your sharing on years of your developing experience; can be certainly helpful to both novice & experienced Magento developer in order to make the most powerful Magento store with enhanced quality & features suitable to various eCommerce enterprises and users’ requirements.

    Features explained on Zend Technology added to Magento’s functions are really worthwhile to know for any of the Magento developers! Cheers!

    VA:F [1.9.22_1171]
    Rating: 0.0/5 (0 votes cast)
    VA:F [1.9.22_1171]
    Rating: 0 (from 0 votes)