array('length'=>13.5, 'width'=>10.75, 'height'=>4, 'emptyBoxWeight'=>0, 'maxWeight'=>999, 'padding'=>0), 'box 2' => array('length'=>14, 'width'=>11.5, 'height'=>4.5, 'emptyBoxWeight'=>0, 'maxWeight'=>999, 'padding'=>0), 'box 3' => array('length'=>18, 'width'=>14, 'height'=>4.75, 'emptyBoxWeight'=>0, 'maxWeight'=>999, 'padding'=>0), ); $enriched = array(); foreach ($DATA['items'] as $i) { $i['weight'] = round($i['grams']/1000*2.20462,2); //Parse metafields if (isset($i['metafields']['length']) && isset($i['metafields']['width']) && isset($i['metafields']['height'])) { $i['length'] = floatval($i['metafields']['length']); $i['width'] = floatval($i['metafields']['width']); $i['height'] = floatval($i['metafields']['height']); } else { //dimensions unknown - use default $i['length'] = $defaultL; $i['width'] = $defaultW; $i['height'] = $defaultH; } //Parse multi-box metafields (up to 10 boxes) $multiBoxes = array(); for ($z=1; $z<=10; $z++) { if (isset($i['metafields']['box'.$z]) && preg_match('/^([0-9.]+?)x([0-9.]+?)x([0-9.]+)@([0-9.]+?)$/i',$i['metafields']['box'.$z],$m)) { $multiBoxes[] = array( 'length' => floatval($m[1]), 'width' => floatval($m[2]), 'height' => floatval($m[3]), 'weight' => floatval($m[4]), ); } } if (isset($i['metafields']['useownbox']) && in_array(strtolower(trim($i['metafields']['useownbox'])),array('1','yes','true'))) { $i['useOwnBox'] = true; } //Error checking - this is optional however if no dimensions are found you will need error handling to correctly handle this scenario if (!isset($i['length']) && count($multiBoxes) == 0) return array(); //dimensions unknown - stop processing and return no rates if (count($multiBoxes) > 0) { foreach ($multiBoxes as $mb) { $i['length'] = $mb['length']; $i['width'] = $mb['width']; $i['height'] = $mb['height']; $i['weight'] = $mb['weight']; $enriched[] = $i; } } else { $enriched[] = $i; } } //generate a list of packages and weights $packages = estimatePackagesByVol($enriched,$boxes,$dunnage); $isresidential = true; if ($DATA['destination']['company_name'] !== null && $DATA['destination']['company_name'] != '') $isresidential = false; //pull carrier rates $usps = new USPSAPI($_USPS_USERNAME,$_USPS_PASSWORD,$_USPS_CONTRACTTYPE); $usps->setOrigin($DATA['origin']['postal_code']); $usps->setDestination($DATA['destination']['province'],substr($DATA['destination']['postal_code'],0,5),$DATA['destination']['country'],$isresidential); $r = $usps->getRate($packages); if ($r) { foreach ($r as $_r) { if (preg_match('/media|library|hold|legal|gift|metered|postcard|letter|select|envelope|flat|regional|holiday|parcel/i',$_r['name'])) continue; $_r['name'] = preg_replace('/ \d-Day/i','',$_r['name']); $eta = ''; if ($_r['businessdays'] != '') $eta = "Estimated {$_r['businessdays']} days"; $_RATES[] = array( "service_name" => 'USPS '.$_r['name'], "service_code" => 'USPS '.$_r['name'], "total_price" => ($_r['amount'])*100, //in cents "description" => $eta, "currency" => "USD", ); } } $ups = new UPSAPI($_UPS_USERID,$_UPS_PASSWD,$_UPS_ACCESS,$_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); $r = $ups->getRate($packages); 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", ); } } $fedex = new FedExAPI($_FEDEX_ACCOUNT,$_FEDEX_METER,$_FEDEX_KEY,$_FEDEX_PASSWORD); $fedex->setOrigin($DATA['origin']['province'],$DATA['origin']['postal_code'],$DATA['origin']['country']); $fedex->setDestination($DATA['destination']['province'],$DATA['destination']['postal_code'],$DATA['destination']['country'],$isresidential); $r = $fedex->getRate($packages); 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 */ }