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' ) ) {
2015-12-11 14:11:12 +00:00
exit ;
2014-09-20 18:39:49 +00:00
}
2013-02-20 17:14:46 +00:00
2011-08-10 17:11:11 +00:00
/**
2015-11-03 12:28:01 +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
2015-12-11 16:41:38 +00:00
* @ version 2.6 . 0
2014-08-31 05:49:58 +00:00
* @ 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
2015-12-11 16:41:38 +00:00
/**
* Features this method supports . Possible features used by core :
2015-12-16 13:52:46 +00:00
* - shipping - zones Shipping zone functionality + instances
* - instance - settings Instance settings screens .
* - settings Non - instance settings screens . Enabled by default for BW compatibility with methods before instances existed .
2016-03-24 18:31:39 +00:00
* - instance - settings - modal Allows the instance settings to be loaded within a modal in the zones UI .
2015-12-11 16:41:38 +00:00
* @ var array
*/
2016-02-10 13:21:16 +00:00
public $supports = array ( 'settings' );
2012-08-14 22:43:48 +00:00
2015-12-11 16:41:38 +00:00
/**
* Unique ID for the shipping method - must be set .
* @ var string
*/
2016-02-10 13:21:16 +00:00
public $id = '' ;
2012-08-14 22:43:48 +00:00
2015-12-11 16:41:38 +00:00
/**
* Method title .
* @ var string
*/
2016-02-10 13:21:16 +00:00
public $method_title = '' ;
2015-12-11 16:41:38 +00:00
/**
* Method description .
* @ var string
*/
2016-02-10 13:21:16 +00:00
public $method_description = '' ;
2015-12-11 16:41:38 +00:00
/**
* yes or no based on whether the method is enabled .
* @ var string
*/
2016-02-10 13:21:16 +00:00
public $enabled = 'yes' ;
2015-12-14 14:03:46 +00:00
/**
* Shipping method title for the frontend .
* @ var string
*/
2014-12-16 11:38:58 +00:00
public $title ;
2012-08-12 14:12:52 +00:00
2015-12-14 16:56:39 +00:00
/**
* This is an array of rates - methods must populate this array to register shipping costs .
* @ var array
*/
2016-02-10 13:21:16 +00:00
public $rates = array ();
2012-08-12 14:12:52 +00:00
2015-12-14 16:56:39 +00:00
/**
* If 'taxable' tax will be charged for this method ( if applicable ) .
* @ var string
*/
2016-02-10 13:21:16 +00:00
public $tax_status = 'taxable' ;
2012-08-12 14:12:52 +00:00
2015-12-14 16:56:39 +00:00
/**
* Fee for the method ( if applicable ) .
* @ var string
*/
2016-02-10 13:21:16 +00:00
public $fee = null ;
2012-08-14 22:43:48 +00:00
2015-12-14 16:56:39 +00:00
/**
* Minimum fee for the method ( if applicable ) .
* @ var string
*/
2016-02-10 13:21:16 +00:00
public $minimum_fee = null ;
2012-08-14 22:43:48 +00:00
2015-12-14 16:56:39 +00:00
/**
* Instance ID if used .
* @ var int
*/
2016-02-10 13:21:16 +00:00
public $instance_id = 0 ;
2015-12-16 13:52:46 +00:00
/**
* Instance form fields .
* @ var array
*/
public $instance_form_fields = array ();
2015-12-14 16:56:39 +00:00
/**
* Instance settings .
2015-12-14 14:03:46 +00:00
* @ var array
*/
2016-02-10 13:21:16 +00:00
public $instance_settings = array ();
2015-12-11 14:11:12 +00:00
2015-12-18 13:12:20 +00:00
/**
* Availability - legacy . Used for method Availability .
* No longer useful for instance based shipping methods .
* @ deprecated 2.6 . 0
* @ var string
*/
public $availability ;
/**
* Availability countries - legacy . Used for method Availability .
* No longer useful for instance based shipping methods .
* @ deprecated 2.6 . 0
2016-02-10 13:21:16 +00:00
* @ var array
2015-12-18 13:12:20 +00:00
*/
2016-02-10 13:21:16 +00:00
public $countries = array ();
2015-12-18 13:12:20 +00:00
2015-12-11 14:11:12 +00:00
/**
2015-12-11 16:41:38 +00:00
* Constructor .
2015-12-14 16:56:39 +00:00
* @ param int $instance_id
2015-12-11 14:11:12 +00:00
*/
2015-12-11 16:41:38 +00:00
public function __construct ( $instance_id = 0 ) {
$this -> instance_id = absint ( $instance_id );
}
2012-08-12 14:12:52 +00:00
2015-12-11 14:11:12 +00:00
/**
* Check if a shipping method supports a given feature .
*
* Methods should override this to declare support ( or lack of support ) for a feature .
*
* @ param $feature string The name of a feature to test support for .
* @ return bool True if the shipping method supports the feature , false otherwise .
*/
public function supports ( $feature ) {
return apply_filters ( 'woocommerce_shipping_method_supports' , in_array ( $feature , $this -> supports ), $feature , $this );
}
/**
* Called to calculate shipping rates for this method . Rates can be added using the add_rate () method .
*/
2015-12-18 17:10:58 +00:00
public function calculate_shipping ( $package = array () ) {}
2015-12-11 14:11:12 +00:00
2013-10-16 13:31:31 +00:00
/**
2015-11-03 12:28:01 +00:00
* Whether or not we need to calculate tax on top of the shipping rate .
2013-10-16 13:31:31 +00:00
* @ return boolean
*/
public function is_taxable () {
2015-12-11 16:41:38 +00:00
return wc_tax_enabled () && 'taxable' === $this -> tax_status && ! WC () -> customer -> is_vat_exempt ();
2015-12-11 14:11:12 +00:00
}
2015-12-14 16:56:39 +00:00
/**
* Whether or not this method is enabled in settings .
* @ since 2.6 . 0
* @ return boolean
*/
public function is_enabled () {
return 'yes' === $this -> enabled ;
}
2015-12-15 15:00:45 +00:00
/**
* Return the shipping method instance ID .
* @ since 2.6 . 0
* @ return int
*/
public function get_instance_id () {
return $this -> instance_id ;
}
2015-12-11 14:11:12 +00:00
/**
* Return the shipping method title .
2015-12-11 16:41:38 +00:00
* @ since 2.6 . 0
* @ return string
*/
public function get_method_title () {
return apply_filters ( 'woocommerce_shipping_method_title' , $this -> method_title , $this );
}
/**
* Return the shipping method description .
* @ since 2.6 . 0
* @ return string
*/
public function get_method_description () {
return apply_filters ( 'woocommerce_shipping_method_description' , $this -> method_description , $this );
}
/**
* Return the shipping title which is user set .
2015-12-11 14:11:12 +00:00
*
* @ return string
*/
public function get_title () {
return apply_filters ( 'woocommerce_shipping_method_title' , $this -> title , $this -> id );
2013-10-16 13:31:31 +00:00
}
2015-12-15 16:22:32 +00:00
/**
* Return calculated rates for a package .
* @ since 2.6 . 0
* @ param object $package
* @ return array
*/
public function get_rates_for_package ( $package ) {
$this -> rates = array ();
if ( $this -> is_available ( $package ) && ( empty ( $package [ 'ship_via' ] ) || in_array ( $this -> id , $package [ 'ship_via' ] ) ) ) {
$this -> calculate_shipping ( $package );
}
return $this -> rates ;
}
2012-01-04 16:24:26 +00:00
/**
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 ())
2016-03-22 17:13:39 +00:00
* @ param array $package option to store information about the package in meta .
2012-01-04 16:24:26 +00:00
*/
2016-03-22 17:13:39 +00:00
public function add_rate ( $args = array (), $package = false ) {
2015-09-07 15:02:23 +00:00
$args = wp_parse_args ( $args , array (
2016-01-08 11:42:32 +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, or leave empty to have it calculated for you, or 'false' to disable calculations
'calc_tax' => 'per_order' , // Calc tax per_order or per_item. Per item needs an array of costs
'meta_data' => array () // Array of misc meta data to store along with this rate - key value pairs.
2015-09-07 15:02:23 +00:00
) );
2012-08-12 14:12:52 +00:00
2015-12-14 16:56:39 +00:00
// ID and label are required
2015-09-07 15:02:23 +00:00
if ( ! $args [ 'id' ] || ! $args [ 'label' ] ) {
2014-11-17 16:30:15 +00:00
return ;
}
2012-08-12 14:12:52 +00:00
2016-01-27 11:06:08 +00:00
// Total up the cost
2015-09-07 15:02:23 +00:00
$total_cost = is_array ( $args [ 'cost' ] ) ? array_sum ( $args [ 'cost' ] ) : $args [ 'cost' ];
$taxes = $args [ 'taxes' ];
2012-08-12 14:12:52 +00:00
2015-12-18 13:12:20 +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 complex tax calculations.
2013-10-16 13:31:31 +00:00
if ( ! is_array ( $taxes ) && $taxes !== false && $total_cost > 0 && $this -> is_taxable () ) {
2015-12-18 13:12:20 +00:00
$taxes = 'per_item' === $args [ 'calc_tax' ] ? $this -> get_taxes_per_item ( $args [ 'cost' ] ) : WC_Tax :: calc_shipping_tax ( $total_cost , WC_Tax :: get_shipping_tax_rates () );
2015-12-14 16:56:39 +00:00
}
2016-02-05 14:58:48 +00:00
// Round the total cost after taxes have been calculated.
$total_cost = wc_format_decimal ( $total_cost , wc_get_price_decimals () );
2012-08-12 14:12:52 +00:00
2016-01-08 11:42:32 +00:00
// Create rate object
$rate = new WC_Shipping_Rate ( $args [ 'id' ], $args [ 'label' ], $total_cost , $taxes , $this -> id );
if ( ! empty ( $args [ 'meta_data' ] ) ) {
foreach ( $args [ 'meta_data' ] as $key => $value ) {
$rate -> add_meta_data ( $key , $value );
}
}
2016-03-22 17:13:39 +00:00
// Store package data
if ( $package ) {
$items_in_package = array ();
foreach ( $package [ 'contents' ] as $item ) {
$product = $item [ 'data' ];
$items_in_package [] = $product -> get_title () . ' × ' . $item [ 'quantity' ];
}
$rate -> add_meta_data ( __ ( 'Items' , 'woocommerce' ), implode ( ', ' , $items_in_package ) );
}
2016-01-08 11:42:32 +00:00
$this -> rates [ $args [ 'id' ] ] = $rate ;
2015-12-14 16:56:39 +00:00
}
2012-08-12 14:12:52 +00:00
2015-12-14 16:56:39 +00:00
/**
* Calc taxes per item being shipping in costs array .
* @ since 2.6 . 0
* @ access protected
* @ param array $costs
* @ return array of taxes
*/
protected function get_taxes_per_item ( $costs ) {
$taxes = array ();
2012-08-12 14:12:52 +00:00
2015-12-14 16:56:39 +00:00
// If we have an array of costs we can look up each items tax class and add tax accordingly
if ( is_array ( $costs ) ) {
2012-08-12 14:12:52 +00:00
2015-12-14 16:56:39 +00:00
$cart = WC () -> cart -> get_cart ();
2012-08-12 14:12:52 +00:00
2015-12-14 16:56:39 +00:00
foreach ( $costs as $cost_key => $amount ) {
if ( ! isset ( $cart [ $cost_key ] ) ) {
continue ;
}
2012-08-12 14:12:52 +00:00
2015-12-14 16:56:39 +00:00
$item_taxes = WC_Tax :: calc_shipping_tax ( $amount , WC_Tax :: get_shipping_tax_rates ( $cart [ $cost_key ][ 'data' ] -> get_tax_class () ) );
2012-08-12 14:12:52 +00:00
2015-12-14 16:56:39 +00:00
// 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 );
}
}
// Add any cost for the order - order costs are in the key 'order'
if ( isset ( $costs [ 'order' ] ) ) {
$item_taxes = WC_Tax :: calc_shipping_tax ( $costs [ 'order' ], WC_Tax :: get_shipping_tax_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 );
}
2012-05-29 12:32:54 +00:00
}
}
2012-01-04 16:24:26 +00:00
2015-12-14 16:56:39 +00:00
return $taxes ;
2012-01-03 19:07:32 +00:00
}
2012-08-12 14:12:52 +00:00
2014-08-31 05:49:58 +00:00
/**
2015-12-18 13:12:20 +00:00
* Is this method available ?
2014-08-31 05:49:58 +00:00
* @ param array $package
* @ return bool
*/
public function is_available ( $package ) {
2015-12-18 13:12:20 +00:00
$available = $this -> is_enabled ();
2012-08-12 14:12:52 +00:00
2015-12-18 13:12:20 +00:00
// Country availability (legacy, for non-zone based methods)
if ( ! $this -> instance_id ) {
$countries = is_array ( $this -> countries ) ? $this -> countries : array ();
2012-08-12 14:12:52 +00:00
2015-12-18 13:12:20 +00:00
switch ( $this -> availability ) {
case 'specific' :
case 'including' :
$available = in_array ( $package [ 'destination' ][ 'country' ], array_intersect ( $countries , array_keys ( WC () -> countries -> get_shipping_countries () ) ) );
break ;
case 'excluding' :
$available = in_array ( $package [ 'destination' ][ 'country' ], array_diff ( array_keys ( WC () -> countries -> get_shipping_countries () ), $countries ) );
break ;
default :
$available = in_array ( $package [ 'destination' ][ 'country' ], array_keys ( WC () -> countries -> get_shipping_countries () ) );
break ;
}
2014-02-13 11:13:56 +00:00
}
2012-08-12 14:12:52 +00:00
2015-12-18 13:12:20 +00:00
return apply_filters ( 'woocommerce_shipping_' . $this -> id . '_is_available' , $available , $package );
2014-08-31 05:49:58 +00:00
}
2012-06-29 23:20:07 +00:00
2014-08-31 05:49:58 +00:00
/**
2015-12-18 13:12:20 +00:00
* Get fee to add to shipping cost .
* @ param string | float $fee
* @ param float $total
2014-08-31 05:49:58 +00:00
* @ 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
}
2015-12-11 16:41:38 +00:00
2015-12-14 16:56:39 +00:00
/**
* Does this method have a settings page ?
* @ return bool
*/
public function has_settings () {
return $this -> instance_id ? $this -> supports ( 'instance-settings' ) : $this -> supports ( 'settings' );
}
2015-12-11 16:41:38 +00:00
/**
2016-03-24 17:26:40 +00:00
* Return admin options as a html string .
* @ return string
2015-12-14 16:56:39 +00:00
*/
2016-03-24 17:26:40 +00:00
public function get_admin_options_html () {
2015-12-14 16:56:39 +00:00
if ( $this -> instance_id ) {
2016-03-24 19:06:51 +00:00
$settings_html = $this -> generate_settings_html ( $this -> get_instance_form_fields (), false );
2015-12-14 16:56:39 +00:00
} else {
2016-03-24 19:06:51 +00:00
$settings_html = $this -> generate_settings_html ( $this -> get_form_fields (), false );
2016-03-24 17:26:40 +00:00
}
2016-03-24 18:31:39 +00:00
return '<table class="form-table">' . $settings_html . '</table>' ;
2016-03-24 17:26:40 +00:00
}
/**
* Output the shipping settings screen .
*/
public function admin_options () {
if ( ! $this -> instance_id ) {
2015-12-14 16:56:39 +00:00
echo '<h2>' . esc_html ( $this -> get_method_title () ) . '</h2>' ;
}
2016-03-24 17:26:40 +00:00
echo wp_kses_post ( wpautop ( $this -> get_method_description () ) );
echo $this -> get_admin_options_html ();
2015-12-14 16:56:39 +00:00
}
/**
* get_option function .
*
* Gets and option from the settings API , using defaults if necessary to prevent undefined notices .
*
* @ param string $key
* @ param mixed $empty_value
* @ return mixed The value specified for the option or a default value for the option .
*/
public function get_option ( $key , $empty_value = null ) {
2015-12-15 12:39:52 +00:00
// Instance options take priority over global options
2016-02-10 13:21:16 +00:00
if ( array_key_exists ( $key , $this -> get_instance_form_fields () ) ) {
2015-12-14 16:56:39 +00:00
return $this -> get_instance_option ( $key , $empty_value );
}
// Return global option
return parent :: get_option ( $key , $empty_value );
}
/**
2015-12-18 14:24:34 +00:00
* Gets an option from the settings API , using defaults if necessary to prevent undefined notices .
2015-12-14 16:56:39 +00:00
*
* @ param string $key
* @ param mixed $empty_value
* @ return mixed The value specified for the option or a default value for the option .
*/
public function get_instance_option ( $key , $empty_value = null ) {
if ( empty ( $this -> instance_settings ) ) {
$this -> init_instance_settings ();
}
// Get option default if unset.
if ( ! isset ( $this -> instance_settings [ $key ] ) ) {
$form_fields = $this -> get_instance_form_fields ();
$this -> instance_settings [ $key ] = $this -> get_field_default ( $form_fields [ $key ] );
}
if ( ! is_null ( $empty_value ) && '' === $this -> instance_settings [ $key ] ) {
$this -> instance_settings [ $key ] = $empty_value ;
}
return $this -> instance_settings [ $key ];
}
/**
* Get settings fields for instances of this shipping method ( within zones ) .
* Should be overridden by shipping methods to add options .
* @ since 2.6 . 0
2015-12-11 16:41:38 +00:00
* @ return array
*/
public function get_instance_form_fields () {
2015-12-16 13:52:46 +00:00
return apply_filters ( 'woocommerce_shipping_instance_form_fields_' . $this -> id , $this -> instance_form_fields );
2015-12-11 16:41:38 +00:00
}
/**
2015-12-14 16:56:39 +00:00
* Return the name of the option in the WP DB .
2015-12-11 16:41:38 +00:00
* @ since 2.6 . 0
2015-12-14 16:56:39 +00:00
* @ return string
2015-12-11 16:41:38 +00:00
*/
2015-12-14 16:56:39 +00:00
public function get_instance_option_key () {
2015-12-18 17:10:58 +00:00
return $this -> instance_id ? $this -> plugin_id . $this -> id . '_' . $this -> instance_id . '_settings' : '' ;
2015-12-11 16:41:38 +00:00
}
/**
2015-12-14 16:56:39 +00:00
* Initialise Settings for instances .
* @ since 2.6 . 0
*/
public function init_instance_settings () {
$this -> instance_settings = get_option ( $this -> get_instance_option_key (), null );
// If there are no settings defined, use defaults.
if ( ! is_array ( $this -> instance_settings ) ) {
$form_fields = $this -> get_instance_form_fields ();
$this -> instance_settings = array_merge ( array_fill_keys ( array_keys ( $form_fields ), '' ), wp_list_pluck ( $form_fields , 'default' ) );
}
}
/**
* Processes and saves options .
* If there is an error thrown , will continue to save and validate fields , but will leave the erroring field out .
2015-12-11 16:41:38 +00:00
* @ since 2.6 . 0
2016-03-24 17:26:40 +00:00
* @ param array $post_data Defaults to $_POST but can be passed in .
2015-12-11 16:41:38 +00:00
* @ return bool was anything saved ?
*/
2016-03-24 17:26:40 +00:00
public function process_admin_options ( $post_data = array () ) {
2015-12-14 16:56:39 +00:00
if ( $this -> instance_id ) {
$this -> init_instance_settings ();
2016-03-24 17:26:40 +00:00
if ( empty ( $post_data ) ) {
$post_data = $_POST ;
}
2015-12-14 16:56:39 +00:00
foreach ( $this -> get_instance_form_fields () as $key => $field ) {
2016-02-10 13:21:16 +00:00
if ( 'title' !== $this -> get_field_type ( $field ) ) {
2015-12-14 16:56:39 +00:00
try {
2016-03-24 17:26:40 +00:00
$this -> instance_settings [ $key ] = $this -> get_field_value ( $key , $field , $post_data );
2015-12-14 16:56:39 +00:00
} catch ( Exception $e ) {
$this -> add_error ( $e -> getMessage () );
}
}
}
2015-12-11 16:41:38 +00:00
2015-12-14 16:56:39 +00:00
return update_option ( $this -> get_instance_option_key (), apply_filters ( 'woocommerce_shipping_' . $this -> id . '_instance_settings_values' , $this -> instance_settings , $this ) );
2015-12-11 16:41:38 +00:00
} else {
2015-12-14 16:56:39 +00:00
return parent :: process_admin_options ();
2015-12-11 16:41:38 +00:00
}
}
2013-11-28 13:12:08 +00:00
}