Hi I'm using this code to get order in xml can any on help ho to get shipping method in xml file

foreach($collection as $order)

     if ($billingAddress = $order->getBillingAddress()){
            $billingStreet = $billingAddress->getStreet();
        if ($shippingAddress = $order->getShippingAddress()){
            $shippingStreet = $shippingAddress->getStreet();

    //$out .= "<dataPackItem  version=\"2.0\">\n";
    //$out .= "<dat:dataPackItemversion=\"1.0\">\n";
            $out.= "<Orders>\n";

                    $out.= "<Order type='V'>";

                        $out.= "<Description>WB Web Order ".$order->getIncrementId()."</Description>\n";
                        $out.= "<Reference>".$order->getIncrementId()."</Reference>\n";
                            $out.= "<OrderBy>\n";
                                $out.= "<Date>".date('Y-m-d',strtotime($order->getCreatedAt()))."</Date>\n";
                            $out.= "</OrderBy>\n";
                            $out.= "<name>{$shippingAddress->getName()}</name>\n";
                            $out.= "<street>{$shippingStreet[0]}</street>\n";
                            $out.= "<PostalCode>{$shippingAddress->getPostcode()}</PostalCode>\n";
                            $out.= "<city>{$shippingAddress->getCity()}</city>\n";
                        $out.= "</DeliverTo>\n";
                    $out.= "<paymentType> \n";
                        $out.= "<ids>{$order->getShippingDescription()}</ids>\n";
                    $out.= "</paymentType>\n";
                $out.= "<orderDetail> \n";
                foreach ($order->getAllItems() as $itemId => $item){
                    $out.= "<orderItem> \n";
                        $itemname =  $item->getName();
            $itemname =  str_replace('&', " ", $itemname);
            $out.= "<text>{$itemname}</text> \n";
                        $out.= "<quantity>{$item->getQtyOrdered()}</quantity>\n";
                        //$out.= "<delivered></delivered>";
                       // $out.= "<rateVAT>{$item->getTax()}</rateVAT> \n";
                        $out.= "<homeCurrency> \n";
                            $out.= "<unitPrice>{$item->getPrice()}</unitPrice>\n";
                        $out.= "</homeCurrency>\n";
                        $out.= "<stockItem>\n";
                            $out.= "<stockItem>\n";
                                $out.= "<ItemCode>{$item->getSku()}</ItemCode>\n";
                            $out.= "</stockItem>\n";
                        $out.= "</stockItem>\n";
                    $out.= "</orderItem>\n";
                $out.= "</orderDetail>\n";

                $out.= "<Delivery>\n";
                    $out.= "<DeliveryMethod>{$item->getShippingDescription()}</DeliveryMethod>\n";
                $out.= "</Delivery>\n";

               // $out.= "<orderSummary>\n";
               //     $out.= "<roundingDocument>math2one</roundingDocument>\n";
               // $out.= "</orderSummary>\n";
            $out.= "</Orders>\n";
        //$out.= "</dataPackItem>\n\n";

You can get the shipping with:


Then you have an object of type Mage_Sales_Model_Order_Shipment, but there is no name or something in it.

The code of the shipping method can bet fetched via $order->getShippingMethod()

I have no idea, what is in $order->getShippingDescription, but maybe it is worth to check.


To answer your question, you can get the shipping method code with:


And you can get that shipping method's information by calling:


But I think Fabian already pointed this out and you should upvote and accept his answer. I'm only answering because of the following:

If I may be so bold - there are many ways of creating XML and this is probably the least maintainable. You shouldn't be concatenating strings to make XML these days in PHP. Instead, create a keyed array that represents your data and convert it to XML via SimpleXML at the end.

A great, and trivial, example (taken from https://stackoverflow.com/questions/1397036/how-to-convert-array-to-simplexml):

//get the latest order
$order = Mage::getModel('sales/order')->getCollection()->getLastItem();

$array = $order->toArray();
$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();

Which outputs:

<?xml version="1.0"?>
    <shipping_description>Flat Rate - Fixed</shipping_description>
    <store_name>Main Website
        Main Website Store
        Default Store View</store_name>
    <updated_at>2014-02-03 15:41:10</updated_at>

This is just an example and I wouldn't use it in production due to the array_flip requirement -- but hopefully you'll see what I'm driving at. There are many ways to achieve the same output but have maintainable, legible code. Your requirement requires the keys to be specifically named, and perhaps you can have an adapter method that would provide a mapping of keyname to keyvalue.

Or, you know, you could just use SOAP :)

If you hit that SO link you'll see many other ways to achieve similar functionality. I highly encourage you review this.

Best of luck.

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.