/* 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(); $_CP_APIUSER = 'API USER'; $_CP_APIPASS = 'API PASSWORD'; $_CP_CUSTOMERNUMBER = 'CUSTOMER NUMBER'; $_CP_CONTRACTNUMBER = 'CONTRACT NUMBER, LEAVE BLANK FOR NONE'; $isresidential = true; if ($DATA['destination']['company_name'] !== null && $DATA['destination']['company_name'] != '') $isresidential = false; //clean postcode $DATA['origin']['postal_code'] = strtoupper(preg_replace('/[^A-Z0-9]/i','',$DATA['origin']['postal_code'])); $DATA['destination']['postal_code'] = strtoupper(preg_replace('/[^A-Z0-9]/i','',$DATA['destination']['postal_code'])); $CP = new CanadaPostAPI($_CP_APIUSER,$_CP_APIPASS,$_CP_CUSTOMERNUMBER,$_CP_CONTRACTNUMBER); $w = 0; foreach ($DATA['items'] as $i) { $w += $i['quantity']*$i['grams']/1000; } $packages = array( array('length'=>5, 'width'=>5, 'height'=>5, 'weight'=>ceil($w)), ); //canada post $CP->setOrigin('','',$DATA['origin']['postal_code']); $CP->setDestination($DATA['destination']['city'],$DATA['destination']['province'],$DATA['destination']['postal_code'],$DATA['destination']['country'],$isresidential); $r = $CP->getRate($packages); if ($r) { foreach ($r as $_r) { $_RATES[] = array( "service_name" => 'Canada Post '.$_r['name'].' ('.$_r['businessdays'].' Business Days)', "service_code" => $_r['code'], "total_price" => $_r['amount']*100, //in cents "currency" => "CAD", ); } } return $_RATES; /* do not edit below this line */ }