Shopatron & Magento integration

At the moment I’m in in the middle of integrating a Magento installation with the Shopatron fulfilment and ordering system. Shopatron – so far I’ve decided that it sounds either like;

  • Megatron’s high maintenance Transformer mistress (who he wines and dines whilst poor old Mumatron is at home minding the Kidatrons).
  • Magento vs Shopatron – that was Wrestlemania 7, right?
  • Some kind of unscary android baddie from a cheap eighties Sci-Fi show, that maybe looks a bit like Metal Mickey or the aliens from the Smash adverts.

Anyway, it’s quite an interesting concept – basically it’s a system which allows manufacturers to start selling online without cannibalising the sales in their distribution chain, pissing off off the retailer clients who they are wholesaling to.

Shopatron have two alternative methods by which you can pass orders across. Their API documentation is available here;

http://service.shopatron.com/display/KBMFG2/API+Guides

The first one involves fully processing an order within Magento and then passing the details of the completed order across to Shopatron when checkout is complete. This is the Immediate Order Processing API. If you are going this route you would need to manage all your inventory within the Magento site, set up your shipping, tax and payment methods. There’s quite a lot of available threads on going this route as you would be using a Magento observer based on the checkout_submit_all_after observer.

The method I am using in the Magento – Shopatron e-commerce site I am currently building is their other method – using the Online Order Processing API. In this case none of the shipping, tax or billing is handled by Magento, the user builds their order in Magento and then when they go to checkout an XML package is created containing the order data, Shopatron manufacturers ID and currency code. You connect to the Shopatron Server using XML-RPC and pass across the data, Shopatron’s server processes this information and returns a success code, upon which you redirect the user to a dedicated Shopatron url to process the order and checkout off-site.

In this instance you set up a Magento module using an observer based on the controller_action_predispatch_checkout_onepage_index event. Magento controllers inherit the preDispatch method which fires a generic event for predispatch, and a specific event based on the requested action path. This controller is part of the checkout module and is called onepage with a default action of index – this is the basis for the Magento controllers naming structure.

That’s how you do it keeping everything tied into the Magento system. When you first look at it you see that in templates/checkout/onepage/link.phtml the process for passing the cart contents to the onepage checkout is a Javascript onclick event. The temptation is to look at a way of altering that to create a form submit or AJAX event which builds the XML using procedural PHP and then redirects the customer to Shopatron. The trouble with that is going that route takes you right out of the Magento system, which you don’t need or want to do.

The final act is to create another observer in your module which has an observer based on the controller_action_predispatch_cms_page_view event. When the order is complete on Shopatron the redirect url takes the customer back to the Magento site to a specific CMS page thanking them for their order. The observer calls a model which assesses the page ID the user is on – if it’s the successful transaction page ID then it empties the cart using;

Mage::getSingleton('checkout/session')->clear();

Which ensures that the user doesn’t mistakenly pass through checkout again and reorder.