is_available()) : $_available_methods[$method->id] = $method; if ($method instanceof jigoshop_calculable_shipping) : self::$has_calculable_shipping = true; endif; endif; endforeach; endif; return $_available_methods; } public static function get_shipping_error_message() { return self::$shipping_error_message; } public static function reset_shipping_methods() { foreach (self::$shipping_methods as $method) : $method->reset_method(); endforeach; } /** * finds the cheapest shipping method * @param type $available_methods all methods that are available * @param type $tax jigoshop_tax class instance * @return type the cheapest shipping method being used */ private static function get_cheapest_method($available_methods, $tax) { $_cheapest_fee = ''; $_cheapest_method = ''; self::$has_calculable_shipping = false; foreach ($available_methods as $method) : $method->set_tax($tax); $method->calculate_shipping(); if ($method instanceof jigoshop_calculable_shipping) : if (!$method->has_error()) : self::$has_calculable_shipping = true; $fee = $method->shipping_total; if ($fee >= 0 && $fee < $_cheapest_fee || !is_numeric($_cheapest_fee)) : $_cheapest_fee = $fee; $_cheapest_method = $method->id; endif; else : self::$shipping_error_message = $method->get_error_message(); endif; elseif ($method->id != 'local_pickup') : // handle normal shipping methods, except, don't let local_pickup be chosen $fee = $method->shipping_total; if ($fee >= 0 && $fee < $_cheapest_fee || !is_numeric($_cheapest_fee)) : $_cheapest_fee = $fee; $_cheapest_method = $method->id; endif; endif; endforeach; return $_cheapest_method; } /** * Calculate the shipping price * * @param type $tax jigoshop_tax class instance */ public static function calculate_shipping($tax) { if (self::$enabled == 'yes') : self::reset_shipping_methods(); self::reset_shipping(); // do not reset session (chosen_shipping_method_id) $calc_cheapest = false; if (isset( jigoshop_session::instance()->chosen_shipping_method_id)) : $chosen_method = jigoshop_session::instance()->chosen_shipping_method_id; else : $chosen_method = ''; $calc_cheapest = true; endif; $_available_methods = self::get_available_shipping_methods(); if (sizeof($_available_methods) > 0) : if (isset( jigoshop_session::instance()->selected_rate_id )) : //make sure all methods are re-calculated since prices have been reset. Otherwise the other shipping //method prices will show free foreach ($_available_methods as $method) : $method->set_tax($tax); $method->calculate_shipping(); endforeach; // select chosen method if ($_available_methods[$chosen_method] && (!($available_methods[$chosen_method] instanceof jigoshop_calculable_shipping) || ($_available_methods[$chosen_method] instanceof jigoshop_calculable_shipping && !$_available_methods[$chosen_method]->has_error()))) : $chosen_method = $_available_methods[$chosen_method]->id; // error returned from service api. Need to auto calculate cheapest method now else : $chosen_method = self::get_cheapest_method($_available_methods, $tax); endif; else : // current jigoshop functionality $_cheapest_method = self::get_cheapest_method($_available_methods, $tax); if ($calc_cheapest || !isset($_available_methods[$chosen_method])) : $chosen_method = $_cheapest_method; endif; endif; if ($chosen_method) : //sets session in the method choose() $_available_methods[$chosen_method]->choose(); // if selected_rate_id has been set, it means there are calculable shipping methods if (isset( jigoshop_session::instance()->selected_rate_id )) : if ( jigoshop_session::instance()->selected_rate_id != 'no_rate_id' && $_available_methods[$chosen_method] instanceof jigoshop_calculable_shipping) : self::$shipping_total = $_available_methods[$chosen_method]->get_selected_price( jigoshop_session::instance()->selected_rate_id ); self::$shipping_tax = $_available_methods[$chosen_method]->get_selected_tax( jigoshop_session::instance()->selected_rate_id ); else : self::$shipping_total = $_available_methods[$chosen_method]->shipping_total; self::$shipping_tax = $_available_methods[$chosen_method]->shipping_tax; endif; else : self::$shipping_total = $_available_methods[$chosen_method]->shipping_total; self::$shipping_tax = $_available_methods[$chosen_method]->shipping_tax; endif; self::$shipping_label = $_available_methods[$chosen_method]->title; endif; endif; //sizeof available methods endif; //self enabled == 'yes' } public static function reset_shipping() { self::$shipping_total = 0; self::$shipping_tax = 0; self::$shipping_label = null; self::$has_calculable_shipping = false; self::$shipping_error_message = null; } }