/* 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(); $w = 0; foreach ($DATA['items'] as $i) { $w += $i['quantity']*$i['grams']/1000; } //use cm and KG $packages = array( array('weight'=>$w, 'length'=>16, 'width'=>11, 'height'=>2), ); //correios $CO = new CorreiosAPI('YOUR ADMIN CODE (OPTIONAL)','YOUR PASSWORD (OPTIONAL)'); $CO->setOrigin(null,null,$DATA['origin']['postal_code'],'BR'); $CO->setDestination(null,null,$DATA['destination']['postal_code'],'BR'); $r = $CO->getRate($packages); if ($r) { foreach ($r as $_r) { $_RATES[] = array( "service_name" => $_r['name'].' ('.$_r['businessdays'].' Business Days)', "service_code" => $_r['code'], "total_price" => $_r['amount']*100, //in cents "currency" => "BRL", ); } } return $_RATES; /* do not edit below this line */ }