/* 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(); //stop on first match, so if you want to match on postcode, make sure they appear first on the list for each vendor $config = array( //this will apply to postcodes 1000-2249 inclusive in australia array( 'country' => 'AU', //comma separated, leave blank for any 'state' => '', //comma separated, leave blank for any 'postcode' => array_merge(range(1000,2249)), //comma separated, leave blank for any 'ratetype' => 'weight', //weight or qty 'rates' => array( array('start'=>0, 'end'=>1000, 'rate'=>4.95), array('start'=>1000, 'end'=>9999999, 'rate'=>40), ), ), //this will apply to postcodes 3000-3050 and 3060-3500 inclusive in australia array( 'country' => 'AU', //comma separated, leave blank for any 'state' => '', //comma separated, leave blank for any 'postcode' => array_merge(range(3000,3050),range(3060,3500)), //comma separated, leave blank for any 'ratetype' => 'weight', //weight or qty 'rates' => array( array('start'=>0, 'end'=>1000, 'rate'=>4.95), array('start'=>1000, 'end'=>9999999, 'rate'=>40), ), ), ); //tally the weight, qty and total for each vendor too $qty = 0; $weighttotal = 0; $pricetotal = 0; foreach ($DATA['items'] as $item) { $qty += $item['quantity']; $weighttotal += $item['quantity']*$item['grams']; //in grams $pricetotal += $item['quantity']*$item['price']/100; //in dollars } $country = strtoupper($DATA['destination']['country']); $postcode = strtoupper($DATA['destination']['postal_code']); $state = strtoupper($DATA['destination']['province']); //go through each item and apply shipping schedule, tally rate $totalRate = 0; //go through each rate and work out which one applies //stop on first match $match = null; foreach ($config as $vr) { //match country $countryMatched = false; if ($vr['country'] != '') { if (strpos($vr['country'],',')!==false) { $_country = explode(',',$vr['country']); if (in_array($country,$_country)) { //matched country within a list $countryMatched = true; } } else if ($vr['country'] == $country) { //matched single entry country $countryMatched = true; } } else { $countryMatched = true; } //match state $stateMatched = false; if ($vr['state'] != '') { if (strpos($vr['state'],',')!==false) { $_state = explode(',',$vr['state']); if (in_array($state,$_state)) { //matched state within a list $stateMatched = true; } } else if ($vr['state'] == $state) { //matched single entry state $stateMatched = true; } } else { $stateMatched = true; } //match postcode $postcodeMatched = false; if ($vr['postcode'] != '') { if (strpos($vr['postcode'],',')!==false) { $_postcode = explode(',',$vr['postcode']); if (in_array($postcode,$_postcode)) { //matched postcode within a list $postcodeMatched = true; } } else if ($vr['postcode'] == $postcode) { //matched single entry postcode $postcodeMatched = true; } } else { $postcodeMatched = true; } if ($countryMatched && $stateMatched && $postcodeMatched) { //match found $match = $vr; break; } } if ($match !== null) { $matchedRate = null; if ($match['ratetype'] == 'weight') { //by weight foreach ($match['rates'] as $r) { if ($r['start'] <= $weighttotal && $weighttotal <= $r['end']) { $matchedRate = $r; break; } } } else { //by qty foreach ($match['rates'] as $r) { if ($r['start'] <= $qty && $qty <= $r['end']) { $matchedRate = $r; break; } } } if ($matchedRate !== null) { //add on the rate for this vendor's products $totalRate += $matchedRate['rate']; } else { //no rate found for this qty/weight range return array(); //uncomment this if you want the order to be blocked, otherwise free shipping for this vendor } } else { //no rate found for this vendor //return array(); //uncomment this if you want the order to be blocked, otherwise free shipping for this vendor } if ($totalRate !== null) { $_RATES[] = array( "service_name" => 'Standard Shipping', "service_code" => 'STANDARD', "total_price" => $totalRate*100, //in cents "currency" => "BRL", ); } return $_RATES; /* do not edit below this line */ }