1<?php 2// (c) Copyright by authors of the Tiki Wiki CMS Groupware Project 3// 4// All Rights Reserved. See copyright.txt for details and a complete list of authors. 5// Licensed under the GNU LESSER GENERAL PUBLIC LICENSE. See license.txt for details. 6// $Id$ 7 8/* Simple shipping calculator example based on number of items in cart 9 * 10 * Needs to declare getName for the pref list, and getRates 11 * 12 * Declare the options you require in the constructor 13 * and the logic to caluculate the price etc (in this case) in getRate 14 */ 15 16require_once 'lib/shipping/shippinglib.php'; 17 18class CustomShippingProvider_Example extends CustomShippingProvider 19{ 20 private $services; 21 22 function __construct() // needs to be without params 23 { 24 $this->services = [ 25 'standard' => [ 26 'name' => tra('Standard Shipping'), 27 'description' => tra('3 to 5 working days'), 28 'zones' => [ 29 'zone 1' => [ // $5 per package, more than 6 packages go free 30 'cost_per_item' => 9, 31 'max_total' => 45, 32 ], 33 'zone 2' => [ // $20 per package, max 3 34 'cost_per_item' => 20, 35 'max_total' => 60, 36 ], 37 ], 38 ], 39 'express' => [ 40 'name' => tra('Express Shipping'), 41 'description' => tra('Next day delivery'), 42 'zones' => [ 43 'zone 1' => [ // $20 per package, max 3 44 'cost_per_item' => 20, 45 'max_total' => 60, 46 ], 47 'zone 2' => [ // $30 per package, max 3 48 'cost_per_item' => 30, 49 'max_total' => 90, 50 ], 51 ], 52 ], 53 ]; 54 } 55 56 function getName() 57 { 58 return tra('Custom Shipping Example'); 59 } 60 61 function getCurrency() 62 { 63 return 'USD'; 64 } 65 66 function getRates(array $from, array $to, array $packages) 67 { 68 if (! empty($to) && ! empty($packages)) { 69 $rates = []; 70 71 foreach ($this->services as $service => $info) { 72 $rates[] = $this->getRate($info, $from, $to, $packages); 73 } 74 75 return $rates; 76 } else { 77 return []; 78 } 79 } 80 81 private function getRate($service, array $from, array $to, array $packages) 82 { 83 $ret = [ 84 'provider' => $this->getName(), 85 'currency' => $this->getCurrency(), 86 'service' => $service['name'], 87 'readable' => $service['description'], 88 ]; 89 90 $itemCount = 0; 91 foreach ($packages as $item) { 92 if (! empty($item['count'])) { 93 $itemCount += (int) $item['count']; 94 } else { 95 $itemCount++; 96 } 97 } 98 99 if (in_array(strtoupper($to['country']), [ 'AR', 'BO', 'BR', 'CL', 'CO', 'EC', 'FK', 'GF', 'GY', 'PY', 'PE', 'GS', 'SR', 'UY', 'VE' ])) { 100 $zone = $service['zones']['zone 2']; // zone 2 is South America 101 } else { 102 $zone = $service['zones']['zone 1']; 103 } 104 105 $ret['cost'] = min($itemCount * $zone['cost_per_item'], $zone['max_total']); 106 107 return $ret; 108 } 109} 110