2011-08-09 15:16:18 +00:00
|
|
|
<?php
|
2013-02-20 17:14:46 +00:00
|
|
|
|
2014-09-20 18:39:49 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly
|
|
|
|
}
|
2013-02-20 17:14:46 +00:00
|
|
|
|
2011-08-10 17:11:11 +00:00
|
|
|
/**
|
|
|
|
* WooCommerce Shipping Method Class
|
2012-08-12 14:12:52 +00:00
|
|
|
*
|
2011-08-10 17:11:11 +00:00
|
|
|
* Extended by shipping methods to handle shipping calculations etc.
|
|
|
|
*
|
2014-08-31 05:49:58 +00:00
|
|
|
* @class WC_Shipping_Method
|
|
|
|
* @version 1.6.4
|
|
|
|
* @package WooCommerce/Abstracts
|
|
|
|
* @category Abstract Class
|
|
|
|
* @author WooThemes
|
2012-08-12 14:12:52 +00:00
|
|
|
*/
|
2012-12-31 18:25:09 +00:00
|
|
|
abstract class WC_Shipping_Method extends WC_Settings_API {
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2012-08-15 17:08:42 +00:00
|
|
|
/** @var string Unique ID for the shipping method - must be set. */
|
2012-08-14 22:43:48 +00:00
|
|
|
var $id;
|
|
|
|
|
2012-08-15 17:08:42 +00:00
|
|
|
/** @var int Optional instance ID. */
|
2012-08-14 22:43:48 +00:00
|
|
|
var $number;
|
|
|
|
|
2012-08-15 17:08:42 +00:00
|
|
|
/** @var string Method title */
|
2012-08-14 22:43:48 +00:00
|
|
|
var $method_title;
|
|
|
|
|
2012-08-15 17:08:42 +00:00
|
|
|
/** @var string User set title */
|
2012-08-14 22:43:48 +00:00
|
|
|
var $title;
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2012-08-15 17:08:42 +00:00
|
|
|
/** @var bool True if the method is available. */
|
2011-08-09 15:16:18 +00:00
|
|
|
var $availability;
|
2012-08-14 22:43:48 +00:00
|
|
|
|
2012-08-15 17:08:42 +00:00
|
|
|
/** @var array Array of countries this method is enabled for. */
|
2014-02-13 11:13:56 +00:00
|
|
|
var $countries = array();
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2012-08-15 17:08:42 +00:00
|
|
|
/** @var string If 'taxable' tax will be charged for this method (if applicable) */
|
2014-08-31 05:49:58 +00:00
|
|
|
var $tax_status = 'taxable';
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2012-08-15 17:08:42 +00:00
|
|
|
/** @var mixed Fees for the method */
|
2014-08-31 05:49:58 +00:00
|
|
|
var $fee = 0;
|
2012-08-14 22:43:48 +00:00
|
|
|
|
2012-08-15 17:08:42 +00:00
|
|
|
/** @var float Minimum fee for the method */
|
2014-08-31 05:49:58 +00:00
|
|
|
var $minimum_fee = null;
|
2012-08-14 22:43:48 +00:00
|
|
|
|
2012-08-15 17:08:42 +00:00
|
|
|
/** @var bool Enabled for disabled */
|
2014-08-31 05:49:58 +00:00
|
|
|
var $enabled = false;
|
2012-08-14 22:43:48 +00:00
|
|
|
|
2012-08-15 17:08:42 +00:00
|
|
|
/** @var bool Whether the method has settings or not (In WooCommerce > Settings > Shipping) */
|
2014-08-31 05:49:58 +00:00
|
|
|
var $has_settings = true;
|
2012-08-14 22:43:48 +00:00
|
|
|
|
2012-08-15 17:08:42 +00:00
|
|
|
/** @var array Features this method supports. */
|
2014-11-17 16:30:15 +00:00
|
|
|
var $supports = array();
|
2012-08-14 22:43:48 +00:00
|
|
|
|
2012-08-15 17:08:42 +00:00
|
|
|
/** @var array This is an array of rates - methods must populate this array to register shipping costs */
|
2014-08-31 05:49:58 +00:00
|
|
|
var $rates = array();
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2013-10-16 13:31:31 +00:00
|
|
|
/**
|
|
|
|
* Whether or not we need to calculate tax on top of the shipping rate
|
2014-08-31 05:49:58 +00:00
|
|
|
*
|
2013-10-16 13:31:31 +00:00
|
|
|
* @return boolean
|
|
|
|
*/
|
|
|
|
public function is_taxable() {
|
|
|
|
return ( get_option( 'woocommerce_calc_taxes' ) == 'yes' && $this->tax_status == 'taxable' && ! WC()->customer->is_vat_exempt() );
|
|
|
|
}
|
|
|
|
|
2012-01-04 16:24:26 +00:00
|
|
|
/**
|
|
|
|
* Add a rate
|
|
|
|
*
|
2012-08-14 22:43:48 +00:00
|
|
|
* Add a shipping rate. If taxes are not set they will be calculated based on cost.
|
|
|
|
*
|
|
|
|
* @param array $args (default: array())
|
2012-01-04 16:24:26 +00:00
|
|
|
*/
|
2014-08-31 05:49:58 +00:00
|
|
|
public function add_rate( $args = array() ) {
|
2012-01-03 19:07:32 +00:00
|
|
|
$defaults = array(
|
2014-08-31 05:49:58 +00:00
|
|
|
'id' => '', // ID for the rate
|
|
|
|
'label' => '', // Label for the rate
|
|
|
|
'cost' => '0', // Amount or array of costs (per item shipping)
|
|
|
|
'taxes' => '', // Pass taxes, nothing to have it calculated for you, or 'false' to calc no tax
|
|
|
|
'calc_tax' => 'per_order' // Calc tax per_order or per_item. Per item needs an array of costs
|
2012-01-03 19:07:32 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
$args = wp_parse_args( $args, $defaults );
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2012-01-03 19:07:32 +00:00
|
|
|
extract( $args );
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2012-01-03 19:07:32 +00:00
|
|
|
// Id and label are required
|
2014-11-17 16:30:15 +00:00
|
|
|
if ( ! $id || ! $label ) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2012-01-14 16:42:04 +00:00
|
|
|
// Handle cost
|
2014-07-30 10:04:01 +00:00
|
|
|
$total_cost = round( ( is_array( $cost ) ) ? array_sum( $cost ) : $cost, absint( get_option( 'woocommerce_price_num_decimals' ) ) );
|
2012-08-12 14:12:52 +00:00
|
|
|
|
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
|
2013-03-03 17:07:31 +00:00
|
|
|
// This saves shipping methods having to do complex tax calculations
|
2013-10-16 13:31:31 +00:00
|
|
|
if ( ! is_array( $taxes ) && $taxes !== false && $total_cost > 0 && $this->is_taxable() ) {
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2014-08-31 05:49:58 +00:00
|
|
|
$taxes = array();
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2012-05-29 12:32:54 +00:00
|
|
|
switch ( $calc_tax ) {
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2012-01-04 16:24:26 +00:00
|
|
|
case "per_item" :
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2012-01-14 16:42:04 +00:00
|
|
|
// If we have an array of costs we can look up each items tax class and add tax accordingly
|
2012-05-29 12:32:54 +00:00
|
|
|
if ( is_array( $cost ) ) {
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2013-11-25 14:01:32 +00:00
|
|
|
$cart = WC()->cart->get_cart();
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2012-05-29 12:32:54 +00:00
|
|
|
foreach ( $cost as $cost_key => $amount ) {
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2014-08-31 05:49:58 +00:00
|
|
|
if ( ! isset( $cart[ $cost_key ] ) ) {
|
2012-05-29 12:32:54 +00:00
|
|
|
continue;
|
2014-08-31 05:49:58 +00:00
|
|
|
}
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2012-05-29 12:32:54 +00:00
|
|
|
$_product = $cart[ $cost_key ]['data'];
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2014-06-12 15:47:43 +00:00
|
|
|
$rates = WC_Tax::get_shipping_tax_rates( $_product->get_tax_class() );
|
|
|
|
$item_taxes = WC_Tax::calc_shipping_tax( $amount, $rates );
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2012-01-04 16:24:26 +00:00
|
|
|
// Sum the item taxes
|
2014-08-31 05:49:58 +00:00
|
|
|
foreach ( array_keys( $taxes + $item_taxes ) as $key ) {
|
2012-05-29 12:32:54 +00:00
|
|
|
$taxes[ $key ] = ( isset( $item_taxes[ $key ] ) ? $item_taxes[ $key ] : 0 ) + ( isset( $taxes[ $key ] ) ? $taxes[ $key ] : 0 );
|
2014-08-31 05:49:58 +00:00
|
|
|
}
|
2012-05-29 12:32:54 +00:00
|
|
|
}
|
2012-08-12 14:12:52 +00:00
|
|
|
|
|
|
|
// Add any cost for the order - order costs are in the key 'order'
|
2012-05-29 12:32:54 +00:00
|
|
|
if ( isset( $cost['order'] ) ) {
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2014-06-12 15:47:43 +00:00
|
|
|
$rates = WC_Tax::get_shipping_tax_rates();
|
|
|
|
$item_taxes = WC_Tax::calc_shipping_tax( $cost['order'], $rates );
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2012-02-29 11:49:39 +00:00
|
|
|
// Sum the item taxes
|
2014-08-31 05:49:58 +00:00
|
|
|
foreach ( array_keys( $taxes + $item_taxes ) as $key ) {
|
2012-05-29 12:32:54 +00:00
|
|
|
$taxes[ $key ] = ( isset( $item_taxes[ $key ] ) ? $item_taxes[ $key ] : 0 ) + ( isset( $taxes[ $key ] ) ? $taxes[ $key ] : 0 );
|
2014-08-31 05:49:58 +00:00
|
|
|
}
|
2012-02-29 11:49:39 +00:00
|
|
|
}
|
2012-05-29 12:32:54 +00:00
|
|
|
}
|
2012-01-14 16:42:04 +00:00
|
|
|
|
2012-01-04 16:24:26 +00:00
|
|
|
break;
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2012-01-04 16:24:26 +00:00
|
|
|
default :
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2014-06-12 15:47:43 +00:00
|
|
|
$rates = WC_Tax::get_shipping_tax_rates();
|
|
|
|
$taxes = WC_Tax::calc_shipping_tax( $total_cost, $rates );
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2012-01-04 16:24:26 +00:00
|
|
|
break;
|
2012-05-29 12:32:54 +00:00
|
|
|
}
|
|
|
|
}
|
2012-01-04 16:24:26 +00:00
|
|
|
|
2012-11-07 16:54:38 +00:00
|
|
|
$this->rates[] = new WC_Shipping_Rate( $id, $label, $total_cost, $taxes, $this->id );
|
2012-01-03 19:07:32 +00:00
|
|
|
}
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2012-08-14 22:43:48 +00:00
|
|
|
/**
|
|
|
|
* has_settings function.
|
|
|
|
*
|
2013-11-28 12:54:19 +00:00
|
|
|
* @return bool
|
2012-08-14 22:43:48 +00:00
|
|
|
*/
|
2014-08-31 05:49:58 +00:00
|
|
|
public function has_settings() {
|
2014-11-17 16:30:15 +00:00
|
|
|
return $this->has_settings;
|
2012-05-29 12:32:54 +00:00
|
|
|
}
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2014-08-31 05:49:58 +00:00
|
|
|
/**
|
|
|
|
* is_available function.
|
|
|
|
*
|
|
|
|
* @param array $package
|
|
|
|
* @return bool
|
|
|
|
*/
|
|
|
|
public function is_available( $package ) {
|
2014-10-11 01:46:19 +00:00
|
|
|
if ( 'no' == $this->enabled ) {
|
2014-08-31 05:49:58 +00:00
|
|
|
return false;
|
|
|
|
}
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2013-08-22 10:58:03 +00:00
|
|
|
// Country availability
|
2014-10-11 02:01:19 +00:00
|
|
|
$countries = is_array( $this->countries ) ? $this->countries : array();
|
|
|
|
|
2013-08-22 10:58:03 +00:00
|
|
|
switch ( $this->availability ) {
|
2014-08-31 05:49:58 +00:00
|
|
|
|
2013-08-22 10:58:03 +00:00
|
|
|
case 'specific' :
|
|
|
|
case 'including' :
|
2014-10-11 01:46:19 +00:00
|
|
|
$ship_to_countries = array_intersect( $countries, array_keys( WC()->countries->get_shipping_countries() ) );
|
2013-08-22 10:58:03 +00:00
|
|
|
break;
|
2014-08-31 05:49:58 +00:00
|
|
|
|
2013-08-22 10:58:03 +00:00
|
|
|
case 'excluding' :
|
2014-10-11 02:01:19 +00:00
|
|
|
$ship_to_countries = array_diff( array_keys( WC()->countries->get_shipping_countries() ), $countries );
|
2013-08-22 10:58:03 +00:00
|
|
|
break;
|
2014-08-31 05:49:58 +00:00
|
|
|
|
2013-08-22 10:58:03 +00:00
|
|
|
default :
|
2013-11-25 14:01:32 +00:00
|
|
|
$ship_to_countries = array_keys( WC()->countries->get_shipping_countries() );
|
2013-08-22 10:58:03 +00:00
|
|
|
break;
|
|
|
|
}
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2014-02-13 11:13:56 +00:00
|
|
|
if ( ! in_array( $package['destination']['country'], $ship_to_countries ) ) {
|
2013-08-22 10:58:03 +00:00
|
|
|
return false;
|
2014-02-13 11:13:56 +00:00
|
|
|
}
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2012-08-21 21:53:27 +00:00
|
|
|
return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', true, $package );
|
2014-08-31 05:49:58 +00:00
|
|
|
}
|
2012-06-29 23:20:07 +00:00
|
|
|
|
|
|
|
/**
|
2014-11-17 16:30:15 +00:00
|
|
|
* Return the shipping method title
|
2012-08-12 14:12:52 +00:00
|
|
|
*
|
2013-08-22 10:58:03 +00:00
|
|
|
* @return string
|
2012-06-29 23:20:07 +00:00
|
|
|
*/
|
2013-08-22 10:58:03 +00:00
|
|
|
public function get_title() {
|
2012-06-29 23:20:07 +00:00
|
|
|
return apply_filters( 'woocommerce_shipping_method_title', $this->title, $this->id );
|
|
|
|
}
|
2012-08-12 14:12:52 +00:00
|
|
|
|
2014-08-31 05:49:58 +00:00
|
|
|
/**
|
|
|
|
* get_fee function.
|
|
|
|
*
|
|
|
|
* @param mixed $fee
|
|
|
|
* @param mixed $total
|
|
|
|
* @return float
|
|
|
|
*/
|
|
|
|
public function get_fee( $fee, $total ) {
|
2014-11-17 16:30:15 +00:00
|
|
|
if ( strstr( $fee, '%' ) ) {
|
2012-04-12 10:04:15 +00:00
|
|
|
$fee = ( $total / 100 ) * str_replace( '%', '', $fee );
|
2014-11-17 16:30:15 +00:00
|
|
|
}
|
|
|
|
if ( ! empty( $this->minimum_fee ) && $this->minimum_fee > $fee ) {
|
|
|
|
$fee = $this->minimum_fee;
|
|
|
|
}
|
2012-04-12 10:04:15 +00:00
|
|
|
return $fee;
|
2012-08-12 14:12:52 +00:00
|
|
|
}
|
|
|
|
|
2012-05-29 12:32:54 +00:00
|
|
|
/**
|
|
|
|
* Check if a shipping method supports a given feature.
|
2012-08-12 14:12:52 +00:00
|
|
|
*
|
|
|
|
* Methods should override this to declare support (or lack of support) for a feature.
|
|
|
|
*
|
2012-05-29 12:32:54 +00:00
|
|
|
* @param $feature string The name of a feature to test support for.
|
2012-08-12 14:12:52 +00:00
|
|
|
* @return bool True if the gateway supports the feature, false otherwise.
|
2012-05-29 12:32:54 +00:00
|
|
|
* @since 1.5.7
|
|
|
|
*/
|
2014-08-31 05:49:58 +00:00
|
|
|
public function supports( $feature ) {
|
2012-06-29 23:20:07 +00:00
|
|
|
return apply_filters( 'woocommerce_shipping_method_supports', in_array( $feature, $this->supports ) ? true : false, $feature, $this );
|
2012-05-29 12:32:54 +00:00
|
|
|
}
|
2013-11-28 13:12:08 +00:00
|
|
|
}
|