/* This macro will be parsed as PHP code (see http://www.php.net) The calculateshipping function is called every time a shipping calculation request is made by Shopify. The function must return an array of available shipping options, otherwise no shipping options will be returned to your customers. */ function calculateshipping($DATA) { /* do not edit above this line */ $_RATES = array(); $_UPS_CLIENT_ID = "YOUR_CLIENT_ID"; $_UPS_CLIENT_SECRET = "YOUR_CLIENT_SECRET"; $_UPS_ACCOUNT = "YOUR_ACCOUNT_NUMBER"; //set to null if you DON'T want to use negotiated rates $w = 0; $t = 0; foreach ($DATA['items'] as $i) { $w += $i['quantity']*round($i['grams']/1000*2.20462,2); $t += $i['quantity']*$i['price']/100; } //default box size $packages = array( array('length'=>12, 'width'=>8.75, 'height'=>6, 'weight'=>$w), ); $isresidential = true; if ($DATA['destination']['company_name'] !== null && $DATA['destination']['company_name'] != '') $isresidential = false; $ups = new UPSRESTAPI($_UPS_CLIENT_ID,$_UPS_CLIENT_SECRET,$_UPS_ACCOUNT); $ups->setOrigin($DATA['origin']['city'],$DATA['origin']['province'],$DATA['origin']['postal_code'],$DATA['origin']['country']); $ups->setDestination($DATA['destination']['city'],$DATA['destination']['province'],$DATA['destination']['postal_code'],$DATA['destination']['country'],$isresidential,$DATA['destination']['address1']); $ups->getTransitTime = true; //fetch regular rates $r = $ups->getRate($packages,'IMPERIAL'); if ($r) { foreach ($r as $_r) { $eta = ''; if ($_r['businessdays'] != '') $eta = "Est. {$_r['businessdays']} business days"; $_RATES[] = array( "service_name" => $_r['name'], "service_code" => $_r['code'], "description" => $eta, "total_price" => ceil($_r['amount']*100), //in cents "currency" => "USD", ); } } //fetch access points within 5 miles of customer $range = 5; $location_options = array( 'LocatorRequest' => array( 'LocationSearchCriteria' => array( 'SearchOption' => array( 'OptionType' => array( 'Code' => '01', //service level option (01=full service centers, 02=shipping & dropoff center, 05=all, 06=self service center) ), 'OptionCode' => array( 'Code' => '001', //location type (001=customer center, 002=ups store, 003=drop box, 018=access point, 011=access point/kiala point/ups paket shop ), ), ), ), ); $ups->setOptions($location_options,'LOCATOR'); $locations = $ups->fetchLocationsFromUPS(trim("{$DATA['destination']['address1']} {$DATA['destination']['address2']}"),$DATA['destination']['city'],$DATA['destination']['province'],$DATA['destination']['postal_code'],$DATA['destination']['country'],$range); foreach ($locations as $l) { $_RATES[] = array( "service_name" => $l['type'].' '.$l['name'].' ('.$l['distance'].')', "service_code" => "{$l['name']}", "description" => trim("{$l['address']} ({$l['hours']})"), "total_price" => 0, //in cents "currency" => "USD", ); } return $_RATES; /* do not edit below this line */ }