Fix or extend core Magento in an upgrade-safe wayNov 20 2010 | 09:22:03 | No Comments

Ah, open source.  The Magento core codebase, layouts and templates are right there, warts and all, just daring you to give in to temptation and ignore the “do not edit this file!” warning at the top of each distributed file.  We all know the guilty pleasure of instant gratification that comes with tweaking a word, or a layout, or a core class directly in the source and seeing our fix immediately on the site.  It seems especially rewarding after the hour or two it took just to hunt down the right cog in The Machine.  And really, who has time to learn entire frameworks and build a module, just to tweak the way a breadcrumb gets output?

But then it’s 500 (or 5,000!) lines of “little tweaks” later and you need to upgrade.  And you’re screwed.  And a couple extra xml files and a separate tree of folders and files doesn’t seem like such a bad idea anymore.

Well, okay, time to roll up your sleeves and learn this durn extension framework that the brainiacs at Varien/Magento have so lovingly constructed.

Layouts and Templates and Blocks, Oh My has a great article on how to modify one or two things in a core layout without (1) modifying it directly in core or (2) having to make a copy of the whole layout file into your theme just to tweak one line:

Now that I understand this layout stuff, I’m doing a lot less messing around directly in theme templates.  By the way, if you are editing the factory-supplied Magento templates and layouts directly, for shame!  Set up a theme and make all your changes in the theme.

Update: another great article on navigating the Magento layout framework:

OO PHP?  Isn’t that like oil and water?

The article linked below offers a lucid explanation of how to roll your own modules that extend the core Model, Controller, Resource, and Block classes (it’s a little different for each case) to override the core behavior in Magento. Not for beginners, you really need to have a handle on OO programming. Again, this is cool because you don’t have to edit a core file directly (where changes will get blown away in an upgrade) and you don’t have to copy the whole file just to tweak one function. The latter strategy prevents you from reaping the benefits of any bugfixes or new features in that file in the next upgrade, and could even cause serious problems/bugs as your copy gets out of sync with the core codebase.

For some reason that comprehensive article does not include instructions for overriding a function in a helper class (yet another special case). This article has an example for that:

Happy coding!

Calling Magento SOAP API from Perl with SOAP::LiteAug 02 2010 | 13:11:40 | 3 Comments

We had a legacy order fulfillment library written in Perl, so although accessing the API is extremely easy in PHP, when we migrated to Magento I had to figure out how to connect to the SOAP API from Perl.

Turns out the most popular CPAN module for doing SOAP in Perl (SOAP::Lite) is not particularly fun to use (see “State of the SOAP” at for a three-year-old call for volunteers to refactor).

Nevertheless, I did eventually emerge victorious. There may be a much easier way but after a lot of head-scratching and trial and error, this worked for me, YMMV.

The soapify routine was stolen from this page:
Many thanks to soapify author Sandeep Satavlekar.

For passing the filter args to sales_order.list, I had to make the call in PHP first and then reverse-engineer the encoding of the nested array that actually got passed. There may be a way to get SOAP::Lite to encode nested arrays like this by default (or encode nested arrays in some other standard SOAPy way that Magento would also accept) but I chose not to waste any more time on this than I already had.


UPDATE: Was getting sporadic “Access Denied” errors. Turns out you can fix this just by turning on cookies. Apparently this only affects you if Magento is using DB sessions. See new get_soap_session below for how to turn on cookies.

UPDATE TO THE UPDATE: Nah, that didn’t fix it. Still searching for a fix… runs fine in a session, fails about 80% of the time from cron.

The complete working example script is after the jump...


# replace these values with the soap account you set up in the admin
my $user = 'username';
my $pass = 'password';

# replace with your domain and path (we installed magento at /shop/)
my $soap_url = 'http://YOURDOMAIN/MAGENTO_PATH/index.php/api/index/index/';

my ($soap,$session_id) = &get_soap_session($user,$pass,$soap_url);

# simple example (one arg of type string)
my $oid = '1000001';
my $order = &get_order($soap,$session_id,$oid);

# gnarly example (argument is nested arrays)
my $orders = &get_recent_invoiced_orders($soap,$session_id);

foreach my $ord (@$orders) {
    my $due = $ord->{grand_total};
    my $paid_or_authorized = $ord->{total_invoiced};
    my $owed = $due - $paid_or_authorized;
    # ignore if not yet paid
    next if ($owed > .02);

exit 0;
Continue reading »

Customizing MagentoJul 08 2010 | 15:07:35 | No Comments

Quick start – here are some pages that helped me get going quickly:

Defining your own attributes is easy.  Getting them to display in a template is not.  Cheat sheet:

Don’t hack the Magento tree directly!  You’ll be sad when you upgrade and your customizations are gone. Do this instead:

Grab all the simple child products for a configurable product (and then display one of their other custom attributes):

Updating stock through the SOAP API is unbelievably slow.  The API doesn’t appear to accept more than one SKU per call! Use a script like this instead:

Some hints on how to get Magento to generate plain-text email:

Kicking off an index process from the command line (using a custom script):