Magento

Magento Order Export Mapping

Published on

My first exposure to Magento as an integration project.

Here I am going to walk through the process of a recent project where I integrated the Magento Checkout process and exported orders to a third party system.

Architecture

Target

The inventory management system is running an MSSQL database. There were API’s, but the database was friendly enough that we are going straight at the DB and skipping a third party API. This means that we will be mapping to a SQL table or tables as our target.

Source

For the most part, Magento is the source. The Magento store was a fairly standard install, with not custom or third-party extensions that altered the order processing or structure. There are a couple of places where the Database is also part of the source and we have to look up information before we can complete our mapping to a target.

Location

This is a pushed initiated event. There were three options. We could push the order each time someone checks out. (This is what we are doing here.) Or we could have the materials/inventory system pull orders on a user request. (The inventory system doesn’t have an option for this and we didn’t want to build it anyway.) Or some batched or scheduled process that would pull from Magento and push to the inventory database (planned for future). There are no wrong ways, it depends on what you need.

We are pushing from Magento on checkout events.

The Trigger

Magento has a great facility called observers that can be used to hook in too many existing events. And by extending the core classes, new events can be dispatched so that you can create an observer to hook into them as well.

This makes Magento a great place to push from as you can have a lot of control over how and when to initiate your integration tasks. (This also makes Magento a fairly decent place to pull into, but that is not what we are doing here.)

However, as with many things in Magento, you have to get your naming structure and configuration files in the right place with the right names or your observer will just sit and do nothing.

Mapping Methods

This is what the mapping looks like. In Observer.php

<?php
$event = $observer->getEvent();
$order = $event->getOrder();
$DOC_ID = $order->getIncrementId();
$DOC_DT = $order->getCreatedAt();
$numOfLines = 0;
foreach($order->getItemsCollection() as $prod){
$numOfLines++;
}
Mage::log(“This order has “.$numOfLines.” lines.”);
$ORD_LINS = $numOfLines;
$qry = 'INSERT INTO ORD_HDR ("ID","DATE","ORD_LINS")
VALUES ("'.$DOC_ID.'","'.$DOC_DT.'","'.$ORD_LINS.'")';

Obviously this is a simple example.

Summary

So first I use the observer to get to the order. Then I make a list of variables and map values into them. This helps me stay sane as making changes goes into the variable mapping as is not done in the creation of the insert statement. This allows me to clearly map some data directly from the Magento order object to the variable, hard code others, and do quick logic to derive or format other data while mapping it to its variable.
The insert statement just maps the columns to the variables. Now we have a SQL statement and we can call an API, build our own, log it, write it to a file, or use any method that we want to execute the statement against the database.


Rukhsar Manzoor

Get in Touch

I am available for consulting, collaboration and contract work. Feel free to contact me if you have a project in mind.

Just say “Hi” to

Email [email protected] or Twitter @RukhsarMan