2011-08-10 17:11:11 +00:00
< ? php
2013-02-20 17:14:46 +00:00
if ( ! defined ( 'ABSPATH' ) ) exit ; // Exit if accessed directly
2011-08-10 17:11:11 +00:00
/**
* Flat Rate Shipping Method
2012-08-14 22:43:48 +00:00
*
2011-08-10 17:11:11 +00:00
* A simple shipping method for a flat fee per item or per order
*
2012-12-31 18:25:09 +00:00
* @ class WC_Shipping_Flat_Rate
* @ version 2.0 . 0
2012-08-14 22:43:48 +00:00
* @ package WooCommerce / Classes / Shipping
* @ author WooThemes
*/
2012-12-31 18:25:09 +00:00
class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
2012-08-14 22:43:48 +00:00
/**
* __construct function .
*
* @ access public
* @ return void
*/
function __construct () {
2013-08-12 21:09:04 +00:00
$this -> id = 'flat_rate' ;
$this -> method_title = __ ( 'Flat Rate' , 'woocommerce' );
2012-02-06 11:16:06 +00:00
$this -> flat_rate_option = 'woocommerce_flat_rates' ;
2013-08-12 21:09:04 +00:00
$this -> method_description = __ ( 'Flat rates let you define a standard rate per item, or per order.' , 'woocommerce' );
2012-04-13 11:16:24 +00:00
2012-12-15 11:53:32 +00:00
add_action ( 'woocommerce_update_options_shipping_' . $this -> id , array ( $this , 'process_admin_options' ) );
add_action ( 'woocommerce_update_options_shipping_' . $this -> id , array ( $this , 'process_flat_rates' ) );
2013-03-13 15:43:45 +00:00
add_filter ( 'woocommerce_settings_api_sanitized_fields_' . $this -> id , array ( $this , 'save_default_costs' ) );
2012-04-13 11:16:24 +00:00
2013-08-12 21:09:04 +00:00
$this -> init ();
}
2012-08-14 22:43:48 +00:00
2013-08-12 21:09:04 +00:00
/**
* init function .
*
* @ access public
* @ return void
*/
function init () {
2012-08-14 22:43:48 +00:00
2011-11-28 15:50:19 +00:00
// Load the settings.
2013-01-02 13:38:33 +00:00
$this -> init_form_fields ();
2011-11-28 15:50:19 +00:00
$this -> init_settings ();
2012-08-14 22:43:48 +00:00
2011-11-28 15:50:19 +00:00
// Define user set variables
2012-12-31 12:07:43 +00:00
$this -> title = $this -> get_option ( 'title' );
$this -> availability = $this -> get_option ( 'availability' );
$this -> countries = $this -> get_option ( 'countries' );
$this -> type = $this -> get_option ( 'type' );
$this -> tax_status = $this -> get_option ( 'tax_status' );
$this -> cost = $this -> get_option ( 'cost' );
$this -> cost_per_order = $this -> get_option ( 'cost_per_order' );
$this -> fee = $this -> get_option ( 'fee' );
$this -> minimum_fee = $this -> get_option ( 'minimum_fee' );
$this -> options = ( array ) explode ( " \n " , $this -> get_option ( 'options' ) );
2012-08-14 22:43:48 +00:00
2012-02-06 11:16:06 +00:00
// Load Flat rates
2011-12-02 18:54:52 +00:00
$this -> get_flat_rates ();
2013-08-12 21:09:04 +00:00
}
2011-11-28 15:50:19 +00:00
2012-08-14 22:43:48 +00:00
2013-08-12 21:09:04 +00:00
/**
* Initialise Gateway Settings Form Fields
*
* @ access public
* @ return void
*/
function init_form_fields () {
2012-08-14 22:43:48 +00:00
2013-08-12 21:09:04 +00:00
$this -> form_fields = array (
2011-11-28 15:50:19 +00:00
'enabled' => array (
2012-08-14 22:43:48 +00:00
'title' => __ ( 'Enable/Disable' , 'woocommerce' ),
'type' => 'checkbox' ,
'label' => __ ( 'Enable this shipping method' , 'woocommerce' ),
2012-05-16 08:03:49 +00:00
'default' => 'no' ,
2012-08-14 22:43:48 +00:00
),
2011-11-28 15:50:19 +00:00
'title' => array (
2012-08-14 22:43:48 +00:00
'title' => __ ( 'Method Title' , 'woocommerce' ),
'type' => 'text' ,
'description' => __ ( 'This controls the title which the user sees during checkout.' , 'woocommerce' ),
2012-05-16 08:03:49 +00:00
'default' => __ ( 'Flat Rate' , 'woocommerce' ),
2013-08-12 21:09:04 +00:00
'desc_tip' => true
2011-11-28 15:50:19 +00:00
),
2011-12-02 18:54:52 +00:00
'availability' => array (
2013-03-13 15:43:45 +00:00
'title' => __ ( 'Availability' , 'woocommerce' ),
2012-08-14 22:43:48 +00:00
'type' => 'select' ,
2011-12-02 18:54:52 +00:00
'default' => 'all' ,
'class' => 'availability' ,
'options' => array (
2012-05-16 08:03:49 +00:00
'all' => __ ( 'All allowed countries' , 'woocommerce' ),
'specific' => __ ( 'Specific Countries' , 'woocommerce' ),
),
2011-12-02 18:54:52 +00:00
),
'countries' => array (
2012-08-14 22:43:48 +00:00
'title' => __ ( 'Specific Countries' , 'woocommerce' ),
'type' => 'multiselect' ,
2011-12-02 18:54:52 +00:00
'class' => 'chosen_select' ,
'css' => 'width: 450px;' ,
'default' => '' ,
2013-11-25 14:01:32 +00:00
'options' => WC () -> countries -> get_shipping_countries (),
2013-10-11 14:27:04 +00:00
'custom_attributes' => array (
2013-10-11 14:30:10 +00:00
'data-placeholder' => __ ( 'Select some countries' , 'woocommerce' )
2013-10-11 14:27:04 +00:00
)
2011-12-02 18:54:52 +00:00
),
2011-11-28 15:50:19 +00:00
'tax_status' => array (
2012-08-14 22:43:48 +00:00
'title' => __ ( 'Tax Status' , 'woocommerce' ),
'type' => 'select' ,
2011-11-28 15:50:19 +00:00
'default' => 'taxable' ,
'options' => array (
2012-05-16 08:03:49 +00:00
'taxable' => __ ( 'Taxable' , 'woocommerce' ),
2014-03-03 14:47:19 +00:00
'none' => _x ( 'None' , 'Tax status' , 'woocommerce' )
2012-05-16 08:03:49 +00:00
),
2011-11-28 15:50:19 +00:00
),
2013-03-13 15:43:45 +00:00
'cost_per_order' => array (
'title' => __ ( 'Cost per order' , 'woocommerce' ),
2013-11-12 17:43:30 +00:00
'type' => 'price' ,
'placeholder' => wc_format_localized_price ( 0 ),
2013-12-04 22:20:30 +00:00
'description' => __ ( 'Enter a cost (excluding tax) per order, e.g. 5.00. Default is 0.' , 'woocommerce' ),
2013-03-13 15:43:45 +00:00
'default' => '' ,
2013-11-12 17:43:30 +00:00
'desc_tip' => true
2012-05-16 08:03:49 +00:00
),
2013-03-13 15:43:45 +00:00
'options' => array (
'title' => __ ( 'Additional Rates' , 'woocommerce' ),
'type' => 'textarea' ,
2013-08-25 11:59:58 +00:00
'description' => __ ( 'Optional extra shipping options with additional costs (one per line): Option Name | Additional Cost [+- Percents] | Per Cost Type (order, class, or item) Example: <code>Priority Mail | 6.95 [+ 0.2%] | order</code>.' , 'woocommerce' ),
2012-05-16 08:03:49 +00:00
'default' => '' ,
2013-08-12 21:09:04 +00:00
'desc_tip' => true ,
2013-08-25 11:59:58 +00:00
'placeholder' => __ ( 'Option Name | Additional Cost [+- Percents%] | Per Cost Type (order, class, or item)' , 'woocommerce' )
2013-03-13 15:43:45 +00:00
),
'additional_costs' => array (
2013-08-12 21:09:04 +00:00
'title' => __ ( 'Additional Costs' , 'woocommerce' ),
'type' => 'title' ,
2013-03-13 15:43:45 +00:00
'description' => __ ( 'Additional costs can be added below - these will all be added to the per-order cost above.' , 'woocommerce' )
),
'type' => array (
'title' => __ ( 'Costs Added...' , 'woocommerce' ),
'type' => 'select' ,
'default' => 'order' ,
'options' => array (
'order' => __ ( 'Per Order - charge shipping for the entire order as a whole' , 'woocommerce' ),
'item' => __ ( 'Per Item - charge shipping for each item individually' , 'woocommerce' ),
'class' => __ ( 'Per Class - charge shipping for each shipping class in an order' , 'woocommerce' ),
),
),
'additional_costs_table' => array (
2013-08-12 21:09:04 +00:00
'type' => 'additional_costs_table'
2011-11-28 15:50:19 +00:00
),
2012-04-12 10:04:15 +00:00
'minimum_fee' => array (
2013-03-13 15:43:45 +00:00
'title' => __ ( 'Minimum Handling Fee' , 'woocommerce' ),
2013-11-12 17:43:30 +00:00
'type' => 'price' ,
'placeholder' => wc_format_localized_price ( 0 ),
2012-05-16 08:03:49 +00:00
'description' => __ ( 'Enter a minimum fee amount. Fee\'s less than this will be increased. Leave blank to disable.' , 'woocommerce' ),
'default' => '' ,
2013-11-12 17:43:30 +00:00
'desc_tip' => true
2012-04-12 10:04:15 +00:00
),
2011-11-28 15:50:19 +00:00
);
2012-08-14 22:43:48 +00:00
2013-08-12 21:09:04 +00:00
}
2012-08-14 22:43:48 +00:00
2013-08-12 21:09:04 +00:00
/**
* calculate_shipping function .
*
* @ access public
* @ param array $package ( default : array ())
* @ return void
*/
function calculate_shipping ( $package = array () ) {
2012-08-14 22:43:48 +00:00
2013-08-12 21:09:04 +00:00
$this -> rates = array ();
$cost_per_order = ( isset ( $this -> cost_per_order ) && ! empty ( $this -> cost_per_order ) ) ? $this -> cost_per_order : 0 ;
2012-08-14 22:43:48 +00:00
2013-08-12 21:09:04 +00:00
if ( $this -> type == 'order' ) {
2012-11-27 16:22:47 +00:00
2013-08-12 21:09:04 +00:00
$shipping_total = $this -> order_shipping ( $package );
2012-11-27 16:22:47 +00:00
2014-02-24 17:17:33 +00:00
if ( ! is_null ( $shipping_total ) || $cost_per_order > 0 ) {
2013-08-12 21:09:04 +00:00
$rate = array (
2012-11-19 14:48:40 +00:00
'id' => $this -> id ,
'label' => $this -> title ,
'cost' => $shipping_total + $cost_per_order ,
);
2014-02-24 17:17:33 +00:00
}
2012-11-27 16:22:47 +00:00
2012-04-13 11:16:24 +00:00
} elseif ( $this -> type == 'class' ) {
2012-11-27 16:22:47 +00:00
2012-04-13 11:16:24 +00:00
$shipping_total = $this -> class_shipping ( $package );
2012-08-14 22:43:48 +00:00
2014-02-24 17:17:33 +00:00
if ( ! is_null ( $shipping_total ) || $cost_per_order > 0 ) {
2013-08-12 21:09:04 +00:00
$rate = array (
2012-11-19 14:48:40 +00:00
'id' => $this -> id ,
'label' => $this -> title ,
'cost' => $shipping_total + $cost_per_order ,
);
2014-02-24 17:17:33 +00:00
}
2012-11-27 16:22:47 +00:00
2012-04-13 11:16:24 +00:00
} elseif ( $this -> type == 'item' ) {
2012-11-27 16:22:47 +00:00
2012-04-13 11:16:24 +00:00
$costs = $this -> item_shipping ( $package );
2012-11-27 16:22:47 +00:00
2012-11-19 14:48:40 +00:00
if ( ! is_null ( $costs ) || $cost_per_order > 0 ) {
2012-11-27 16:22:47 +00:00
2014-02-24 17:17:33 +00:00
if ( ! is_array ( $costs ) ) {
2012-11-19 14:48:40 +00:00
$costs = array ();
2014-02-24 17:17:33 +00:00
}
2012-11-27 16:22:47 +00:00
2012-11-19 14:48:40 +00:00
$costs [ 'order' ] = $cost_per_order ;
2012-11-27 16:22:47 +00:00
2012-11-19 14:48:40 +00:00
$rate = array (
'id' => $this -> id ,
'label' => $this -> title ,
'cost' => $costs ,
'calc_tax' => 'per_item' ,
);
2012-11-27 16:22:47 +00:00
2012-11-19 14:48:40 +00:00
}
2012-04-13 11:16:24 +00:00
}
2012-11-27 16:22:47 +00:00
2014-02-24 17:17:33 +00:00
if ( isset ( $rate ) ) {
2013-04-11 15:50:05 +00:00
$this -> add_rate ( $rate );
2014-02-24 17:17:33 +00:00
}
2012-08-14 22:43:48 +00:00
2012-02-29 11:49:39 +00:00
// Add any extra rates
2013-03-13 15:43:45 +00:00
if ( sizeof ( $this -> options ) > 0 ) {
2012-08-14 22:43:48 +00:00
2014-02-24 17:17:33 +00:00
if ( ! isset ( $rate ) ) {
2013-04-11 15:50:05 +00:00
$rate = array (
'id' => $this -> id ,
'label' => $this -> title ,
'cost' => 0 ,
);
2014-02-24 17:17:33 +00:00
}
2013-04-11 15:50:05 +00:00
2013-03-13 15:43:45 +00:00
// Get item qty
$total_quantity = 0 ;
2012-08-14 22:43:48 +00:00
2014-02-24 17:17:33 +00:00
foreach ( $package [ 'contents' ] as $item_id => $values ) {
if ( $values [ 'quantity' ] > 0 && $values [ 'data' ] -> needs_shipping () ) {
2013-03-13 15:43:45 +00:00
$total_quantity += $values [ 'quantity' ];
2014-02-24 17:17:33 +00:00
}
}
2012-08-14 22:43:48 +00:00
2013-03-13 15:43:45 +00:00
// Loop options
foreach ( $this -> options as $option ) {
2012-08-14 22:43:48 +00:00
2013-10-24 18:36:22 +00:00
$this_option = array_map ( 'trim' , explode ( WC_DELIMITER , $option ) );
2012-08-14 22:43:48 +00:00
2013-03-13 15:43:45 +00:00
if ( sizeof ( $this_option ) !== 3 ) continue ;
2012-08-14 22:43:48 +00:00
2013-03-13 15:43:45 +00:00
$extra_rate = $rate ;
2012-08-14 22:43:48 +00:00
2014-05-01 13:39:51 +00:00
$extra_rate [ 'id' ] = $this -> id . ':' . urldecode ( sanitize_title ( $this_option [ 0 ] ) );
2014-04-25 11:31:26 +00:00
$extra_rate [ 'label' ] = $this_option [ 0 ];
$this_cost = $this_option [ 1 ];
$this_cost_percents = '' ;
2012-08-14 22:43:48 +00:00
2014-03-25 13:59:30 +00:00
$pattern =
2014-02-24 17:40:26 +00:00
'/' . // start regex
'(\d+\.?\d*)' . // capture digits, optionally capture a `.` and more digits
'\s*' . // match whitespace
'(\+|-)' . // capture the operand
'\s*' . // match whitespace
'(\d+\.?\d*)' . // capture digits, optionally capture a `.` and more digits
'\%/' ; // match the percent sign & end regex
if ( preg_match ( $pattern , $this_cost , $this_cost_matches ) ) {
2013-08-25 11:59:58 +00:00
$this_cost_mathop = $this_cost_matches [ 2 ];
$this_cost_percents = $this_cost_matches [ 3 ] / 100 ;
$this_cost = $this_cost_matches [ 1 ];
unset ( $this_cost_matches );
}
2013-03-13 15:43:45 +00:00
// Backwards compat with yes and no
if ( $this_option [ 2 ] == 'yes' ) {
$this_type = 'order' ;
} elseif ( $this_option [ 2 ] == 'no' ) {
$this_type = $this -> type ;
} else {
$this_type = $this_option [ 2 ];
2012-02-29 11:49:39 +00:00
}
2013-03-13 15:43:45 +00:00
switch ( $this_type ) {
case 'class' :
2013-04-16 12:01:55 +00:00
$this_cost = $this_cost * sizeof ( $this -> find_shipping_classes ( $package ) );
2013-09-24 11:24:26 +00:00
2013-08-25 11:59:58 +00:00
// Factor $this_cost by the percentage if provided.
if ( $this_cost_percents ) {
foreach ( $this -> find_shipping_classes ( $package ) as $shipping_class => $items ){
foreach ( $items as $item_id => $values ) {
2014-02-24 18:54:56 +00:00
$this_cost = $this -> calc_percentage_adjustment ( $this_cost , $this_cost_percents , $this_cost_mathop , $values [ 'line_total' ] );
2013-08-25 11:59:58 +00:00
}
}
}
2013-03-13 15:43:45 +00:00
break ;
case 'item' :
$this_cost = $this_cost * $total_quantity ;
2013-09-24 11:24:26 +00:00
2013-08-25 11:59:58 +00:00
// Factor $this_cost by the percentage if provided.
if ( $this_cost_percents ) {
foreach ( $package [ 'contents' ] as $item_id => $values ) {
2014-02-24 18:54:56 +00:00
$this_cost = $this -> calc_percentage_adjustment ( $this_cost , $this_cost_percents , $this_cost_mathop , $values [ 'line_total' ] );
2013-08-25 11:59:58 +00:00
}
}
break ;
case 'order' :
// Factor $this_cost by the percentage if provided.
if ( $this_cost_percents ) {
2014-02-24 18:54:56 +00:00
$this_cost = $this -> calc_percentage_adjustment ( $this_cost , $this_cost_percents , $this_cost_mathop , $package [ 'contents_cost' ] );
2013-08-25 11:59:58 +00:00
}
2013-03-13 15:43:45 +00:00
break ;
2012-02-29 11:49:39 +00:00
}
2012-08-14 22:43:48 +00:00
2013-03-13 15:43:45 +00:00
// Per item rates
if ( is_array ( $extra_rate [ 'cost' ] ) ) $extra_rate [ 'cost' ][ 'order' ] = $extra_rate [ 'cost' ][ 'order' ] + $this_cost ;
// Per order or class rates
else $extra_rate [ 'cost' ] = $extra_rate [ 'cost' ] + $this_cost ;
2012-08-14 22:43:48 +00:00
2013-03-13 15:43:45 +00:00
$this -> add_rate ( $extra_rate );
}
2012-02-29 11:49:39 +00:00
}
2013-08-12 21:09:04 +00:00
}
2012-08-14 22:43:48 +00:00
2014-02-24 18:54:56 +00:00
/**
* Calculate the percentage adjustment for each shipping rate .
*
* @ access public
* @ param float $cost
* @ param float $percent_adjustment
* @ param string $percent_operator
* @ param float $base_price
* @ return float
*/
function calc_percentage_adjustment ( $cost , $percent_adjustment , $percent_operator , $base_price ) {
if ( '+' == $percent_operator ) {
$cost += $percent_adjustment * $base_price ;
} else {
$cost -= $percent_adjustment * $base_price ;
}
return $cost ;
}
2013-08-12 21:09:04 +00:00
/**
* order_shipping function .
*
* @ access public
* @ param array $package
* @ return float
*/
function order_shipping ( $package ) {
$cost = null ;
$fee = null ;
2012-08-14 22:43:48 +00:00
2012-04-13 11:16:24 +00:00
if ( sizeof ( $this -> flat_rates ) > 0 ) {
2012-08-14 22:43:48 +00:00
2013-08-12 21:09:04 +00:00
$found_shipping_classes = $this -> find_shipping_classes ( $package );
// Find most expensive class (if found)
foreach ( $found_shipping_classes as $shipping_class => $products ) {
if ( isset ( $this -> flat_rates [ $shipping_class ] ) ) {
if ( $this -> flat_rates [ $shipping_class ][ 'cost' ] > $cost ) {
$cost = $this -> flat_rates [ $shipping_class ][ 'cost' ];
$fee = $this -> flat_rates [ $shipping_class ][ 'fee' ];
}
} else {
// No matching classes so use defaults
if ( ! empty ( $this -> cost ) && $this -> cost > $cost ) {
$cost = $this -> cost ;
$fee = $this -> fee ;
}
}
}
2012-04-13 11:16:24 +00:00
}
2012-08-14 22:43:48 +00:00
2012-11-19 14:48:40 +00:00
// Default rates if set
if ( is_null ( $cost ) && $this -> cost !== '' ) {
$cost = $this -> cost ;
$fee = $this -> fee ;
} elseif ( is_null ( $cost ) ) {
2013-08-14 15:42:43 +00:00
// Set rates to 0 if nothing is set by the user
$cost = 0 ;
$fee = 0 ;
2012-11-27 16:22:47 +00:00
}
2012-08-14 22:43:48 +00:00
2012-04-13 11:16:24 +00:00
// Shipping for whole order
return $cost + $this -> get_fee ( $fee , $package [ 'contents_cost' ] );
2013-08-12 21:09:04 +00:00
}
2012-08-14 22:43:48 +00:00
2013-08-12 21:09:04 +00:00
/**
* class_shipping function .
*
* @ access public
* @ param array $package
* @ return float
*/
function class_shipping ( $package ) {
2012-04-13 11:16:24 +00:00
$cost = null ;
2013-08-12 21:09:04 +00:00
$fee = null ;
2013-11-14 01:53:31 +00:00
$matched = false ;
2012-08-14 22:43:48 +00:00
2013-04-16 12:01:55 +00:00
if ( sizeof ( $this -> flat_rates ) > 0 || $this -> cost !== '' ) {
2013-08-12 21:09:04 +00:00
// Find shipping classes for products in the cart.
$found_shipping_classes = $this -> find_shipping_classes ( $package );
// Store prices too, so we can calc a fee for the class.
$found_shipping_classes_values = array ();
foreach ( $found_shipping_classes as $shipping_class => $products ) {
2014-02-24 17:17:33 +00:00
if ( ! isset ( $found_shipping_classes_values [ $shipping_class ] ) ) {
2013-08-12 21:09:04 +00:00
$found_shipping_classes_values [ $shipping_class ] = 0 ;
2014-02-24 17:17:33 +00:00
}
2013-08-12 21:09:04 +00:00
foreach ( $products as $product ) {
$found_shipping_classes_values [ $shipping_class ] += $product [ 'data' ] -> get_price () * $product [ 'quantity' ];
}
}
// For each found class, add up the costs and fees
foreach ( $found_shipping_classes_values as $shipping_class => $class_price ) {
if ( isset ( $this -> flat_rates [ $shipping_class ] ) ) {
$cost += $this -> flat_rates [ $shipping_class ][ 'cost' ];
$fee += $this -> get_fee ( $this -> flat_rates [ $shipping_class ][ 'fee' ], $class_price );
$matched = true ;
} elseif ( $this -> cost !== '' ) {
// Class not set so we use default rate if its set
$cost += $this -> cost ;
$fee += $this -> get_fee ( $this -> fee , $class_price );
$matched = true ;
}
}
2012-04-13 11:16:24 +00:00
}
2012-08-14 22:43:48 +00:00
2012-04-13 11:16:24 +00:00
// Total
2014-02-24 17:17:33 +00:00
if ( $matched ) {
2012-11-19 14:48:40 +00:00
return $cost + $fee ;
2014-02-24 17:17:33 +00:00
} else {
2012-11-19 14:48:40 +00:00
return null ;
2014-02-24 17:17:33 +00:00
}
2013-08-12 21:09:04 +00:00
}
2012-08-14 22:43:48 +00:00
2013-08-12 21:09:04 +00:00
/**
* item_shipping function .
*
* @ access public
* @ param array $package
* @ return array
*/
function item_shipping ( $package ) {
2012-04-13 11:16:24 +00:00
// Per item shipping so we pass an array of costs (per item) instead of a single value
$costs = array ();
2012-11-27 16:22:47 +00:00
2012-11-19 14:48:40 +00:00
$matched = false ;
2012-08-14 22:43:48 +00:00
2012-04-13 11:16:24 +00:00
// Shipping per item
foreach ( $package [ 'contents' ] as $item_id => $values ) {
$_product = $values [ 'data' ];
2012-08-14 22:43:48 +00:00
2012-04-13 11:16:24 +00:00
if ( $values [ 'quantity' ] > 0 && $_product -> needs_shipping () ) {
$shipping_class = $_product -> get_shipping_class ();
2012-11-27 16:22:47 +00:00
2012-11-19 14:48:40 +00:00
$fee = $cost = 0 ;
2012-11-27 16:22:47 +00:00
2012-05-16 08:03:49 +00:00
if ( isset ( $this -> flat_rates [ $shipping_class ] ) ) {
$cost = $this -> flat_rates [ $shipping_class ][ 'cost' ];
2013-08-12 21:09:04 +00:00
$fee = $this -> get_fee ( $this -> flat_rates [ $shipping_class ][ 'fee' ], $_product -> get_price () );
$matched = true ;
2012-11-19 14:48:40 +00:00
} elseif ( $this -> cost !== '' ) {
2012-04-13 11:16:24 +00:00
$cost = $this -> cost ;
$fee = $this -> get_fee ( $this -> fee , $_product -> get_price () );
2012-11-19 14:48:40 +00:00
$matched = true ;
2012-04-13 11:16:24 +00:00
}
2012-08-14 22:43:48 +00:00
2012-05-16 08:03:49 +00:00
$costs [ $item_id ] = ( ( $cost + $fee ) * $values [ 'quantity' ] );
2012-04-13 11:16:24 +00:00
}
}
2012-11-27 16:22:47 +00:00
2014-02-24 17:17:33 +00:00
if ( $matched ) {
2012-11-19 14:48:40 +00:00
return $costs ;
2014-02-24 17:17:33 +00:00
} else {
2012-11-19 14:48:40 +00:00
return null ;
2014-02-24 17:17:33 +00:00
}
2013-08-12 21:09:04 +00:00
}
/**
* Finds and returns shipping classes and the products with said class .
*
* @ access public
* @ param mixed $package
* @ return array
*/
public function find_shipping_classes ( $package ) {
$found_shipping_classes = array ();
2013-04-16 12:01:55 +00:00
// Find shipping classes for products in the cart
if ( sizeof ( $package [ 'contents' ] ) > 0 ) {
foreach ( $package [ 'contents' ] as $item_id => $values ) {
if ( $values [ 'data' ] -> needs_shipping () ) {
$found_class = $values [ 'data' ] -> get_shipping_class ();
2014-02-24 17:17:33 +00:00
if ( ! isset ( $found_shipping_classes [ $found_class ] ) ) {
2013-04-16 12:01:55 +00:00
$found_shipping_classes [ $found_class ] = array ();
2014-02-24 17:17:33 +00:00
}
2013-04-16 12:01:55 +00:00
$found_shipping_classes [ $found_class ][ $item_id ] = $values ;
}
}
}
return $found_shipping_classes ;
2013-08-12 21:09:04 +00:00
}
/**
* validate_additional_costs_field function .
*
* @ access public
* @ param mixed $key
2013-11-28 12:54:19 +00:00
* @ return bool
2013-08-12 21:09:04 +00:00
*/
function validate_additional_costs_table_field ( $key ) {
return false ;
}
/**
* generate_additional_costs_html function .
*
* @ access public
2013-11-28 12:54:19 +00:00
* @ return string
2013-08-12 21:09:04 +00:00
*/
function generate_additional_costs_table_html () {
ob_start ();
?>
< tr valign = " top " >
< th scope = " row " class = " titledesc " >< ? php _e ( 'Costs' , 'woocommerce' ); ?> :</th>
< td class = " forminp " id = " <?php echo $this->id ; ?>_flat_rates " >
< table class = " shippingrows widefat " cellspacing = " 0 " >
< thead >
< tr >
< th class = " check-column " >< input type = " checkbox " ></ th >
< th class = " shipping_class " >< ? php _e ( 'Shipping Class' , 'woocommerce' ); ?> </th>
< th >< ? php _e ( 'Cost' , 'woocommerce' ); ?> <a class="tips" data-tip="<?php _e( 'Cost, excluding tax.', 'woocommerce' ); ?>">[?]</a></th>
< th >< ? php _e ( 'Handling Fee' , 'woocommerce' ); ?> <a class="tips" data-tip="<?php _e( 'Fee excluding tax. Enter an amount, e.g. 2.50, or a percentage, e.g. 5%.', 'woocommerce' ); ?>">[?]</a></th>
</ tr >
</ thead >
< tfoot >
< tr >
2014-03-25 13:59:30 +00:00
< th colspan = " 4 " >< a href = " # " class = " add button " >< ? php _e ( 'Add Cost' , 'woocommerce' ); ?> </a> <a href="#" class="remove button"><?php _e( 'Delete selected costs', 'woocommerce' ); ?></a></th>
2013-08-12 21:09:04 +00:00
</ tr >
</ tfoot >
< tbody class = " flat_rates " >
< tr >
< td ></ td >
< td class = " flat_rate_class " >< ? php _e ( 'Any class' , 'woocommerce' ); ?> </td>
2013-11-12 17:43:30 +00:00
< td >< input type = " text " value = " <?php echo esc_attr( wc_format_localized_price( $this->cost ) ); ?> " name = " default_cost " placeholder = " <?php _e( 'N/A', 'woocommerce' ); ?> " size = " 4 " class = " wc_input_price " /></ td >
< td >< input type = " text " value = " <?php echo esc_attr( wc_format_localized_price( $this->fee ) ); ?> " name = " default_fee " placeholder = " <?php _e( 'N/A', 'woocommerce' ); ?> " size = " 4 " class = " wc_input_price " /></ td >
2013-08-12 21:09:04 +00:00
</ tr >
< ? php
$i = - 1 ;
if ( $this -> flat_rates ) {
foreach ( $this -> flat_rates as $class => $rate ) {
$i ++ ;
echo ' < tr class = " flat_rate " >
< th class = " check-column " >< input type = " checkbox " name = " select " /></ th >
< td class = " flat_rate_class " >
< select name = " ' . esc_attr( $this->id . '_class[' . $i . ']' ) . ' " class = " select " > ' ;
2013-11-25 14:01:32 +00:00
if ( WC () -> shipping -> get_shipping_classes () ) {
foreach ( WC () -> shipping -> get_shipping_classes () as $shipping_class ) {
2013-11-20 19:11:59 +00:00
echo '<option value="' . esc_attr ( $shipping_class -> slug ) . '" ' . selected ( $shipping_class -> slug , $class , false ) . '>' . $shipping_class -> name . '</option>' ;
2013-08-12 21:09:04 +00:00
}
} else {
echo '<option value="">' . __ ( 'Select a class…' , 'woocommerce' ) . '</option>' ;
}
echo ' </ select >
</ td >
2013-11-12 17:43:30 +00:00
< td >< input type = " text " value = " ' . esc_attr( $rate['cost'] ) . ' " name = " ' . esc_attr( $this->id .'_cost[' . $i . ']' ) . ' " placeholder = " ' . wc_format_localized_price( 0 ) . ' " size = " 4 " class = " wc_input_price " /></ td >
< td >< input type = " text " value = " ' . esc_attr( $rate['fee'] ) . ' " name = " ' . esc_attr( $this->id .'_fee[' . $i . ']' ) . ' " placeholder = " ' . wc_format_localized_price( 0 ) . ' " size = " 4 " class = " wc_input_price " /></ td >
2013-08-12 21:09:04 +00:00
</ tr > ' ;
}
}
?>
</ tbody >
</ table >
< script type = " text/javascript " >
2013-03-13 15:43:45 +00:00
jQuery ( function () {
jQuery ( '#<?php echo $this->id; ?>_flat_rates' ) . on ( 'click' , 'a.add' , function (){
var size = jQuery ( '#<?php echo $this->id; ?>_flat_rates tbody .flat_rate' ) . size ();
jQuery ( ' < tr class = " flat_rate " > \
< th class = " check-column " >< input type = " checkbox " name = " select " /></ th > \
2013-08-12 21:09:04 +00:00
< td class = " flat_rate_class " > \
< select name = " <?php echo $this->id ; ?>_class[' + size + '] " class = " select " > \
< ? php
2013-11-25 14:01:32 +00:00
if ( WC () -> shipping -> get_shipping_classes ()) :
foreach ( WC () -> shipping -> get_shipping_classes () as $class ) :
2013-08-12 21:09:04 +00:00
echo '<option value="' . esc_attr ( $class -> slug ) . '">' . esc_js ( $class -> name ) . '</option>' ;
endforeach ;
else :
echo '<option value="">' . __ ( 'Select a class…' , 'woocommerce' ) . '</option>' ;
endif ;
?> \
</ select > \
</ td > \
2013-11-12 17:43:30 +00:00
< td >< input type = " text " name = " <?php echo $this->id ; ?>_cost[' + size + '] " placeholder = " <?php echo wc_format_localized_price( 0 ); ?> " size = " 4 " class = " wc_input_price " /></ td > \
< td >< input type = " text " name = " <?php echo $this->id ; ?>_fee[' + size + '] " placeholder = " <?php echo wc_format_localized_price( 0 ); ?> " size = " 4 " class = " wc_input_price " /></ td > \
2013-08-12 21:09:04 +00:00
</ tr > ').appendTo(' #<?php echo $this->id; ?>_flat_rates table tbody');
2013-03-13 15:43:45 +00:00
return false ;
2011-12-02 18:54:52 +00:00
});
2012-08-14 22:43:48 +00:00
2013-03-13 15:43:45 +00:00
// Remove row
jQuery ( '#<?php echo $this->id; ?>_flat_rates' ) . on ( 'click' , 'a.remove' , function (){
2013-09-04 13:32:16 +00:00
var answer = confirm ( " <?php _e( 'Delete the selected rates?', 'woocommerce' ); ?> " );
2013-03-13 15:43:45 +00:00
if ( answer ) {
2013-03-25 14:46:08 +00:00
jQuery ( '#<?php echo $this->id; ?>_flat_rates table tbody tr th.check-column input:checked' ) . each ( function ( i , el ){
2013-03-13 15:43:45 +00:00
jQuery ( el ) . closest ( 'tr' ) . remove ();
});
}
return false ;
});
2012-08-14 22:43:48 +00:00
2013-03-13 15:43:45 +00:00
});
</ script >
2013-08-12 21:09:04 +00:00
</ td >
</ tr >
< ? php
return ob_get_clean ();
}
/**
* process_flat_rates function .
*
* @ access public
* @ return void
*/
function process_flat_rates () {
2011-12-02 18:54:52 +00:00
// Save the rates
$flat_rate_class = array ();
$flat_rate_cost = array ();
$flat_rate_fee = array ();
$flat_rates = array ();
2012-08-14 22:43:48 +00:00
2013-11-25 13:34:21 +00:00
if ( isset ( $_POST [ $this -> id . '_class' ] ) ) $flat_rate_class = array_map ( 'wc_clean' , $_POST [ $this -> id . '_class' ] );
2013-10-01 16:54:29 +00:00
if ( isset ( $_POST [ $this -> id . '_cost' ] ) ) $flat_rate_cost = array_map ( 'stripslashes' , $_POST [ $this -> id . '_cost' ] );
if ( isset ( $_POST [ $this -> id . '_fee' ] ) ) $flat_rate_fee = array_map ( 'stripslashes' , $_POST [ $this -> id . '_fee' ] );
2012-08-14 22:43:48 +00:00
2011-12-02 18:54:52 +00:00
// Get max key
$values = $flat_rate_class ;
2012-08-14 22:43:48 +00:00
ksort ( $values );
$value = end ( $values );
2012-05-16 08:03:49 +00:00
$key = key ( $values );
2012-08-14 22:43:48 +00:00
2012-05-16 08:03:49 +00:00
for ( $i = 0 ; $i <= $key ; $i ++ ) {
2013-07-19 06:08:13 +00:00
if ( ! empty ( $flat_rate_class [ $i ] ) && isset ( $flat_rate_cost [ $i ] ) && isset ( $flat_rate_fee [ $i ] ) ) {
2012-08-14 22:43:48 +00:00
2013-11-25 13:34:21 +00:00
$flat_rate_cost [ $i ] = wc_format_decimal ( $flat_rate_cost [ $i ] );
2013-10-01 16:54:29 +00:00
2014-02-24 17:17:33 +00:00
if ( ! strstr ( $flat_rate_fee [ $i ], '%' ) ) {
2013-11-25 13:34:21 +00:00
$flat_rate_fee [ $i ] = wc_format_decimal ( $flat_rate_fee [ $i ] );
2014-02-24 17:17:33 +00:00
} else {
2013-11-25 13:34:21 +00:00
$flat_rate_fee [ $i ] = wc_clean ( $flat_rate_fee [ $i ] );
2014-02-24 17:17:33 +00:00
}
2012-08-14 22:43:48 +00:00
2011-12-02 18:54:52 +00:00
// Add to flat rates array
2014-05-01 13:39:51 +00:00
$flat_rates [ urldecode ( sanitize_title ( $flat_rate_class [ $i ] ) ) ] = array (
2012-05-16 08:03:49 +00:00
'cost' => $flat_rate_cost [ $i ],
'fee' => $flat_rate_fee [ $i ],
);
}
}
2012-08-14 22:43:48 +00:00
2012-05-16 08:03:49 +00:00
update_option ( $this -> flat_rate_option , $flat_rates );
2012-08-14 22:43:48 +00:00
2011-12-02 18:54:52 +00:00
$this -> get_flat_rates ();
2013-08-12 21:09:04 +00:00
}
/**
* save_default_costs function .
*
* @ access public
2013-11-28 12:54:19 +00:00
* @ param array $fields
* @ return array
2013-08-12 21:09:04 +00:00
*/
function save_default_costs ( $fields ) {
2013-11-25 13:34:21 +00:00
$default_cost = ( $_POST [ 'default_cost' ] === '' ) ? '' : wc_format_decimal ( $_POST [ 'default_cost' ] );
2013-10-02 11:50:03 +00:00
2014-02-24 17:17:33 +00:00
if ( ! strstr ( $_POST [ 'default_fee' ], '%' ) ) {
2013-11-25 13:34:21 +00:00
$default_fee = ( $_POST [ 'default_fee' ] === '' ) ? '' : wc_format_decimal ( $_POST [ 'default_fee' ] );
2014-02-24 17:17:33 +00:00
} else {
2013-11-25 13:34:21 +00:00
$default_fee = wc_clean ( $_POST [ 'default_fee' ] );
2014-02-24 17:17:33 +00:00
}
2013-03-13 15:43:45 +00:00
$fields [ 'cost' ] = $default_cost ;
$fields [ 'fee' ] = $default_fee ;
return $fields ;
2013-08-12 21:09:04 +00:00
}
/**
* get_flat_rates function .
*
* @ access public
* @ return void
*/
function get_flat_rates () {
$this -> flat_rates = array_filter ( ( array ) get_option ( $this -> flat_rate_option ) );
}
2011-08-10 17:11:11 +00:00
2013-08-25 11:59:58 +00:00
}