/* 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; $t = 0; foreach ($DATA['items'] as $i) { $w += $i['quantity']*$i['grams']/1000; $t += $i['quantity']*$i['price']/100; } $w = $w*2.20462; //in pounds $_SITEID = 'YOUR SITE ID'; $_PASSWORD = 'YOUR PASSWORD'; $_PAYMENTACCOUNTNUMBER = 'YOUR PAYMENT ACCOUNT NUMBER'; $packages = array( array('weight'=>$w, 'length'=>5, 'width'=>5, 'height'=>5), ); $foundDHL = false; $dhl = new DHLAPI($_SITEID,$_PASSWORD,$_PAYMENTACCOUNTNUMBER); $dhl->setOrigin($DATA['origin']['postal_code'],$DATA['origin']['country']); $dhl->setDestination($DATA['destination']['city'],$DATA['destination']['province'],$DATA['destination']['postal_code'],$DATA['destination']['country'],false); $r = $dhl->getRate($packages,'IMPERIAL',false); if ($r) { foreach ($r as $_r) { if ($_r['amount'] == 0) continue; $_r['name'] = trim(preg_replace('/nondoc/i','',$_r['name'])); //remove 'nondoc' from the names (optinal) if (preg_match('/medical|doc/i',$_r['name'])) continue; //filter out medical and doc rates (optional) $_RATES[] = array( "service_name" => $_r['name'], "service_code" => $_r['name'], "total_price" => $_r['amount']*100, "currency" => "USD", ); $foundDHL = true; } } if (!$foundDHL) { //try again but send the order value for import taxes $r = $dhl->getRate($packages,'IMPERIAL',$t,'USD'); if ($r) { foreach ($r as $_r) { if ($_r['amount'] == 0) continue; $_r['name'] = trim(preg_replace('/nondoc/i','',$_r['name'])); //remove 'nondoc' from the names (optinal) if (preg_match('/medical|doc/i',$_r['name'])) continue; //filter out medical and doc rates (optional) $_RATES[] = array( "service_name" => $_r['name'], "service_code" => $_r['name'], "total_price" => $_r['amount']*100, "currency" => "USD", ); $foundDHL = true; } } } return $_RATES; /* do not edit below this line */ }