0

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">
  <Response>
    <Status code="500" text="Price does not match current pricelist- 653244    Possibly out of date Pricelist, Please contact supplier for updated pricelist " />
  </Response>
</cXML>
  • 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 1.9.1.0 CE. Apologies, I thought I had entered this into the post because I ran out of tags to use. – doubleplusgood Jul 25 '17 at 8:19
1

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();

        $orderCollection
        ->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"?>
<order>
    <entity_id>231802</entity_id>
    <state>new</state>
    <status>pending</status>
    <protect_code>c4159c</protect_code>
    <shipping_description>Select Shipping Method - UK Delivery</shipping_description>
    <mailchimp_sync_modified>0</mailchimp_sync_modified>
    <total_item_count>1</total_item_count>
    <base_gomage_gift_wrap_refunded>0.0000</base_gomage_gift_wrap_refunded>
    <grand_total>8.4500</grand_total>
    <base_shipping_incl_tax>2.9500</base_shipping_incl_tax>
    <subtotal>4.5800</subtotal>
    <tax_amount>0.9200</tax_amount>
    <store_to_order_rate>1.0000</store_to_order_rate>
    <total_qty_ordered>2.0000</total_qty_ordered>
    <billing_address_id>460738</billing_address_id>
    <quote_id>556551</quote_id>
    <shipping_address_id>460739</shipping_address_id>
    <subtotal_incl_tax>5.5000</subtotal_incl_tax>
    <weight>40.0000</weight>
    <increment_id>100233364</increment_id>
    <store_currency_code>GBP</store_currency_code>
    <customer_email>customer@domain.com</customer_email>
    <customer_firstname>John</customer_firstname>
    <customer_lastname>Doe</customer_lastname>
    <remote_ip>99.99.99.1</remote_ip>
    <shipping_method>DHL</shipping_method>
    <store_name>Magento Store</store_name>
    <updated_at>2017-07-21 03:49:41</updated_at>
    <gift_cards>a:0:{}</gift_cards>
    <is_printed>No</is_printed>
    <mailchimp_sync_delta>0000-00-00 00:00:00</mailchimp_sync_delta>
    <mailchimp_sync_error></mailchimp_sync_error>
</order>
<order>
    ....
</order>

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? – doubleplusgood 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.