Archive for 2009

Magento Onepage Checkout – down the rabbit hole, and what about the JavaScript?

Thursday, September 3rd, 2009 by

The Magento Onepage Checkout is quite a tricky component. For simple tasks, it is quite possible to change the PHP code with surgical precision without messing around with the JavaScript part. But unfortunately, in many complicated tasks, like adding a new step with a bunch of new functions, would inevitably involve JavaScript handling.

Here come a couple of useful tips:

Our final target is the opcheckout.js, located at: skin/frontend/default/default/js/opcheckout.js

Before we jump into hundreds of lines of JS codes, our first stop should be the template files for the Onepage Checkout. At the end of those files, you will see something like:

[codesyntax lang="javascript" lines="fancy"]
<script type=”text/javascript”>
var billing = new Billing('co-billing-form', ...);

Yes, this is the critical link between our PHP part and JS part.


Unleash the Power of Magento EAV Database Structure, Generic EAV Object

Tuesday, August 11th, 2009 by

One of the major features of Magento is the Entity-Attribute-Value (Magento EAV) data model.

This is indeed quite a block to play with. However with the right leverage, it can become rather handy.

To some extent, using EAV is, arguably, an improvement. The major advantage of the Magento EAV is: there won’t be endless ad hoc attributes inserted to the table, which is one of the critical flaw for older platforms, say OS-commerce, where table columns rapidly grow to an unmanageable size.

On the other hand, EAV basically means no raw queries of the database, or at least very challenging if you are really determined to query directly into the database, not to mention that an upgrade will easily break your code. This is, indeed, very clumsy for simple things like selecting a group of products or customers. In addition, there is quite an efficiency trade-off since there would be quite a few table joint to load an EAV object. Also, quite a lot of data a duplicated throughout different tables, a small price we have to pay.

Despite all these, personally, I find the Magento EAV quite okay. I guess the decision making factor for the Magento team to choose EAV tables against flat table is that EAV is generically more flexible. It’s relatively easy to maintain given the fact more than often multiple developers/teams are involved in the same project.


Magento Adminhtml – Using the Same Backend to Frontend Structure

Thursday, July 30th, 2009 by

The Magento adminhtml (or admin panel) is quite different from other modules for the frontend. The Adminhtml module has a distinct implementation of the Model-Controller-View scheme. In fact, forms, tables, graphs and etc. are usually stereotypical and highly repetitive comparing to the large varieties in the frondend. As a result, in Adminhtml page, such elements are generalized into block ‘widgets’ to boost coding efficiency.

However, when building a new module, we often need to modify the frontend and the backend together. And in many cases, since the we already build the functions in frontend module, it’s really a waste of energy if we have to rewrite everything in terms of ‘widget’ in the backend. Simply, we would like to migrate the controller and view to the backend and reuse the code.

Here, I’m going to go through the basic steps the Mage_Adminhtml_Block_Template rather than widgets.

(This is a developer’s guide.)

Let’s begin with creating a new menu in the Admin panel. We added to the config.xml to our new module:

[codesyntax lang="xml" lines="fancy"]

    <new_module translate="title" module="new_module">
      <title>New Module Name</title>



Create a new Magento attribute and attribute group using the sql setup file

Wednesday, July 8th, 2009 by

More than often, we need to create a new Magento attribute to extend some Magento functions, or even a new attribute group to serve our own module.  Here is how we do it using the sql setup file.

First, a few words about the sql setup file.

If this is the first time building the module or fresh installation, create a php file, say “mysql4-install-0.1.0.php”, in [company]/[module]/sql/[module]/[module]_setup/ folder, where the version 0.1.0 is defined the the config.xml file.

In case of module upgrade, try something like “mysql4-upgrade-0.1.0-0.2.0.php”, again, verify your upgrade version in the config.xml file.

Inside this file, first we need to initiate:

[codesyntax lang="php" lines="fancy"]

$installer = $this;
$setup = new Mage_Eav_Model_Entity_Setup('core_setup');



Magento Onepage Checkout, Quick But Not Dirty

Thursday, June 18th, 2009 by

Magento Onepage Checkout, Quick But Not Dirty

The Magento Onepage Checkout is a very special feature in Magento. Beyond the regular Model-Controller-View structure, the Onepage MCV is quite a hybrid of PHP and JavaScript.

The first place to look is of course the Mage_Checkout_OnepageController, this is pretty much the bread and butter of many nice little tricks.

The index action together with the corresponding block and template file are indeed very important here.  Indeed, our first treat today lies in the block file (Checkout/Block/Onepage.php):

[codesyntax lang="php" lines="fancy"]

$stepCodes = array('billing', 'shipping', 'shipping_method', 'payment',  'review');