/* 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 $emptyBoxWeight = 0; //in lb $defaultBoxL = 8.25; $defaultBoxW = 6; $defaultBoxH = 4; $w = 0; foreach ($DATA['items'] as $i) { $w += $i['quantity']*round($i['grams']/1000*2.20462,2); } $isresidential = true; if ($DATA['destination']['company_name'] !== null && $DATA['destination']['company_name'] != '') $isresidential = false; //use UPS if ($w < 1) $w = 1; $serviceCode = '92'; //surepost service code, 92,93,94,95 are valid codes $packages = array( array('weight'=>($w+$emptyBoxWeight), 'length'=>$defaultBoxL, 'width'=>$defaultBoxW, 'height'=>$defaultBoxH), ); $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']); $r = $ups->getRateSurepost($packages,$serviceCode) { if ($r) { foreach ($r as $_r) { $_RATES[] = array( "service_name" => $_r['name'], "service_code" => $_r['code'], "total_price" => $_r['amount']*100, //in cents "currency" => "USD", ); } } return $_RATES; /* do not edit below this line */ }