OpenCart – Export Order to XML

In my term at Opencart Forums as a moderator I have seen many people come to the forums over the years requesting technical assistance on the following: how to migrate order data to a 3rd party fulfillment house after an initial order has taken place.

What is fulfillment?

A fulfillment house is a service you will find outside of the USA that typically acts as a central HUB for goods being shipped out to customers. Some fulfillment houses actually take care of payment processing as well such as OGONE “one of the largest payments processors / fulfillment houses in Europe. In the USA it would be a shipping facility or warehouse that holds a store owners inventory waiting to be shipped out to addresses spanning the USA or even the Globe. All of these establishments whether it be in EUROPE or the USA use major shipping providers such as DHL, USPS, UPS, or FED EX.

Fulfillment requires order data from an Opencart store to be able to set up a shipment whereby getting the order to the customer on time and in good condition. This order data must be sent to the Fulfillment houses servers over as a secure/reliable method “which does NOT include email”. Email is not totally reliable. However, generating an XML file that can be sent to a web folder on a server “which can later be accessed by fulfillment” is very secure and reliable. That is what this article is about. The central question: how do we export all of an orders data to an XML, which is then sent to a folder, which is then accessible by a shipment house?

What is XML?

  • XML stands for Extensible Markup Language
  • XML is a markup language much like HTML
  • XML was designed to carry data, not to display data
  • XML tags are not predefined. You must define your own tags
  • XML is designed to be self-descriptive
  • XML is a W3C Recommendation

In short, XML is a great way to hold this data let alone be parsed by a 3rd party like fulfillment.

Generating the XML Document

For this tutorial I will be using code examples from a project I did a few years back that uses a slightly older version of opencart. We will be using the following file: catalog/model/checkout/order.php

So again, what we are going to do is pass on individual order data to an XML document and stick that document in a web folder that can be accessed by the person/company of your choice via secure FTP. The first step is knowing where to place the PHP CODE that generates the data. Naturally, you will want to place this code towards the end of your order processing after the order table has been updated and an order completed. Clearly, you can set it up to file under a specific order status ID such as PENDING, PROCESSING etc but I chose COMPLETE which would be for most. Opening your PHP FILE look for this line of code:

foreach ($emails as $email) {
if ($email && preg_match(‘/^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i’, $email)) {


The above code is the last procedure of the default order.php file which sends out confirmation emails to an admin or admin(s). This is a great place to insert our new PHP that will generate the XML. Here is the code that will generate the XML with specific order data. If you want more or different order data then simple add to the code:


$date=date($language->get(‘date_format_short’), strtotime($order_info[‘date_added’]));
//Initial variables for fullfillment
$xml = new XMLWriter;
$xml->setIndentString(‘  ‘);


//Write Static Elements

$xml->writeAttribute(‘version’, ‘1’);
$xml->writeAttribute(‘encoding’, ‘utf-8’);
$xml->writeAttribute(‘xmlns’, ‘’);
$xml->writeElement(‘OrderDate’, $date);
$xml->writeElement(‘OrderID’, $order_id);
$xml->writeElement(‘OrderTotal’, $result[‘text’]);
$xml->writeElement(‘FirstName’, $order_info[‘firstname’]);
$xml->writeElement(‘LastName’, $order_info[‘lastname’]);
$xml->writeElement(‘Email’, $order_info[’email’]);
$xml->writeElement(‘Phone’, $order_info[‘telephone’]);
$xml->writeElement(‘Shipping-Address1’, $order_info[‘shipping_address_1’]);
$xml->writeElement(‘Shipping-Address2’, $order_info[‘shipping_address_2’]);
$xml->writeElement(‘Shipping-City’, $order_info[‘shipping_city’]);
$xml->writeElement(‘Shipping-State’, $order_info[‘shipping_zone’]);
$xml->writeElement(‘Shipping-Zipcode’, $order_info[‘shipping_postcode’]);
$xml->writeElement(‘Shipping-Country’, $order_info[‘shipping_country’]);

//Set up array for items in the order

foreach ($order_product_query->rows as $product) {

$xml->writeElement(‘name’, $product[‘name’]);
$xml->writeElement(‘model’, $product[‘model’]);
$xml->writeElement(‘quantity’, $product[‘quantity’]);


$doc = $xml->flush();

file_put_contents(‘/home/purx/public_html/dataXMLORD/Order’.’_’.$order_id.’.’.’xml’, $doc);

This line places the XML file in a directory on your server:

file_put_contents(‘/home/name/public_html/dataXMLORD/Order’.’_’.$order_id.’.’.’xml’, $doc);

We’re using php’s XML WRITER in order to do the XML. Documentation can be found at

I hope you’ve enjoyed this tutorial

Looking for quality OpenCart Hosting? Look no further than Arvixe Web Hosting!

Tags: , , , , , | Posted under OpenCart | RSS 2.0

Author Spotlight

Joe Stenhouse

I am a web application developer that specializes in PHP, JAVASCRIPT, MYSQL, HTML, and CSS. We manifest exciting potentials within the world wide web through means of innovation.

10 Comments on OpenCart – Export Order to XML

  1. Derrik Willett says:

    I really need this code above to work, but when I paste it i my order.php file it is incomplete and throws of the code below it.

    Do you have any suggestions?

  2. Sally says:

    This is exactly what I need. The fulfillment centre asks for it to be sent to an IP address. How can this be done?

    Thanks for this…I’ve been searching forever how to make this work!

  3. Sally says:

    Joe, are you available to do custom work on this, or do you know someone who has this experience?

  4. Stefan says:

    Nice work. Just one question. How can you show your coupon discount apart from your order total in the XML. In the order.php there is no code for coupon only for vouchers.

    I’m using OC

  5. George says:


    This is EXACTLY what I need but I can’t get it to work, could you help me please? I have tried contacting you on the e-mail you provided above but couldn’t reach you.


Leave a Reply

Your email address will not be published. Required fields are marked *