Magento: Beginner’s Guide – Book Review

I must be doing more Magento work than I realized, as Packt Publishing has asked me to review their new book Magento: Beginner’s Guide for them.
I’ve just received my review copy and I’ll be delving into it in the coming days as I have time.

My first impression is of a good quality softcover book. It’s an on-demand printing, and obviously they are doing it correctly. The book itself is targeted at the store’s operator, and not a theme designer or Magento coder. If you want to set up your own Magento store, having a book like this to explain exactly what everything does, even just in the Admin panel, is a great help.

Stay tuned for the full review!

 

Using the Default Web Url in Magento

Magento has a setting in admin under System > Configuration > Web > Default Pages called “Default Web Url”. Normally this is set to a value of cms, and you specify which CMS page to use in the next option “CMS Home Page”. Magento (of course!) doesn’t use the Default Web Url as you would expect – it is not a simple url string that will redirect the visitor. If you’ve tried to set this to land your users, for example, on a category or product page, you’ll have found out it doesn’t work the way you might think.

The Default Web Url value is interpreted by Magento using the three-part front controller scheme that runs the entire site. The format is like this: frontname/controllername/actionname and you can only have up to three parts specified, the way it’s interpreted (as of 1.3.2.3). For those who aren’t familiar with this scheme, here’s a very brief overview. (visit http://alanstorm.com/magento_controller for a more in-depth discussion) The frontname is defined in a module’s config.xml file. Look in the frontend section under routers and you’ll find a frontName tag. (Magento core modules are all under /app/code/core/Mage). The frontname routes the request to the module’s front controller, which are in the corresponding ModuleName/controllers directory. In there are all the controller files, named as ControllernameController.php and inside those files are the action methods, named actionnameAction().

Throughout the code examples below, replace Mycompany and Mymodule with whatever names you want to use. Be sure to pay attention to capitalization; it’s important and it’s not necessarily consistent or intuitive!

To set up a module that will let you redirect an incoming root website link to wherever you want, first set up a module xml file under /app/etc/modules/Mycompany_Mymodule.xml containing this code:
[code lang=”xml”]
<?xml version="1.0"?>
<config>
<modules>
<Mycompany_Mymodule>
<active>true</active>
<codePool>local</codePool>
</Mycompany_Mymodule>
</modules>
</config>
[/code]

Then, create your company’s module directories:
[code]
/app/code/local/Mycompany/Mymodule
/app/code/local/Mycompany/Mymodule/controllers
/app/code/local/Mycompany/Mymodule/etc
[/code]

Create your config file in /app/code/local/Mycompany/Mymodule/etc/config.xml:
[code lang=”xml”]
<?xml version="1.0"?>
<config>
<modules>
<Mycompany_Mymodule>
<version>0.1.0</version>
</Mycompany_Mymodule>
</modules>
<frontend>
<routers>
<mycompany>
<use>standard</use>
<args>
<module>Mycompany_Mymodule</module>
<frontName>mycompany</frontName>
</args>
</mycompany>
</routers>
</frontend>
</config>
[/code]

You will see that we specify a frontend router, called mycompany, indicating that the frontName mycompany should direct the code to Mycompany_Mymodule.

Now, create the controller file in /app/code/local/Mycompany/Mymodule/controllers/PageController.php (the file can be named for whatever controllername you want to specify after the frontname on the url line, in this case the controllername would be “page” all lowercase. The file name should be your controllername with an uppercase first letter, followed by Controller.php)
[code lang=”php”]
<?php
class Mycompany_Mymodule_PageController extends Mage_Core_Controller_Front_Action
{
public function indexAction()
{
$this->getResponse()->setRedirect(‘/products.html’);
}
}
[/code]

Here we define a class named after your module name and front controller name. In that class we have an indexAction() method, which is what gets called if you don’t specify an action name (like mycompany/page). If you want a different action name (like mycompany/page/discounts) you would create a method called discountsAction(). In our indexAction method above, we do a simple redirect, which will send the website visitor to whatever url we want, with no Magento shenanigans to mess with us. Set the admin Default Web Url to “mycompany/page” and it will fire the indexAction and redirect our visitor! Here we have it hard-coded, but it would be simple to set up a definable config setting in admin, if you want a fully-functional module.

 

Jersey Express – Magento eCommerce Integration

Screenshot of the new Jersey Express Magento siteThis project was a Magento eCommerce platform integration for Jersey Express. The client supplied the new site’s graphical design. My task was to apply the look and feel to Magento, re-arranging widgets and functionality as needed. I try to code standards-compliant XHTML and CSS. We’d be in a real mess without standards! There are some cases where I cannot, due to special requests and pre-existing conditions – for this particular site, we were trying to keep costs as low as possible, so I didn’t take the time to ensure validation and correct every error. The design came together nicely, although the cut-up ended up not being very helpful. The Magento templating system is far more complex then you’d think.

There were a number of custom programming challenges. Magento is written in fully object-oriented PHP, using the Model-View-Controller pattern throughout. On one hand, it’s amazingly awesome to work with; flexible and powerful, without the ugly code in so many other systems. On the other hand, it can be nightmareish to get your head around, due to the size and complexity of the system. Also, being an open source solution, the documentation isn’t what you’d expect from a commercial solution. The initial learning curve for the programmer is rather steep. I liken it to learning to ride a dragon. Very difficult to do, dangerous until you get good at it, but once you have it down, you wield amazing power.

I ended up rewriting a few modules for this site, specifically to get the price calculations in line with customer requests, and to get the menus and catalog to be dynamic in just the right way. It also took a custom piece of code to import several thousand configurable products and their associated simple products. Not easy to do at all, but totally worth the time.

I look forward to the next Magento project! This was fun. If you want a sports jersey, by the way, check these guys out. They are the official manufacturers for the pro sports teams, so what you get and the quality of workmanship is top-shelf. I’ve visited their factory several times, and can vouch for it myself. Tell them I sent you!