2011-08-10 17:11:11 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WooCommerce Shipping Class
|
|
|
|
*
|
|
|
|
* Handles shipping and loads shipping methods via hooks.
|
|
|
|
*
|
|
|
|
* @class woocommerce_shipping
|
|
|
|
* @package WooCommerce
|
|
|
|
* @category Shipping
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
|
|
|
class woocommerce_shipping {
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
private static $_instance;
|
|
|
|
|
|
|
|
public static $enabled = false;
|
|
|
|
public static $shipping_methods = array();
|
|
|
|
public static $chosen_method = null;
|
|
|
|
public static $shipping_total = 0;
|
|
|
|
public static $shipping_tax = 0;
|
|
|
|
public static $shipping_label = null;
|
|
|
|
|
|
|
|
public static function init() {
|
|
|
|
|
2011-08-10 17:11:11 +00:00
|
|
|
if (get_option('woocommerce_calc_shipping')!='no') self::$enabled = true;
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2011-08-10 17:11:11 +00:00
|
|
|
do_action('woocommerce_shipping_init');
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2011-08-10 17:11:11 +00:00
|
|
|
$load_methods = apply_filters('woocommerce_shipping_methods', array());
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
foreach ($load_methods as $method) :
|
|
|
|
|
|
|
|
self::$shipping_methods[] = &new $method();
|
|
|
|
|
|
|
|
endforeach;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public static function get() {
|
|
|
|
if (!isset(self::$_instance)) {
|
|
|
|
$c = __CLASS__;
|
|
|
|
self::$_instance = new $c;
|
|
|
|
}
|
|
|
|
return self::$_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_available_shipping_methods() {
|
|
|
|
|
|
|
|
if (self::$enabled=='yes') :
|
|
|
|
|
|
|
|
$_available_methods = array();
|
2011-09-02 14:42:04 +00:00
|
|
|
|
|
|
|
foreach ( self::$shipping_methods as $shipping_method ) :
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2011-09-02 14:42:04 +00:00
|
|
|
if ($shipping_method->is_available()) :
|
|
|
|
|
|
|
|
$shipping_method->calculate_shipping();
|
|
|
|
|
|
|
|
// If available, put available methods/rates in the array
|
|
|
|
if ($shipping_method->multiple_rates) :
|
|
|
|
|
|
|
|
foreach ($shipping_method->rates as $rate) :
|
|
|
|
|
|
|
|
$method = $rate;
|
|
|
|
|
|
|
|
$_available_methods[$method->id] = $method;
|
|
|
|
|
|
|
|
endforeach;
|
|
|
|
|
|
|
|
else :
|
|
|
|
|
|
|
|
$method = $shipping_method;
|
|
|
|
|
|
|
|
$_available_methods[$method->id] = $method;
|
|
|
|
|
|
|
|
endif;
|
|
|
|
|
|
|
|
endif;
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
endforeach;
|
2011-09-02 14:42:04 +00:00
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
return $_available_methods;
|
|
|
|
|
|
|
|
endif;
|
|
|
|
}
|
|
|
|
|
|
|
|
function reset_shipping_methods() {
|
2011-09-02 14:42:04 +00:00
|
|
|
foreach ( self::$shipping_methods as $shipping_method ) :
|
|
|
|
$shipping_method->shipping_total = 0;
|
|
|
|
$shipping_method->shipping_tax = 0;
|
|
|
|
$shipping_method->rates = array();
|
2011-08-09 15:16:18 +00:00
|
|
|
endforeach;
|
|
|
|
}
|
|
|
|
|
|
|
|
function calculate_shipping() {
|
|
|
|
|
|
|
|
if (self::$enabled=='yes') :
|
|
|
|
|
|
|
|
self::$shipping_total = 0;
|
|
|
|
self::$shipping_tax = 0;
|
|
|
|
self::$shipping_label = null;
|
|
|
|
$_cheapest_fee = '';
|
|
|
|
$_cheapest_method = '';
|
2011-09-02 14:42:04 +00:00
|
|
|
if (isset($_SESSION['_chosen_shipping_method'])) $chosen_method = $_SESSION['_chosen_shipping_method']; else $chosen_method = '';
|
2011-08-09 15:16:18 +00:00
|
|
|
$calc_cheapest = false;
|
|
|
|
|
|
|
|
if (!$chosen_method || empty($chosen_method)) $calc_cheapest = true;
|
|
|
|
|
|
|
|
self::reset_shipping_methods();
|
|
|
|
|
|
|
|
$_available_methods = self::get_available_shipping_methods();
|
|
|
|
|
|
|
|
if (sizeof($_available_methods)>0) :
|
|
|
|
|
2011-09-02 14:42:04 +00:00
|
|
|
foreach ($_available_methods as $method_id => $method) :
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
$fee = $method->shipping_total;
|
|
|
|
if ($fee < $_cheapest_fee || !is_numeric($_cheapest_fee)) :
|
2011-09-02 14:42:04 +00:00
|
|
|
$_cheapest_fee = $fee;
|
|
|
|
$_cheapest_method = $method_id;
|
2011-08-09 15:16:18 +00:00
|
|
|
endif;
|
2011-09-02 14:42:04 +00:00
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
endforeach;
|
|
|
|
|
|
|
|
// Default to cheapest
|
|
|
|
if ($calc_cheapest || !isset($_available_methods[$chosen_method])) :
|
|
|
|
$chosen_method = $_cheapest_method;
|
|
|
|
endif;
|
|
|
|
|
|
|
|
if ($chosen_method) :
|
|
|
|
|
2011-09-02 14:42:04 +00:00
|
|
|
$_SESSION['_chosen_shipping_method'] = $chosen_method;
|
2011-08-09 15:16:18 +00:00
|
|
|
self::$shipping_total = $_available_methods[$chosen_method]->shipping_total;
|
|
|
|
self::$shipping_tax = $_available_methods[$chosen_method]->shipping_tax;
|
|
|
|
self::$shipping_label = $_available_methods[$chosen_method]->title;
|
|
|
|
|
|
|
|
endif;
|
|
|
|
endif;
|
|
|
|
|
|
|
|
endif;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
function reset_shipping() {
|
2011-09-02 14:42:04 +00:00
|
|
|
unset($_SESSION['_chosen_shipping_method']);
|
2011-08-09 15:16:18 +00:00
|
|
|
self::$shipping_total = 0;
|
|
|
|
self::$shipping_tax = 0;
|
|
|
|
self::$shipping_label = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|