Magento: Adding custom attributes to orders

I’m currently working on a little Magento side project. The learning curve is still steep, and still frustrating. A big part of it involves creating custom fields on orders.

I’m currently working on a little Magento side project. The learning curve is still steep, and still frustrating. A key part of it involves creating custom fields on orders.

Better Magento developers than me have already gone this route, so I didn’t need to reinvent the wheel. Alan Storm’s excellent post on setup resources was a huge help. I’ll just note a couple of things: first, the actual setup class name could in fact be anything, it doesn’t have to follow the Company_Module_Model_Mysql4_Setup pattern. All you need is to place it in the location in your directory that corresponds to the class name (as with all Magento classes).

Second, it bears repeating: the installer script will only run if the version stored in the core_resource table is different from the version being installed. To run the installer multiple times, simply delete the appropriate record from core_resource, then clear the cache (if it’s enabled) and refresh any page. Having only written one other extension with an installer script in my career, I’d forgotten that particular bit.

Third, and this also bears repeating: damn, but Magento is not friendly to developers. Usual Magento bitching aside (convoluted config.xml, overcomplex extension class/directory structure), why is it making developers do extra work, like writing:

$installer = $this;

…in every installer script? It’s just a little thing, you say? Maybe, but it’s one of many unnecessary little things, one more little annoyance for experienced developers, one more thing novice devs need to remember, making the learning curve just a little bit steeper.

So that’s general instructions for the installer. Now, how do I specifically add this custom field? I got my answer from the first post on this page. The logic goes like this:

$setup = new Mage_Eav_Model_Entity_Setup('core_setup');
$setup->addAttribute('order', 'newfield', array(
    'position'             => 1,
    'type'              => 'text',
    'label'                => 'Your New Field',
    'global'            => 1,
    'visible'           => 1,
    'required'          => 0,
    'user_defined'      => 1,
    'searchable'        => 0,
    'filterable'        => 0,
    'comparable'        => 0,
    'visible_on_front'  => 1,
    'visible_in_advanced_search' => 0,
    'unique'            => 0,
    'is_configurable'   => 0,
    'position'          => 1,

Pretty self-explanatory, right? Note, though, one thing that tripped me up before I found the above link: the first argument of addAttribute() is a string, not an integer. I’d expected to have to enter the entity_type_id corresponding to orders (5, in my system), but no, it looks like it needs the entity_type_code (‘order’). Weird, and against my common sense.

So there you go, now you know how add custom fields to orders.

PS: here’s some more Magento bitching. This whole EAV scheme, what exactly is it good for? Because frankly, so far it’s given me nothing but headaches. It makes the db bloated and slow, makes looking up data extremely time-consuming, and does not make my life as a developer easier. Like, at all. Other e-commerce platforms (*coughUbercartcough*) use simpler and saner systems that are just as flexible, if not more so.

PPS: While playing around with this extension, I discovered some weird / amusing behaviour related to installing & uninstalling extensions. Since this has nothing to do with custom attributes or installer scripts per se, I’m saving it for another post.

Comments are closed.