Вопрос

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>
Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с magento.stackexchange
scroll top