1

I was trying to get the information of tracking in the UPS. I got on of the method from the core (app/code/core/Mage/Usa/Model/Shipping/Carrier/Ups.php => method name getTracking($trackings)) that gives me the output that I want. But it's format in object form kind of as shown below.

So my question is how can I convert object in array?

Mage_Shipping_Model_Tracking_Result Object
(
    [_trackings:protected] => Array
        (
            [0] => Mage_Shipping_Model_Tracking_Result_Status Object
                (
                    [_data:protected] => Array
                        (
                            [carrier] => ups
                            [carrier_title] => United Parcel Service
                            [tracking] => 1Z90Y8A21294899248
                            [service] => UPS 3 Day Select
                            [shippeddate] => 20180910
                            [weight] => 6.00 LBS
                            [status] => Order Processed: Ready for UPS
                            [deliverydate] => 2018-09-10
                            [deliverytime] => 06:31:46
                            [deliverylocation] => 
                            [signedby] => 
                            [deliveryto] => US
                            [progressdetail] => Array
                                (
                                )

                        )

                    [_hasDataChanges:protected] => 1
                    [_origData:protected] => 
                    [_idFieldName:protected] => 
                    [_isDeleted:protected] => 
                    [_oldFieldsMap:protected] => Array
                        (
                        )

                    [_syncFieldsMap:protected] => Array
                        (
                        )

                )

        )

    [_error:protected] => 
)

I have already tried with the getData(), also (array) $obj but that's not working giving blank responce.

1 Answer 1

1

All Magento Models inherit from the Varien_Object class. Magento Models store their data in a protected _data property. The Varien_Object class gives us several methods we can use to extract this data. You will see getData, which will return an array of key/value pairs.

You can use below code to get data from object in array format

$array = $object->getData();

This will give you array of object.

Below code give you reference how you can get object data within object

if ($trackings = $this->_result->getAllTrackings()) {
      foreach ($trackings as $tracking){
         if($data = $tracking->getAllData()){
             if (isset($data['status'])) {
                $statuses .= Mage::helper('usa')->__($data['status']);
             } else {
               $statuses .= Mage::helper('usa')->__($data['error_message']);
             }
          }
      }
  }
7
  • Hi Prashant, I have already tried with he getData(), It's giving blank response. Commented Sep 15, 2018 at 6:22
  • Updated answer please check it Commented Sep 15, 2018 at 6:28
  • What will be $this->_result for me, I am getting tracking information in my custom controller? Commented Sep 15, 2018 at 6:33
  • Your object which you have var_dump Commented Sep 15, 2018 at 6:48
  • I have checked it's the code form the responce() function in the ups.php model.I am using the below code , $ups = Mage::getSingleton('usa/shipping_carrier_ups')->getTracking($track); if ($trackings = $ups->getAllTrackings()) { foreach .... } It's showing blank page. Commented Sep 15, 2018 at 6:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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