/* 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(); //calculate total weight $w = 0; foreach ($DATA['items'] as $i) { $w += $i['quantity']*round($i['grams']/1000*2.20462,2); } //pull from fedex $isresidential = true; if ($DATA['destination']['company_name'] !== null && $DATA['destination']['company_name'] != '') $isresidential = false; $_FEDEX_ACCOUNT = '1111111'; $_FEDEX_METER = '22222222'; $_FEDEX_FREIGHT_ACCOUNT = '33333333'; $_FEDEX_KEY = 'YOUR_KEY_HERE'; $_FEDEX_PASSWORD = 'YOUR_PASSWORD_HERE'; $packages = array( array('weight'=>$w, 'length'=>18, 'width'=>12, 'height'=>4), ); if ($DATA['destination']['city'] == '') $DATA['destination']['city'] = 'city'; if ($DATA['destination']['address1'] == '') $DATA['destination']['address1'] = 'address'; $fedex = new FedExAPI($_FEDEX_ACCOUNT,$_FEDEX_METER,$_FEDEX_KEY,$_FEDEX_PASSWORD); $fedex->setOrigin($DATA['origin']['province'],$DATA['origin']['postal_code'],$DATA['origin']['country'],$DATA['origin']['city'],$DATA['origin']['address1']); $fedex->setDestination($DATA['destination']['province'],$DATA['destination']['postal_code'],$DATA['destination']['country'],$isresidential,$DATA['destination']['city'],$DATA['destination']['address1']); $r = $fedex->getFreightRate($packages,$_FEDEX_FREIGHT_ACCOUNT,'CLASS_070'); if ($r) { foreach ($r as $_r) { $eta = ''; if ($_r['businessdays'] != '') $eta = "Estimated {$_r['businessdays']} days"; $_RATES[] = array( "service_name" => $_r['name'], "service_code" => $_r['name'], "total_price" => $_r['amount']*100, //in cents "description" => $eta, "currency" => "USD", ); } } return $_RATES; /* do not edit below this line */ }