2011-08-09 15:16:18 +00:00
|
|
|
<?php
|
2011-08-10 17:11:11 +00:00
|
|
|
/**
|
|
|
|
* WooCommerce Shipping Method Class
|
|
|
|
*
|
|
|
|
* Extended by shipping methods to handle shipping calculations etc.
|
|
|
|
*
|
|
|
|
* @class woocommerce_shipping_method
|
|
|
|
* @package WooCommerce
|
|
|
|
* @category Shipping
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
2011-11-28 15:50:19 +00:00
|
|
|
class woocommerce_shipping_method extends woocommerce_settings_api {
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
var $id;
|
2012-01-03 19:07:32 +00:00
|
|
|
var $method_title; // Method title
|
|
|
|
var $title; // User set title
|
2012-01-04 16:24:26 +00:00
|
|
|
var $tax_status;
|
2011-08-09 15:16:18 +00:00
|
|
|
var $availability;
|
|
|
|
var $countries;
|
|
|
|
var $type;
|
|
|
|
var $fee = 0;
|
|
|
|
var $min_amount = null;
|
|
|
|
var $enabled = false;
|
2012-01-03 19:07:32 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Rates
|
|
|
|
*
|
|
|
|
* This is an array of rates - methods must populate this array to register shipping costs
|
|
|
|
*/
|
|
|
|
var $rates = array(); // This is an array of rates - methods must populate this array to register shipping costs
|
|
|
|
|
2012-01-04 16:24:26 +00:00
|
|
|
/**
|
|
|
|
* Add a rate
|
|
|
|
*
|
|
|
|
* A a shipping rate. If taxes are not set they will be calculated based on cost.
|
|
|
|
*/
|
2012-01-03 19:07:32 +00:00
|
|
|
function add_rate( $args = array() ) {
|
2012-01-04 16:24:26 +00:00
|
|
|
global $woocommerce;
|
|
|
|
|
2012-01-03 19:07:32 +00:00
|
|
|
$defaults = array(
|
|
|
|
'id' => '',
|
|
|
|
'label' => '',
|
|
|
|
'cost' => '0',
|
2012-01-04 16:24:26 +00:00
|
|
|
'taxes' => '',
|
|
|
|
'calc_tax' => 'per_order'
|
2012-01-03 19:07:32 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$args = wp_parse_args( $args, $defaults );
|
|
|
|
|
|
|
|
extract( $args );
|
|
|
|
|
|
|
|
// Id and label are required
|
|
|
|
if (!$id || !$label) return;
|
|
|
|
|
2012-01-04 16:24:26 +00:00
|
|
|
// Taxes - if not an array and not set to false, calc tax based on cost and passed calc_tax variable
|
|
|
|
// This saves shipping methods having to do compelex tax calculations
|
|
|
|
if (!is_array($taxes) && $taxes!==false && $cost>0 && get_option('woocommerce_calc_taxes')=='yes' && $this->tax_status=='taxable' ) :
|
|
|
|
|
2012-01-10 15:11:06 +00:00
|
|
|
$_tax = new woocommerce_tax();
|
2012-01-04 16:24:26 +00:00
|
|
|
$taxes = array();
|
|
|
|
|
|
|
|
switch ($calc_tax) :
|
|
|
|
|
|
|
|
case "per_item" :
|
|
|
|
if (sizeof($woocommerce->cart->get_cart())>0) : foreach ($woocommerce->cart->get_cart() as $item_id => $values) :
|
|
|
|
|
|
|
|
$_product = $values['data'];
|
|
|
|
|
|
|
|
if ($values['quantity']>0 && $_product->needs_shipping() && $_product->is_shipping_taxable()) :
|
|
|
|
|
|
|
|
$rates = $_tax->get_shipping_tax_rates( $_product->get_tax_class() );
|
|
|
|
$item_taxes = $_tax->calc_shipping_tax( $cost, $rates );
|
|
|
|
|
|
|
|
// Sum the item taxes
|
|
|
|
foreach (array_keys($taxes + $item_taxes) as $key) :
|
|
|
|
$taxes[$key] = (isset($item_taxes[$key]) ? $item_taxes[$key] : 0) + (isset($taxes[$key]) ? $taxes[$key] : 0);
|
|
|
|
endforeach;
|
|
|
|
|
|
|
|
endif;
|
|
|
|
endforeach; endif;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case "per_order" :
|
|
|
|
default :
|
|
|
|
|
|
|
|
$rates = $_tax->get_shipping_tax_rates();
|
|
|
|
$taxes = $_tax->calc_shipping_tax( $cost, $rates );
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
endswitch;
|
|
|
|
|
|
|
|
endif;
|
|
|
|
|
|
|
|
$this->rates[] = new woocommerce_shipping_rate( $id, $label, $cost, $taxes );
|
2012-01-03 19:07:32 +00:00
|
|
|
}
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2011-09-06 11:18:22 +00:00
|
|
|
function is_available() {
|
2011-09-06 11:11:22 +00:00
|
|
|
global $woocommerce;
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2012-01-03 19:07:32 +00:00
|
|
|
if ($this->enabled=="no")
|
|
|
|
return false;
|
2011-08-09 15:16:18 +00:00
|
|
|
|
2012-01-03 19:07:32 +00:00
|
|
|
if (isset($woocommerce->cart->cart_contents_total) && isset($this->min_amount) && $this->min_amount && $this->min_amount > $woocommerce->cart->cart_contents_total)
|
|
|
|
return false;
|
2011-08-09 15:16:18 +00:00
|
|
|
|
|
|
|
$ship_to_countries = '';
|
|
|
|
|
|
|
|
if ($this->availability == 'specific') :
|
|
|
|
$ship_to_countries = $this->countries;
|
|
|
|
else :
|
2011-08-10 17:11:11 +00:00
|
|
|
if (get_option('woocommerce_allowed_countries')=='specific') :
|
|
|
|
$ship_to_countries = get_option('woocommerce_specific_allowed_countries');
|
2011-08-09 15:16:18 +00:00
|
|
|
endif;
|
|
|
|
endif;
|
|
|
|
|
|
|
|
if (is_array($ship_to_countries)) :
|
2011-09-06 11:11:22 +00:00
|
|
|
if (!in_array($woocommerce->customer->get_shipping_country(), $ship_to_countries)) return false;
|
2011-08-09 15:16:18 +00:00
|
|
|
endif;
|
|
|
|
|
2012-01-09 10:06:57 +00:00
|
|
|
return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', true );
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
|
|
|
|
2011-09-06 11:18:22 +00:00
|
|
|
function get_fee( $fee, $total ) {
|
2011-08-09 15:16:18 +00:00
|
|
|
if (strstr($fee, '%')) :
|
|
|
|
return ($total/100) * str_replace('%', '', $fee);
|
|
|
|
else :
|
|
|
|
return $fee;
|
|
|
|
endif;
|
2012-01-04 16:24:26 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* WooCommerce Shipping Rate Class
|
|
|
|
*
|
|
|
|
* Simple Class for storing rates.
|
|
|
|
*/
|
|
|
|
class woocommerce_shipping_rate {
|
|
|
|
|
|
|
|
var $id = '';
|
|
|
|
var $label = '';
|
|
|
|
var $cost = 0;
|
|
|
|
var $taxes = array();
|
|
|
|
|
|
|
|
function woocommerce_shipping_rate( $id, $label, $cost, $taxes ) {
|
|
|
|
$this->id = $id;
|
|
|
|
$this->label = $label;
|
|
|
|
$this->cost = $cost;
|
|
|
|
$this->taxes = ($taxes) ? $taxes : array();
|
|
|
|
}
|
|
|
|
|
|
|
|
function get_shipping_tax() {
|
|
|
|
$taxes = 0;
|
|
|
|
if ($this->taxes && sizeof($this->taxes)>0) :
|
|
|
|
$taxes = array_sum($this->taxes);
|
|
|
|
endif;
|
|
|
|
return $taxes;
|
2011-08-09 15:16:18 +00:00
|
|
|
}
|
|
|
|
}
|