I have a requirement to create a scheduled task that will send orders (since the last time the task ran) to an external web service. The data needs to be sent in cXML format and one xml per order. It needs to be the cXML formatted data rather than actual file.

I'm looking for some advice really on how best to accomplish this. I have created a base module with a cronjob that will run every 5 minutes.

I have a Model/Export.php as follows that is pretty basic at the moment to just get orders with specific statuses;

class OrderSync_Model_Export

    public function ExportSales()
        //A collection of orders
        $order = Mage::getModel('sales/order')->getCollection();
        $order->addAttributeToFilter('status', array('in' => array('pending','complete')));
        $orders = $order->getItems();

I obviously then need to get the specific data from each order but the part i'm not sure is how to get/send each individual order as cXML ( http://cxml.org/) to the web service. I assume I would probably need to cURL this to the web service but it's the part about sending each individual order when the cronjob runs that i'm not so sure of and wondered if someone might be able to help?

Also, the web service returns a response for each order cXML that it receives like so and I'd like to be able to update an order attribute with the status detail (code and text) if anyone has any pointers for this too?

The example response is as follows;

<?xml version="1.0" encoding="utf-16"?>
<cXML payloadID="int.coupahost.com" xml:lang="en" timestamp="2017-04-06T11:43:25.2681984+01:00">
    <Status code="500" text="Price does not match current pricelist- 653244    Possibly out of date Pricelist, Please contact supplier for updated pricelist " />
  • Hi @doubleplusgood it looks to me that your are using Magento 1.x. In order to help with an accurate answer, could you confirm what version are you using?
    – diazwatson
    Jul 25 '17 at 7:10
  • @diazwatson It is indeed Magento 1 - Magento CE. Apologies, I thought I had entered this into the post because I ran out of tags to use. Jul 25 '17 at 8:19

I guess you could have a look and try to use the SimpleXMLElement object to build your xml.

Here is an example

 public function ExportSales()
        //A collection of orders
        $orderCollection = Mage::getModel('sales/order')->getCollection();

        ->addAttributeToFilter('status', array('in' => array('pending', 'complete')))

        // Filter by lastRun
        ->addFieldToFilter('start_date', array('gteq' => $lastRun))
        ->addFieldToFilter('end_date', array('lteq' => $current))

       // Filter by Non exported (Even better)
        ->addFieldToFilter('exported', array('eq' => '0'));

        $orders = $orderCollection->getItems();

        foreach ($orders as $order) {
            $array = $order->toArray();

            // lib/Varien/Simplexml/Element.php
            $xml = new SimpleXMLElement('<order/>');

            //necessary to flip keys/values for conversion
            $array = array_flip($array);
            array_walk_recursive($array, array($xml, 'addChild'));

            echo $xml->asXML();

This will generate the following structure

<?xml version="1.0"?>
    <shipping_description>Select Shipping Method - UK Delivery</shipping_description>
    <store_name>Magento Store</store_name>
    <updated_at>2017-07-21 03:49:41</updated_at>
    <mailchimp_sync_delta>0000-00-00 00:00:00</mailchimp_sync_delta>

I know this is not a valid cXML structure but you can parse it and modify it before send it to the external web service.

  • Thanks Diaz. A great help. Do you know how I might then get the response from the xml that comes back from the web service and update an attribute of the order with the status code and message from the response xml? Jul 26 '17 at 8:03
  • You can also use the SimpleXml object to work with the xml that comes as a response from the web service. Then you can update you order using $order->setData(). Here is an example. magento.stackexchange.com/questions/133027/…
    – diazwatson
    Jul 26 '17 at 8:32
  • Happy to help. If this answer or any other one helped to solve your issue, please mark it as accepted.
    – diazwatson
    Jul 26 '17 at 12:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.