/classes/jigoshop_shipping.class.php

https://github.com/annzaan/jigoshop · PHP · 257 lines · 159 code · 55 blank · 43 comment · 41 complexity · 0f3f6ec2cb67b665b6e4c3f990de504e MD5 · raw file

  1. <?php
  2. /**
  3. * Shipping class
  4. *
  5. * DISCLAIMER
  6. *
  7. * Do not edit or add directly to this file if you wish to upgrade Jigoshop to newer
  8. * versions in the future. If you wish to customise Jigoshop core for your needs,
  9. * please use our GitHub repository to publish essential changes for consideration.
  10. *
  11. * @package Jigoshop
  12. * @category Checkout
  13. * @author Jigoshop
  14. * @copyright Copyright © 2011-2013 Jigoshop.
  15. * @license http://jigoshop.com/license/commercial-edition
  16. */
  17. class jigoshop_shipping extends Jigoshop_Singleton {
  18. protected static $enabled = false;
  19. protected static $shipping_methods = array();
  20. protected static $chosen_method = null;
  21. protected static $shipping_total = 0;
  22. protected static $shipping_tax = 0;
  23. protected static $shipping_label = null;
  24. private static $shipping_error_message = null;
  25. /** Constructor */
  26. protected function __construct() {
  27. self::shipping_inits();
  28. if (self::get_options()->get_option('jigoshop_calc_shipping') != 'no') :
  29. self::$enabled = true;
  30. endif;
  31. }
  32. /**
  33. * Initialize all shipping modules.
  34. */
  35. public static function shipping_inits() {
  36. do_action('jigoshop_shipping_init'); /* loaded plugins for shipping inits */
  37. $load_methods = apply_filters('jigoshop_shipping_methods', array());
  38. foreach ($load_methods as $method) :
  39. self::$shipping_methods[] = new $method();
  40. endforeach;
  41. }
  42. public static function is_enabled() {
  43. return self::$enabled;
  44. }
  45. public static function get_total() {
  46. return self::$shipping_total;
  47. }
  48. public static function get_tax() {
  49. return self::$shipping_tax;
  50. }
  51. public static function get_label() {
  52. return self::$shipping_label;
  53. }
  54. public static function get_all_methods() {
  55. return self::$shipping_methods;
  56. }
  57. public static function show_shipping_calculator() {
  58. return (self::is_enabled() && self::get_options()->get_option('jigoshop_enable_shipping_calc')=='yes' && jigoshop_cart::needs_shipping());
  59. }
  60. public static function get_available_shipping_methods() {
  61. $_available_methods = array();
  62. if (self::$enabled == 'yes') :
  63. foreach (self::get_all_methods() as $method) :
  64. if ( jigoshop_cart::has_free_shipping_coupon() && $method->id == 'free_shipping' )
  65. $_available_methods[$method->id] = $method;
  66. if ($method->is_available()) :
  67. $_available_methods[$method->id] = $method;
  68. endif;
  69. endforeach;
  70. endif;
  71. //throw error if there are no shipping methods
  72. if ( empty( $_available_methods )) {
  73. self::$shipping_error_message = __('Please enter your shipping destination and postal code to view shipping options and rates.', 'jigoshop');
  74. if ( self::get_options()->get_option('jigoshop_enable_shipping_calc') == 'no' && is_cart() ) {
  75. self::$shipping_error_message .= __(' If the Shipping Calculator is not available here, you will need to advance to the Checkout to do this.','jigoshop');
  76. }
  77. self::$shipping_error_message .= __(' There may be no methods available for your destination and you should contact us for assistance.', 'jigoshop');
  78. }
  79. return apply_filters('jigoshop_available_shipping_methods',$_available_methods);
  80. }
  81. public static function get_shipping_error_message() {
  82. return self::$shipping_error_message;
  83. }
  84. public static function reset_shipping_methods() {
  85. foreach (self::$shipping_methods as $method) :
  86. $method->reset_method();
  87. endforeach;
  88. }
  89. /**
  90. * finds the cheapest shipping method
  91. * @param type $available_methods all methods that are available
  92. * @param type $tax jigoshop_tax class instance
  93. * @return type the cheapest shipping method being used
  94. */
  95. private static function get_cheapest_method($available_methods, $tax) {
  96. $_cheapest_fee = '';
  97. $_cheapest_method = '';
  98. $_selected_service = '';
  99. foreach ($available_methods as $method) :
  100. $method->set_tax($tax);
  101. $method->calculate_shipping();
  102. if ($method->id != 'local_pickup') : // don't let local_pickup be chosen automatically
  103. if (!$method->has_error()) :
  104. $fee = $method->get_cheapest_price(); // obtain cheapest price
  105. if ($fee >= 0 && $fee < $_cheapest_fee || !is_numeric($_cheapest_fee)) :
  106. $_cheapest_fee = $fee;
  107. $_cheapest_method = $method->id;
  108. $_selected_service = $method->get_cheapest_service();
  109. endif;
  110. else :
  111. $method_error_message = $method->get_error_message();
  112. if ($method_error_message) :
  113. self::$shipping_error_message .= $method_error_message . PHP_EOL;
  114. endif;
  115. endif;
  116. endif;
  117. endforeach;
  118. if (!empty($_selected_service)) :
  119. $available_methods[$_cheapest_method]->set_selected_service_index($_selected_service);
  120. endif;
  121. return $_cheapest_method;
  122. }
  123. /**
  124. *
  125. * @return mixed the id of the chosen shipping method or false if none are chosen
  126. */
  127. public static function get_chosen_method() {
  128. $_available_methods = self::get_available_shipping_methods();
  129. foreach ($_available_methods as $method) :
  130. if ($method->is_chosen()) :
  131. return $method->id;
  132. endif;
  133. endforeach;
  134. return false;
  135. }
  136. public static function get_chosen_method_title() {
  137. $_available_methods = self::get_available_shipping_methods();
  138. $chosen_method = self::get_chosen_method();
  139. return $_available_methods[$chosen_method]->title;
  140. }
  141. /**
  142. * Calculate the shipping price
  143. *
  144. * @param type $tax jigoshop_tax class instance
  145. */
  146. public static function calculate_shipping($tax) {
  147. if (self::$enabled == 'yes') :
  148. self::reset_shipping_methods();
  149. self::reset_shipping(); // do not reset session (chosen_shipping_method_id)
  150. $calc_cheapest = false;
  151. if (!empty( jigoshop_session::instance()->chosen_shipping_method_id)) :
  152. $chosen_method = jigoshop_session::instance()->chosen_shipping_method_id;
  153. else :
  154. $chosen_method = '';
  155. $calc_cheapest = true;
  156. endif;
  157. $_available_methods = self::get_available_shipping_methods();
  158. if (sizeof($_available_methods) > 0) :
  159. // have to check numeric selected_rate_id because it can be 0, and empty returns true for 0. That is unwanted behaviour
  160. if (is_numeric( jigoshop_session::instance()->selected_rate_id ) && !empty($chosen_method)) :
  161. //make sure all methods are re-calculated since prices have been reset. Otherwise the other shipping
  162. //method prices will show free
  163. foreach ($_available_methods as $method) :
  164. $method->set_tax($tax);
  165. $method->calculate_shipping();
  166. endforeach;
  167. // select chosen method.
  168. if (isset($_available_methods[$chosen_method]) && $_available_methods[$chosen_method] && !$_available_methods[$chosen_method]->has_error()) :
  169. $chosen_method = $_available_methods[$chosen_method]->id;
  170. // chosen shipping method had issues, need to auto calculate cheapest method now
  171. else :
  172. $chosen_method = self::get_cheapest_method($_available_methods, $tax);
  173. endif;
  174. else :
  175. // current jigoshop functionality
  176. $_cheapest_method = self::get_cheapest_method($_available_methods, $tax);
  177. if (!$_cheapest_method) :
  178. // there was an error, and if chosen method was in the session we want to reset that
  179. $chosen_method = $_cheapest_method;
  180. else :
  181. if ($calc_cheapest || !isset($_available_methods[$chosen_method])) :
  182. $chosen_method = $_cheapest_method;
  183. endif;
  184. endif;
  185. endif;
  186. if ($chosen_method) :
  187. //sets session in the method choose()
  188. $_available_methods[$chosen_method]->choose();
  189. self::$shipping_total = $_available_methods[$chosen_method]->get_selected_price( jigoshop_session::instance()->selected_rate_id );
  190. self::$shipping_tax = $_available_methods[$chosen_method]->get_selected_tax( jigoshop_session::instance()->selected_rate_id );
  191. self::$shipping_label = $_available_methods[$chosen_method]->get_selected_service(jigoshop_session::instance()->selected_rate_id );
  192. endif;
  193. endif; //sizeof available methods
  194. endif; //self enabled == 'yes'
  195. }
  196. public static function reset_shipping() {
  197. self::$shipping_total = 0;
  198. self::$shipping_tax = 0;
  199. self::$shipping_label = null;
  200. self::$shipping_error_message = null;
  201. }
  202. }