2

I'm using a PHP wrapper API for ups' rating api, not the ups dev kit directly. I am trying to echo the "MonetaryValue" of the shipping quote contained in this array.

Trying to get value of:

["RatedShipment"][0]["RatedPackage"][0]["TotalCharges"]["MonetaryValue"]

I'm in a little over my head as I've never work with an array of this complexity. However non-complex it may be.

Array output:

object(Ups\Entity\RateResponse)#60 (1) { 
    ["RatedShipment"]=> array(1) { 
        [0]=> object(Ups\Entity\RatedShipment)#61 (11) { 
            ["Service"]=> object(Ups\Entity\Service)#62 (3) { 
                ["Description"]=> NULL 
                ["code":"Ups\Entity\Service":private]=> string(2) "03" 
                ["description":"Ups\Entity\Service":private]=> NULL 
            }

            ["RateShipmentWarning"]=> string(56) "Your invoice may vary from the displayed reference rates" 

            ["BillingWeight"]=> object(Ups\Entity\BillingWeight)#68 (2) { 
                ["UnitOfMeasurement"]=> object(Ups\Entity\UnitOfMeasurement)#70 (4) { 
                    ["Code"]=> string(3) "LBS" 
                    ["Description"]=> NULL 
                    ["code":"Ups\Entity\UnitOfMeasurement":private]=> string(3) "LBS" 
                    ["description":"Ups\Entity\UnitOfMeasurement":private]=> NULL 
                }

                ["Weight"]=> string(4) "10.0" 
            }

            ["TransportationCharges"]=> object(Ups\Entity\Charges)#63 (5) { 
                ["CurrencyCode"]=> string(3) "USD" 
                ["MonetaryValue"]=> float(9.84) 
                ["Code"]=> NULL 
                ["Description"]=> NULL 
                ["SubType"]=> NULL 
            } 

            ["ServiceOptionsCharges"]=> object(Ups\Entity\Charges)#65 (5) { 
                ["CurrencyCode"]=> string(3) "USD" 
                ["MonetaryValue"]=> float(0) 
                ["Code"]=> NULL 
                ["Description"]=> NULL 
                ["SubType"]=> NULL 
            } 

            ["TotalCharges"]=> object(Ups\Entity\Charges)#66 (5) { 
                ["CurrencyCode"]=> string(3) "USD" 
                ["MonetaryValue"]=> float(9.84) 
                ["Code"]=> NULL 
                ["Description"]=> NULL 
                ["SubType"]=> NULL 
            } 

            ["GuaranteedDaysToDelivery"]=> NULL 
            ["ScheduledDeliveryTime"]=> NULL 
            ["RatedPackage"]=> array(1) { 
                [0]=> object(Ups\Entity\RatedPackage)#67 (5) { 
                    ["Weight"]=> string(4) "10.0" 
                    ["BillingWeight"]=> object(Ups\Entity\BillingWeight)#74 (2) { 
                        ["UnitOfMeasurement"]=> object(Ups\Entity\UnitOfMeasurement)#76 (4) { 
                            ["Code"]=> string(3) "LBS" 
                            ["Description"]=> NULL 
                            ["code":"Ups\Entity\UnitOfMeasurement":private]=> string(3) "LBS" 
                            ["description":"Ups\Entity\UnitOfMeasurement":private]=> NULL 
                        } 

                        ["Weight"]=> string(4) "10.0" 
                    }

                    ["TransportationCharges"]=> object(Ups\Entity\Charges)#64 (5) { 
                        ["CurrencyCode"]=> string(3) "USD" 
                        ["MonetaryValue"]=> float(9.84) 
                        ["Code"]=> NULL 
                        ["Description"]=> NULL 
                        ["SubType"]=> NULL 
                    } 

                    ["ServiceOptionsCharges"]=> object(Ups\Entity\Charges)#71 (5) { 
                        ["CurrencyCode"]=> string(3) "USD" 
                        ["MonetaryValue"]=> float(0) 
                        ["Code"]=> NULL 
                        ["Description"]=> NULL 
                        ["SubType"]=> NULL 
                    } 

                    ["TotalCharges"]=> object(Ups\Entity\Charges)#72 (5) { 
                        ["CurrencyCode"]=> string(3) "USD" 
                        ["MonetaryValue"]=> float(9.84) 
                        ["Code"]=> NULL 
                        ["Description"]=> NULL 
                        ["SubType"]=> NULL 
                    } 

                } 

            } 

            ["SurCharges"]=> array(0) { } 
            ["NegotiatedRates"]=> NULL 
        } 

    } 

} 

Code to get the above output:

$rate = new Ups\Rate(
    $accessKey = '###removed#for#post###',
    $userId = '###removed#for#post###',
    $password = '###removed#for#post###'
);

try {
    $shipment = new \Ups\Entity\Shipment();

    $shipperAddress = $shipment->getShipper()->getAddress();
    $shipperAddress->setPostalCode('99205');

    $address = new \Ups\Entity\Address();
    $address->setPostalCode('99205');
    $shipFrom = new \Ups\Entity\ShipFrom();
    $shipFrom->setAddress($address);

    $shipment->setShipFrom($shipFrom);

    $shipTo = $shipment->getShipTo();
    $shipTo->setCompanyName('Test Ship To');
    $shipToAddress = $shipTo->getAddress();
    $shipToAddress->setPostalCode('99205');

    $package = new \Ups\Entity\Package();
    $package->getPackagingType()->setCode(\Ups\Entity\PackagingType::PT_PACKAGE);
    $package->getPackageWeight()->setWeight(10);

    $dimensions = new \Ups\Entity\Dimensions();
    $dimensions->setHeight(10);
    $dimensions->setWidth(10);
    $dimensions->setLength(10);

    $unit = new \Ups\Entity\UnitOfMeasurement;
    $unit->setCode(\Ups\Entity\UnitOfMeasurement::UOM_IN);

    $dimensions->setUnitOfMeasurement($unit);
    $package->setDimensions($dimensions);

    $shipment->addPackage($package);

    var_dump($rate->getRate($shipment));

} catch (Exception $e) {
    var_dump($e);
}
Rizier123
  • 58,877
  • 16
  • 101
  • 156

0 Answers0