/* 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(); //put your origin address for various groups here - vendor name must match what's entered in Shopify (case sensitive) $origins = array( 'Vendor 1' => array( 'city' => 'Brooklyn', 'province' => 'NY', 'zip' => '11211', 'country' => 'US', 'ups_key' => 'XYZ123', 'ups_userid' => 'UPSuserID', 'ups_password' => 'UPSPassword', 'ups_account' => '123456', 'usps_username' => 'USPSusername', 'usps_password' => 'USPSPassword', 'usps_accounttype' => 'C', 'fedex_account' => '56789', 'fedex_meter' => 'Meter#', 'fedex_key' => 'XYZ456', 'fedex_password' => 'FXPassword', ), 'Vendor 2' => array( 'city' => 'Los Angeles', 'province' => 'CA', 'zip' => '90001', 'country' => 'US', 'ups_key' => 'XYZ123', 'ups_userid' => 'UPSuserID', 'ups_password' => 'UPSPassword', 'ups_account' => '123456', 'usps_username' => 'USPSusername', 'usps_password' => 'USPSPassword', 'usps_accounttype' => 'C', 'fedex_account' => '56789', 'fedex_meter' => 'Meter#', 'fedex_key' => 'XYZ456', 'fedex_password' => 'FXPassword', ), ); $wGrouped = array(); foreach ($DATA['items'] as $i) { //this is where you put your own logic to assign items to groups $group = $i['vendor']; if (!isset($wGrouped[$group])) $wGrouped[$group] = 0; $wGrouped[$group] += $i['quantity']*round($i['grams']/1000*2.20462,2); } $isresidential = true; if ($DATA['destination']['company_name'] !== null && $DATA['destination']['company_name'] != '') $isresidential = false; $ratesGrouped = array(); foreach ($wGrouped as $group => $w) { $packages[] = array('length'=>12,'width'=>8.75,'height'=>6,'weight'=>($w+0.4125)); $ups = new UPSAPI($origins[$group]['ups_userid'],$origins[$group]['ups_password'],$origins[$group]['ups_key'],$origins[$group]['ups_account']); $ups->getTransitTime = true; $ups->setOrigin($origins[$group]['city'],$origins[$group]['province'],$origins[$group]['zip'],$origins[$group]['country']); $ups->setDestination($DATA['destination']['city'],$DATA['destination']['province'],$DATA['destination']['postal_code'],$DATA['destination']['country'],$isresidential); $r = $ups->getRate($packages,'IMPERIAL'); if ($r) { foreach ($r as $_r) { $ratesGrouped[$group]['UPS'.$_r['code']] = $_r; } } $usps = new USPSAPI($origins[$group]['usps_username'],$origins[$group]['usps_password'],$origins[$group]['usps_accounttype']); $usps->setOrigin($origins[$group]['zip']); $usps->setDestination($DATA['destination']['province'],$DATA['destination']['postal_code'],$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']); $ratesGrouped[$group]['USPS'.$_r['code']] = $_r; } } $fedex = new FedExAPI($origins[$group]['fedex_account'],$origins[$group]['fedex_meter'],$origins[$group]['fedex_key'],$origins[$group]['fedex_password']); $fedex->setOrigin($origins[$group]['province'],$origins[$group]['zip'],$origins[$group]['country']); $fedex->setDestination($DATA['destination']['province'],$DATA['destination']['postal_code'],$DATA['destination']['country'],$isresidential); $r = $fedex->getRate($packages,'IMPERIAL'); if ($r) { foreach ($r as $_r) { $ratesGrouped[$group]['FEDEX'.$_r['code']] = $_r; } } } //merge like rates together $commonServices = null; foreach ($ratesGrouped as $s => $rates) { $thisRate = $rates; if ($commonServices === null) $commonServices = array_keys($rates); $commonServices = array_intersect($commonServices,array_keys($rates)); } if ($commonServices !== null) { $allRates = array(); foreach ($commonServices as $service) { $allRates[$service] = $thisRate[$service]; //copy all other values $allRates[$service]['amount'] = 0; //reset price foreach ($ratesGrouped as $s => $rates) { $allRates[$service]['amount'] += $rates[$service]['amount']; } } foreach ($allRates as $_r) { $_RATES[] = array( "service_name" => $_r['name'], "service_code" => $_r['name'], "total_price" => ($_r['amount'])*100, //in cents "currency" => "USD", ); } } return $_RATES; /* do not edit below this line */ }