/* 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(); $_FIRSTMILE_USERNAME = 'YOUR_FIRST_MILE_API_USERNAME'; $_FIRSTMILE_PASSWORD = 'YOUR_FIRST_MILE_API_PASSWORD'; $isResidential = true; if ($DATA['destination']['company_name'] !== null && $DATA['destination']['company_name'] != '') $isResidential = false; $w = 0; foreach ($DATA['items'] as $i) { $w += $i['quantity']*round($i['grams']/1000*2.20462,2); } //default box size $packages = array( array('length'=>12, 'width'=>8.75, 'height'=>6, 'weight'=>$w), ); $FM = new FirstMileAPI($_FIRSTMILE_USERNAME,$_FIRSTMILE_PASSWORD); $FM->setOrigin(trim($DATA['origin']['address1'].' '.$DATA['origin']['address2']),$DATA['origin']['city'],$DATA['origin']['province'],$DATA['origin']['postal_code'],$DATA['origin']['country']); $FM->setDestination(trim($DATA['destination']['address1'].' '.$DATA['destination']['address2']),$DATA['destination']['city'],$DATA['destination']['province'],$DATA['destination']['postal_code'],$DATA['destination']['country'],$isResidential); $r = $FM->getRate($packages,'IMPERIAL',$e,'ALL'); if ($r) { foreach ($r as $_r) { if ($_r['amount'] == 0) continue; $rateName = $_r['name']; $eta = ''; $_RATES[] = array( "service_name" => $rateName, "service_code" => $rateName, "description" => $eta, "total_price" => ceil($_r['amount']*100), "currency" => "USD", ); } } return $_RATES; /* do not edit below this line */ }