2

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.="<DeliverTo>\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.="</Order>";
            $out.= "</Orders>\n";
        //$out.= "</dataPackItem>\n\n";
};
3

You can get the shipping with:

$order->getShippingMethod(true);

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.

3

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

$order->getShippingMethod()

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

$order->getShippingMethod(true);

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"?>
<order>
    <entity_id>8</entity_id>
    <state>new</state>
    <status>pending</status>
    <protect_code>x2fsdfsdg</protect_code>
    <shipping_description>Flat Rate - Fixed</shipping_description>
    <reward_points_balance>0</reward_points_balance>
    <total_item_count>1</total_item_count>
    <reward_currency_amount>0.0000</reward_currency_amount>
    <grand_total>2085.0000</grand_total>
    <base_shipping_incl_tax>500.0000</base_shipping_incl_tax>
    <subtotal_incl_tax>1585.0000</subtotal_incl_tax>
    <store_to_order_rate>1.0000</store_to_order_rate>
    <total_qty_ordered>100.0000</total_qty_ordered>
    <billing_address_id>15</billing_address_id>
    <quote_id>25</quote_id>
    <shipping_address_id>16</shipping_address_id>
    <weight>25.0000</weight>
    <increment_id>100000008</increment_id>
    <store_currency_code>USD</store_currency_code>
    <customer_email>youremail@domain.com</customer_email>
    <customer_firstname>Phillip</customer_firstname>
    <customer_lastname>Jackson</customer_lastname>
    <remote_ip>1.1.1.1</remote_ip>
    <shipping_method>flatrate_flatrate</shipping_method>
    <store_name>Main Website
        Main Website Store
        Default Store View</store_name>
    <updated_at>2014-02-03 15:41:10</updated_at>
    <gift_cards>a:0:{}</gift_cards>
</order>

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.