/* 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(); $DATA = enrichProductDetails($DATA); //free shipping threshold (NB: Bespoke does not have visibility of discount coupons, all thresholding will be done on the pre-discounted cart total) $freeShippingTreshold = 100; //rate for each product type (all prices in cents) $config = array( 'ProductType1' => array('rate'=>1000, 'sub'=>400), 'ProductType2' => array('rate'=>999, 'sub'=>500), 'ProductType3' => array('rate'=>1099, 'sub'=>500), ); $defaultrate = 500; $cartTotal = 0; $groupedByType = array(); //go through cart and group by type foreach ($DATA['items'] as $item) { $productType = $item['product_type']; $quantity = $item['quantity']; $item['quantity'] = 1; //reset qty as we're adding them into the grouped array individually for ($i=0; $i<$quantity; $i++) { //insert each unit in as a separate entry $groupedByType[$productType][] = $item; } } $rate = 0; foreach ($groupedByType as $productType => $items) { if (isset($config[$productType])) { $first = $config[$productType]['rate']; $sub = $config[$productType]['sub']; } else { //default $first = $defaultrate; $sub = $defaultrate; } foreach ($items as $item) { $cartTotal += $item['price']/100; } $qty = count($items); if ($qty > 1) { $rate += $first + $sub*($qty-1); } else { $rate += $first; } } //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 */ }