2011-08-10 17:11:11 +00:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* WooCommerce Shipping Class
|
|
|
|
*
|
|
|
|
* Handles shipping and loads shipping methods via hooks.
|
|
|
|
*
|
2012-01-21 09:01:41 +00:00
|
|
|
* @class Woocommerce_Shipping
|
2011-08-10 17:11:11 +00:00
|
|
|
* @package WooCommerce
|
|
|
|
* @category Shipping
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
2012-01-21 09:01:41 +00:00
|
|
|
class Woocommerce_Shipping {
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2011-09-06 11:11:22 +00:00
|
|
|
var $enabled = false;
|
|
|
|
var $shipping_methods = array();
|
|
|
|
var $chosen_method = null;
|
|
|
|
var $shipping_total = 0;
|
2012-01-02 00:49:00 +00:00
|
|
|
var $shipping_taxes = array();
|
2011-09-06 11:11:22 +00:00
|
|
|
var $shipping_label = null;
|
2011-12-02 18:54:52 +00:00
|
|
|
var $shipping_classes;
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2011-12-08 13:13:59 +00:00
|
|
|
function init() {
|
2011-12-19 14:05:32 +00:00
|
|
|
|
2011-09-06 11:11:22 +00:00
|
|
|
if (get_option('woocommerce_calc_shipping')!='no') $this->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
|
|
|
|
2011-12-19 14:05:32 +00:00
|
|
|
// Get order option
|
|
|
|
$ordering = (array) get_option('woocommerce_shipping_method_order');
|
|
|
|
$order_end = 999;
|
|
|
|
|
|
|
|
// Load gateways in order
|
|
|
|
foreach ($load_methods as $method) :
|
|
|
|
|
2012-01-10 15:11:06 +00:00
|
|
|
$load_method = new $method();
|
2011-12-19 14:05:32 +00:00
|
|
|
|
|
|
|
if (isset($ordering[$load_method->id]) && is_numeric($ordering[$load_method->id])) :
|
|
|
|
// Add in position
|
|
|
|
$this->shipping_methods[$ordering[$load_method->id]] = $load_method;
|
|
|
|
else :
|
|
|
|
// Add to end of the array
|
|
|
|
$this->shipping_methods[$order_end] = $load_method;
|
|
|
|
$order_end++;
|
|
|
|
endif;
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
endforeach;
|
|
|
|
|
2011-12-19 14:05:32 +00:00
|
|
|
ksort($this->shipping_methods);
|
|
|
|
|
|
|
|
add_action('woocommerce_update_options_shipping', array(&$this, 'process_admin_options'));
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
2011-12-02 18:54:52 +00:00
|
|
|
|
|
|
|
function get_shipping_classes() {
|
|
|
|
if (!is_array($this->shipping_classes)) :
|
|
|
|
|
|
|
|
$args = array(
|
2011-12-09 16:45:03 +00:00
|
|
|
'hide_empty' => '0'
|
2011-12-02 18:54:52 +00:00
|
|
|
);
|
|
|
|
$classes = get_terms( 'product_shipping_class', $args );
|
|
|
|
|
|
|
|
$this->shipping_classes = ($classes) ? $classes : array();
|
|
|
|
|
|
|
|
endif;
|
|
|
|
|
|
|
|
return (array) $this->shipping_classes;
|
|
|
|
}
|
2011-09-06 11:11:22 +00:00
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
function get_available_shipping_methods() {
|
2011-09-06 11:11:22 +00:00
|
|
|
if ($this->enabled=='yes') :
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
$_available_methods = array();
|
2011-09-02 14:42:04 +00:00
|
|
|
|
2011-09-06 11:11:22 +00:00
|
|
|
foreach ( $this->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
|
2012-01-03 19:07:32 +00:00
|
|
|
if (is_array($shipping_method->rates) && sizeof($shipping_method->rates) > 0) :
|
|
|
|
foreach ($shipping_method->rates as $rate) $_available_methods[$rate->id] = $rate;
|
2011-09-02 14:42:04 +00:00
|
|
|
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-06 11:11:22 +00:00
|
|
|
foreach ( $this->shipping_methods as $shipping_method ) :
|
2011-09-02 14:42:04 +00:00
|
|
|
$shipping_method->rates = array();
|
2011-08-09 15:16:18 +00:00
|
|
|
endforeach;
|
|
|
|
}
|
|
|
|
|
|
|
|
function calculate_shipping() {
|
2011-09-06 11:11:22 +00:00
|
|
|
if ($this->enabled=='yes') :
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2011-09-06 11:11:22 +00:00
|
|
|
$this->shipping_total = 0;
|
2012-01-02 00:49:00 +00:00
|
|
|
$this->shipping_taxes = array();
|
2011-09-06 11:11:22 +00:00
|
|
|
$this->shipping_label = null;
|
2012-01-03 19:07:32 +00:00
|
|
|
$_cheapest_cost = '';
|
2011-08-09 15:16:18 +00:00
|
|
|
$_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
|
|
|
|
2011-09-06 11:11:22 +00:00
|
|
|
$this->reset_shipping_methods();
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2011-09-06 11:11:22 +00:00
|
|
|
$_available_methods = $this->get_available_shipping_methods();
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
if (sizeof($_available_methods)>0) :
|
|
|
|
|
2012-01-06 14:28:08 +00:00
|
|
|
// If not set, set a default
|
|
|
|
if (!$chosen_method || empty($chosen_method) || !isset($_available_methods[$chosen_method])) :
|
|
|
|
|
|
|
|
$chosen_method = get_option('woocommerce_default_shipping_method');
|
|
|
|
|
|
|
|
if (!$chosen_method || empty($chosen_method) || !isset($_available_methods[$chosen_method])) :
|
|
|
|
// Default to cheapest
|
|
|
|
foreach ($_available_methods as $method_id => $method) :
|
|
|
|
if ($method->cost < $_cheapest_cost || !is_numeric($_cheapest_cost)) :
|
|
|
|
$_cheapest_cost = $method->cost;
|
|
|
|
$_cheapest_method = $method_id;
|
|
|
|
endif;
|
|
|
|
endforeach;
|
|
|
|
|
|
|
|
$chosen_method = $_cheapest_method;
|
2011-08-09 15:16:18 +00:00
|
|
|
endif;
|
|
|
|
|
|
|
|
endif;
|
2012-01-06 14:28:08 +00:00
|
|
|
|
2011-08-09 15:16:18 +00:00
|
|
|
if ($chosen_method) :
|
2011-09-02 14:42:04 +00:00
|
|
|
$_SESSION['_chosen_shipping_method'] = $chosen_method;
|
2012-01-03 19:07:32 +00:00
|
|
|
$this->shipping_total = $_available_methods[$chosen_method]->cost;
|
|
|
|
$this->shipping_taxes = $_available_methods[$chosen_method]->taxes;
|
|
|
|
$this->shipping_label = $_available_methods[$chosen_method]->label;
|
2011-08-09 15:16:18 +00:00
|
|
|
endif;
|
|
|
|
endif;
|
|
|
|
|
|
|
|
endif;
|
|
|
|
}
|
|
|
|
|
|
|
|
function reset_shipping() {
|
2011-09-02 14:42:04 +00:00
|
|
|
unset($_SESSION['_chosen_shipping_method']);
|
2011-09-06 11:11:22 +00:00
|
|
|
$this->shipping_total = 0;
|
2012-01-02 00:49:00 +00:00
|
|
|
$this->shipping_taxes = array();
|
2011-09-06 11:11:22 +00:00
|
|
|
$this->shipping_label = null;
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
|
|
|
|
2011-12-19 14:05:32 +00:00
|
|
|
function process_admin_options() {
|
2012-01-06 14:28:08 +00:00
|
|
|
|
|
|
|
$default_shipping_method = (isset($_POST['default_shipping_method'])) ? esc_attr($_POST['default_shipping_method']) : '';
|
2011-12-19 14:05:32 +00:00
|
|
|
$method_order = (isset($_POST['method_order'])) ? $_POST['method_order'] : '';
|
|
|
|
|
|
|
|
$order = array();
|
|
|
|
|
|
|
|
if (is_array($method_order) && sizeof($method_order)>0) :
|
|
|
|
$loop = 0;
|
|
|
|
foreach ($method_order as $method_id) :
|
|
|
|
$order[$method_id] = $loop;
|
|
|
|
$loop++;
|
|
|
|
endforeach;
|
|
|
|
endif;
|
|
|
|
|
2012-01-06 14:28:08 +00:00
|
|
|
update_option( 'woocommerce_default_shipping_method', $default_shipping_method );
|
2011-12-19 14:05:32 +00:00
|
|
|
update_option( 'woocommerce_shipping_method_order', $order );
|
|
|
|
}
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|