Advertisement
Boelle

Untitled

Aug 25th, 2015
304
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.81 KB | None | 0 0
  1. <?php
  2.  
  3. class distance {
  4. var $code, $title, $description, $icon, $enabled;
  5.  
  6. // class constructor
  7. function distance() {
  8. global $order;
  9.  
  10. $this->code = 'distance';
  11. $this->title = MODULE_SHIPPING_DISTANCE_TEXT_TITLE;
  12. $this->description = MODULE_SHIPPING_DISTANCE_TEXT_DESCRIPTION;
  13. $this->sort_order = MODULE_SHIPPING_DISTANCE_SORT_ORDER;
  14. $this->icon = '';
  15. $this->tax_class = MODULE_SHIPPING_DISTANCE_TAX_CLASS;
  16. $this->enabled = ((MODULE_SHIPPING_DISTANCE_STATUS == 'True') ? true : false);
  17.  
  18. if ( ($this->enabled == true) && ((int)MODULE_SHIPPING_DISTANCE_ZONE > 0) ) {
  19. $check_flag = false;
  20. $check_query = tep_db_query("select zone_id from " . TABLE_ZONES_TO_GEO_ZONES . " where geo_zone_id = '" . MODULE_SHIPPING_DISTANCE_ZONE . "' and zone_country_id = '" . $order->delivery['country']['id'] . "' order by zone_id");
  21. while ($check = tep_db_fetch_array($check_query)) {
  22. if ($check['zone_id'] < 1) {
  23. $check_flag = true;
  24. break;
  25. } elseif ($check['zone_id'] == $order->delivery['zone_id']) {
  26. $check_flag = true;
  27. break;
  28. }
  29. }
  30.  
  31. if ($check_flag == false) {
  32. $this->enabled = false;
  33. }
  34. }
  35. }
  36.  
  37. // class methods
  38. function quote($method = '') {
  39. global $order;
  40.  
  41. // get destination
  42. $geo_address = NULL;
  43. $geo_address .= rawurlencode($order->delivery['street_address']) . '+';
  44. $geo_address .= rawurlencode($order->delivery['city']) . '+';
  45. $geo_address .= rawurlencode($order->delivery['postcode']) . '+';
  46. $geo_address .= rawurlencode($order->delivery['state']) . '+';
  47. $geo_address .= rawurlencode($order->delivery['country']['title']);
  48.  
  49. $url = 'http://maps.googleapis.com/maps/api/geocode/xml?address=' . $geo_address . '&sensor=false';
  50. $ch = curl_init($url);
  51. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  52. $data = curl_exec($ch); curl_close($ch);
  53. $geocode = simplexml_load_string($data, null, LIBXML_NOCDATA);
  54.  
  55. $destination = $geocode->result->geometry->location->lat . ',' . $geocode->result->geometry->location->lng;
  56.  
  57. // work out DISTANCE UNITS
  58. $url = 'http://maps.googleapis.com/maps/api/distancematrix/xml?origins=' . MODULE_SHIPPING_DISTANCE_ORIGIN . '&destinations=' . $destination . '&units=' . MODULE_SHIPPING_DISTANCE_UNITS;
  59. $ch = curl_init($url);
  60. curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  61. $data = curl_exec($ch); curl_close($ch);
  62. $geocode = simplexml_load_string($data, null, LIBXML_NOCDATA);
  63.  
  64. $actual_distance = $geocode->row->element->distance->value;
  65. $miles = $actual_distance/1609.34;
  66. $km = $actual_distance/1000;
  67.  
  68. // work out cost
  69. $distance = (MODULE_SHIPPING_DISTANCE_UNITS == 'metric') ? $km : $miles;
  70.  
  71. $distance_cost = preg_split("/[:,]/" , MODULE_SHIPPING_DISTANCE_COST);
  72. $size = sizeof($distance_cost);
  73. for ($i=0, $n=$size; $i<$n; $i+=2) {
  74. if ($distance <= $distance_cost[$i]) {
  75. $shipping = $distance_cost[$i+1];
  76. break;
  77. }
  78. }
  79. //$cost = round(($distance * MODULE_SHIPPING_DISTANCE_COST), 2);
  80.  
  81. $this->quotes = array('id' => $this->code,
  82. 'module' => MODULE_SHIPPING_DISTANCE_TEXT_TITLE,
  83. 'methods' => array(array('id' => $this->code,
  84. 'title' => MODULE_SHIPPING_DISTANCE_TEXT_WAY,
  85. 'cost' => $shipping + MODULE_SHIPPING_DISTANCE_HANDLING)));
  86.  
  87. if ($this->tax_class > 0) {
  88. $this->quotes['tax'] = tep_get_tax_rate($this->tax_class, $order->delivery['country']['id'], $order->delivery['zone_id']);
  89. }
  90.  
  91. if (tep_not_null($this->icon)) $this->quotes['icon'] = tep_image($this->icon, $this->title);
  92.  
  93. return $this->quotes;
  94. }
  95.  
  96. function check() {
  97. if (!isset($this->_check)) {
  98. $check_query = tep_db_query("select configuration_value from configuration where configuration_key = 'MODULE_SHIPPING_DISTANCE_STATUS'");
  99. $this->_check = tep_db_num_rows($check_query);
  100. }
  101. return $this->_check;
  102. }
  103.  
  104. function install() {
  105. tep_db_query("insert into configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Enable Distance Based Shipping', 'MODULE_SHIPPING_DISTANCE_STATUS', 'True', 'Do you want to offer distance based shipping?', '6', '0', 'tep_cfg_select_option(array(\'True\', \'False\'), ', now())");
  106. tep_db_query("insert into configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, set_function, date_added) values ('Distance Units', 'MODULE_SHIPPING_DISTANCE_UNITS', 'imperial', 'Choose Imperial (Mile) or Metric (KM) for working out cost of shipping.', '6', '0', 'tep_cfg_select_option(array(\'imperial\', \'metric\'), ', now())");
  107. tep_db_query("insert into configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Shipping Table', 'MODULE_SHIPPING_DISTANCE_COST', '10:1.99,20:2.99,100:0', 'The shipping cost is based on the Calculated Imperial/Metric Distance. Example: 10:1.99,20:2.99,etc.. Up to 10 charge 1.99, from there to 20 charge 2.99, etc', '6', '0', now())");
  108. tep_db_query("insert into configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Handling Fee', 'MODULE_SHIPPING_DISTANCE_HANDLING', '0', 'Handling fee for this shipping method.', '6', '0', now())");
  109. tep_db_query("insert into configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Origin Location', 'MODULE_SHIPPING_DISTANCE_ORIGIN', '51.503327,-0.119377', 'Your EXACT location using Latitude and Longitude.', '6', '0', now())");
  110. tep_db_query("insert into configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Tax Class', 'MODULE_SHIPPING_DISTANCE_TAX_CLASS', '0', 'Use the following tax class on the shipping fee.', '6', '0', 'tep_get_tax_class_title', 'tep_cfg_pull_down_tax_classes(', now())");
  111. tep_db_query("insert into configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, use_function, set_function, date_added) values ('Shipping Zone', 'MODULE_SHIPPING_DISTANCE_ZONE', '0', 'If a zone is selected, only enable this shipping method for that zone.', '6', '0', 'tep_get_zone_class_title', 'tep_cfg_pull_down_zone_classes(', now())");
  112. tep_db_query("insert into configuration (configuration_title, configuration_key, configuration_value, configuration_description, configuration_group_id, sort_order, date_added) values ('Sort Order', 'MODULE_SHIPPING_DISTANCE_SORT_ORDER', '0', 'Sort order of display.', '6', '0', now())");
  113. }
  114.  
  115. function remove() {
  116. tep_db_query("delete from configuration where configuration_key in ('" . implode("', '", $this->keys()) . "')");
  117. }
  118.  
  119. function keys() {
  120. return array('MODULE_SHIPPING_DISTANCE_STATUS', 'MODULE_SHIPPING_DISTANCE_UNITS', 'MODULE_SHIPPING_DISTANCE_COST', 'MODULE_SHIPPING_DISTANCE_HANDLING', 'MODULE_SHIPPING_DISTANCE_ORIGIN', 'MODULE_SHIPPING_DISTANCE_TAX_CLASS', 'MODULE_SHIPPING_DISTANCE_ZONE', 'MODULE_SHIPPING_DISTANCE_SORT_ORDER');
  121. }
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement