/* 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(); //free shipping threshold $freeShippingTreshold = 100; //rate for each sku (all prices in cents) $config = array( 'SKU0001' => array('rate'=>1000, 'sub'=>500), 'SKU0002' => array('rate'=>999, 'sub'=>500), 'SKU0003' => array('rate'=>1099, 'sub'=>500), ); $defaultrate = 500; $rate = 0; $cartTotal = 0; //go through cart and group by type foreach ($DATA['items'] as $item) { $cartTotal += $item['quantity']*$item['price']/100; if (isset($config[$item['sku']])) { if ($item['quantity'] > 1) { $rate += $config[$item['sku']]['rate'] + $config[$item['sku']]['sub']*($item['quantity']-1); } else { $rate += $config[$item['sku']]['rate']; } } else { //default rate $rate += $defaultrate*$item['quantity']; } } //round up nearest cent $rate = ceil($rate); //apply free shipping if ($cartTotal > $freeShippingTreshold) $rate = 0; $_RATES[] = array( "service_name" => "Standard Shipping", "service_code" => "STANDARD", "total_price" => $rate, //in cents "currency" => "USD", ); return $_RATES; /* do not edit below this line */ }