2011-08-10 17:11:11 +00:00
< ? php
/**
* 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-01-27 16:38:39 +00:00
* @ class WC_Flat_Rate
2012-08-14 22:43:48 +00:00
* @ version 1.6 . 4
* @ package WooCommerce / Classes / Shipping
* @ author WooThemes
*/
2012-10-15 10:57:58 +00:00
if ( ! defined ( 'ABSPATH' ) ) exit ; // Exit if accessed directly
2012-01-27 16:38:39 +00:00
class WC_Flat_Rate extends WC_Shipping_Method {
2012-08-14 22:43:48 +00:00
/**
* __construct function .
*
* @ access public
* @ return void
*/
function __construct () {
2012-02-06 11:16:06 +00:00
$this -> id = 'flat_rate' ;
2012-05-16 08:03:49 +00:00
$this -> method_title = __ ( 'Flat rate' , 'woocommerce' );
2011-11-28 15:50:19 +00:00
2012-02-06 11:16:06 +00:00
$this -> flat_rate_option = 'woocommerce_flat_rates' ;
2012-05-16 08:03:49 +00:00
$this -> admin_page_heading = __ ( 'Flat Rates' , 'woocommerce' );
$this -> admin_page_description = __ ( 'Flat rates let you define a standard rate per item, or per order.' , 'woocommerce' );
2012-04-13 11:16:24 +00:00
2012-05-16 08:03:49 +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' ) );
2012-04-13 11:16:24 +00:00
2012-02-06 11:16:06 +00:00
$this -> init ();
2012-08-14 22:43:48 +00:00
}
/**
* init function .
*
* @ access public
* @ return void
*/
2012-02-06 11:16:06 +00:00
function init () {
// Load the form fields.
2011-11-28 15:50:19 +00:00
$this -> init_form_fields ();
2012-08-14 22:43:48 +00:00
2011-11-28 15:50:19 +00:00
// Load the settings.
$this -> init_settings ();
2012-08-14 22:43:48 +00:00
2011-11-28 15:50:19 +00:00
// Define user set variables
2012-05-16 10:00:18 +00:00
$this -> enabled = $this -> settings [ 'enabled' ];
$this -> title = $this -> settings [ 'title' ];
$this -> availability = $this -> settings [ 'availability' ];
$this -> countries = $this -> settings [ 'countries' ];
$this -> type = $this -> settings [ 'type' ];
$this -> tax_status = $this -> settings [ 'tax_status' ];
$this -> cost = $this -> settings [ 'cost' ];
$this -> cost_per_order = isset ( $this -> settings [ 'cost_per_order' ] ) ? $this -> settings [ 'cost_per_order' ] : '' ;
2012-08-14 22:43:48 +00:00
$this -> fee = $this -> settings [ 'fee' ];
$this -> minimum_fee = isset ( $this -> settings [ 'minimum_fee' ] ) ? $this -> settings [ 'minimum_fee' ] : '' ;
$this -> options = isset ( $this -> settings [ 'options' ] ) ? $this -> settings [ 'options' ] : '' ;
2012-02-29 11:49:39 +00:00
// Get options
2012-05-16 10:00:18 +00:00
$this -> options = ( array ) explode ( " \n " , $this -> 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 ();
2012-02-06 11:16:06 +00:00
}
2011-11-28 15:50:19 +00:00
2012-08-14 22:43:48 +00:00
/**
2011-11-28 15:50:19 +00:00
* Initialise Gateway Settings Form Fields
2012-08-14 22:43:48 +00:00
*
* @ access public
* @ return void
2011-11-28 15:50:19 +00:00
*/
function init_form_fields () {
global $woocommerce ;
2012-08-14 22:43:48 +00:00
2011-11-28 15:50:19 +00:00
$this -> form_fields = array (
'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' ),
2011-11-28 15:50:19 +00:00
),
2012-05-16 10:00:18 +00:00
'cost_per_order' => array (
2012-08-14 22:43:48 +00:00
'title' => __ ( 'Cost per order' , 'woocommerce' ),
2012-11-20 17:32:50 +00:00
'type' => 'number' ,
'custom_attributes' => array (
'step' => 'any' ,
'min' => '0'
),
2012-05-16 10:00:18 +00:00
'description' => __ ( 'Enter a cost per order, e.g. 5.00. Leave blank to disable.' , 'woocommerce' ),
'default' => '' ,
),
2011-12-02 18:54:52 +00:00
'availability' => array (
2012-08-14 22:43:48 +00:00
'title' => __ ( 'Method availability' , 'woocommerce' ),
'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' => '' ,
2012-05-16 08:03:49 +00:00
'options' => $woocommerce -> countries -> countries ,
2011-12-02 18:54:52 +00:00
),
2011-11-28 15:50:19 +00:00
'type' => array (
2012-08-14 22:43:48 +00:00
'title' => __ ( 'Calculation Type' , 'woocommerce' ),
'type' => 'select' ,
'description' => '' ,
2011-11-28 15:50:19 +00:00
'default' => 'order' ,
'options' => array (
2012-05-16 08:03:49 +00:00
'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' ),
),
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' ,
'description' => '' ,
2011-11-28 15:50:19 +00:00
'default' => 'taxable' ,
'options' => array (
2012-05-16 08:03:49 +00:00
'taxable' => __ ( 'Taxable' , 'woocommerce' ),
'none' => __ ( 'None' , 'woocommerce' ),
),
2011-11-28 15:50:19 +00:00
),
'cost' => array (
2012-08-14 22:43:48 +00:00
'title' => __ ( 'Default Cost' , 'woocommerce' ),
2012-11-20 17:32:50 +00:00
'type' => 'number' ,
'custom_attributes' => array (
'step' => 'any' ,
'min' => '0'
),
2012-05-16 08:03:49 +00:00
'description' => __ ( 'Cost excluding tax. Enter an amount, e.g. 2.50.' , 'woocommerce' ),
'default' => '' ,
),
2011-11-28 15:50:19 +00:00
'fee' => array (
2012-08-14 22:43:48 +00:00
'title' => __ ( 'Default Handling Fee' , 'woocommerce' ),
'type' => 'text' ,
2012-05-16 08:03:49 +00:00
'description' => __ ( 'Fee excluding tax. Enter an amount, e.g. 2.50, or a percentage, e.g. 5%. Leave blank to disable.' , 'woocommerce' ),
'default' => '' ,
2011-11-28 15:50:19 +00:00
),
2012-04-12 10:04:15 +00:00
'minimum_fee' => array (
2012-08-14 22:43:48 +00:00
'title' => __ ( 'Minimum Fee' , 'woocommerce' ),
2012-11-20 17:32:50 +00:00
'type' => 'number' ,
'custom_attributes' => array (
'step' => 'any' ,
'min' => '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' => '' ,
2012-04-12 10:04:15 +00:00
),
2012-02-29 11:49:39 +00:00
'options' => array (
2012-08-14 22:43:48 +00:00
'title' => __ ( 'Shipping Options' , 'woocommerce' ),
'type' => 'textarea' ,
2012-05-16 08:03:49 +00:00
'description' => __ ( 'Optional extra shipping options with additional costs (one per line). Example: <code>Option Name|Cost|Per-order (yes or no)</code>. Example: <code>Priority Mail|6.95|yes</code>. If per-order is set to no, it will use the "Calculation Type" setting.' , 'woocommerce' ),
'default' => '' ,
2012-02-29 11:49:39 +00:00
),
2011-11-28 15:50:19 +00:00
);
2012-08-14 22:43:48 +00:00
}
/**
* calculate_shipping function .
*
* @ access public
* @ param array $package ( default : array ())
* @ return void
*/
2012-04-13 11:16:24 +00:00
function calculate_shipping ( $package = array () ) {
2011-09-06 11:11:22 +00:00
global $woocommerce ;
2012-08-14 22:43:48 +00:00
2012-11-19 14:48:40 +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
2012-04-13 11:16:24 +00:00
if ( $this -> type == 'order' ) {
2012-11-19 14:48:40 +00:00
2012-04-13 11:16:24 +00:00
$shipping_total = $this -> order_shipping ( $package );
2012-11-19 14:48:40 +00:00
if ( ! is_null ( $shipping_total ) || $cost_per_order > 0 )
$rate = array (
'id' => $this -> id ,
'label' => $this -> title ,
'cost' => $shipping_total + $cost_per_order ,
);
2012-04-13 11:16:24 +00:00
} elseif ( $this -> type == 'class' ) {
2012-11-19 14:48:40 +00:00
2012-04-13 11:16:24 +00:00
$shipping_total = $this -> class_shipping ( $package );
2012-08-14 22:43:48 +00:00
2012-11-19 14:48:40 +00:00
if ( ! is_null ( $shipping_total ) || $cost_per_order > 0 )
$rate = array (
'id' => $this -> id ,
'label' => $this -> title ,
'cost' => $shipping_total + $cost_per_order ,
);
2012-04-13 11:16:24 +00:00
} elseif ( $this -> type == 'item' ) {
2012-11-19 14:48:40 +00:00
2012-04-13 11:16:24 +00:00
$costs = $this -> item_shipping ( $package );
2012-11-19 14:48:40 +00:00
if ( ! is_null ( $costs ) || $cost_per_order > 0 ) {
if ( ! is_array ( $costs ) )
$costs = array ();
$costs [ 'order' ] = $cost_per_order ;
$rate = array (
'id' => $this -> id ,
'label' => $this -> title ,
'cost' => $costs ,
'calc_tax' => 'per_item' ,
);
}
2012-04-13 11:16:24 +00:00
}
2012-11-19 14:48:40 +00:00
if ( ! isset ( $rate ) )
return ;
2012-02-29 11:49:39 +00:00
2012-01-03 19:07:32 +00:00
// Register the rate
2012-08-14 22:43:48 +00:00
$this -> add_rate ( $rate );
2012-02-29 11:49:39 +00:00
// Add any extra rates
2012-04-13 11:16:24 +00:00
if ( sizeof ( $this -> options ) > 0 ) foreach ( $this -> options as $option ) {
2012-08-14 22:43:48 +00:00
2012-04-18 06:03:28 +00:00
$this_option = preg_split ( '~\s*\|\s*~' , trim ( $option ) );
2012-08-14 22:43:48 +00:00
2012-05-16 10:00:18 +00:00
if ( sizeof ( $this_option ) !== 3 ) continue ;
2012-08-14 22:43:48 +00:00
2012-02-29 11:49:39 +00:00
$extra_rate = $rate ;
2012-08-14 22:43:48 +00:00
2012-05-16 10:00:18 +00:00
$extra_rate [ 'id' ] = $this -> id . ':' . sanitize_title ( $this_option [ 0 ]);
$extra_rate [ 'label' ] = $this_option [ 0 ];
2012-08-14 22:43:48 +00:00
2012-05-16 08:03:49 +00:00
$per_order_cost = ( $this_option [ 2 ] == 'yes' ) ? 1 : 0 ;
2012-02-29 11:49:39 +00:00
$this_cost = $this_option [ 1 ];
2012-08-14 22:43:48 +00:00
2012-05-16 08:03:49 +00:00
if ( is_array ( $extra_rate [ 'cost' ] ) ) {
if ( $per_order_cost ) {
2012-02-29 11:49:39 +00:00
$extra_rate [ 'cost' ][ 'order' ] = $this_cost ;
} else {
2012-04-23 14:32:21 +00:00
$total_quantity = 0 ;
2012-08-14 22:43:48 +00:00
2012-04-23 14:32:21 +00:00
// Shipping per item
foreach ( $package [ 'contents' ] as $item_id => $values )
if ( $values [ 'quantity' ] > 0 && $values [ 'data' ] -> needs_shipping () )
$total_quantity += $values [ 'quantity' ];
2012-08-14 22:43:48 +00:00
2012-02-29 11:49:39 +00:00
// Per-product shipping
$extra_rate [ 'cost' ][ 'order' ] = $this_cost * $total_quantity ;
}
} else {
// If using shipping per class, multiple the cost by the classes we found
2012-05-16 08:03:49 +00:00
if ( ! $per_order_cost && $this -> type == 'class' ) {
2012-02-29 11:49:39 +00:00
$this_cost = $this_cost * $found_shipping_classes ;
}
2012-08-14 22:43:48 +00:00
2012-02-29 11:49:39 +00:00
$extra_rate [ 'cost' ] = $extra_rate [ 'cost' ] + $this_cost ;
}
2012-08-14 22:43:48 +00:00
2012-05-16 08:03:49 +00:00
$this -> add_rate ( $extra_rate );
2012-02-29 11:49:39 +00:00
}
2012-08-14 22:43:48 +00:00
}
/**
* order_shipping function .
*
* @ access public
* @ param array $package
* @ return float
*/
2012-04-13 11:16:24 +00:00
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
2012-04-13 11:16:24 +00:00
$found_shipping_classes = array ();
2012-08-14 22:43:48 +00:00
2012-04-13 11:16:24 +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 () )
2012-08-14 22:43:48 +00:00
$found_shipping_classes [] = $values [ 'data' ] -> get_shipping_class ();
2012-04-13 11:16:24 +00:00
}
}
$found_shipping_classes = array_unique ( $found_shipping_classes );
2012-08-14 22:43:48 +00:00
2012-04-13 11:16:24 +00:00
// Find most expensive class (if found)
foreach ( $found_shipping_classes as $shipping_class ) {
2012-05-16 08:03:49 +00:00
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' ];
2012-04-13 11:16:24 +00:00
}
} else {
// No matching classes so use defaults
2012-11-19 14:48:40 +00:00
if ( ! empty ( $this -> cost ) && $this -> cost > $cost ) {
2012-04-13 11:16:24 +00:00
$cost = $this -> cost ;
$fee = $this -> fee ;
}
}
}
}
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 ) ) {
// No match
return null ;
}
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' ] );
}
2012-08-14 22:43:48 +00:00
/**
* class_shipping function .
*
* @ access public
* @ param array $package
* @ return float
*/
2012-04-13 11:16:24 +00:00
function class_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 ) {
$found_shipping_classes = array ();
2012-08-14 22:43:48 +00:00
2012-04-13 11:16:24 +00:00
// Find shipping classes for products in the cart. Store prices too, so we can calc a fee for the class.
if ( sizeof ( $package [ 'contents' ] ) > 0 ) {
foreach ( $package [ 'contents' ] as $item_id => $values ) {
2012-05-16 08:03:49 +00:00
if ( $values [ 'data' ] -> needs_shipping () ) {
if ( isset ( $found_shipping_classes [ $values [ 'data' ] -> get_shipping_class () ] ) ) {
$found_shipping_classes [ $values [ 'data' ] -> get_shipping_class () ] = ( $values [ 'data' ] -> get_price () * $values [ 'quantity' ] ) + $found_shipping_classes [ $values [ 'data' ] -> get_shipping_class () ];
2012-04-13 11:16:24 +00:00
} else {
2012-05-16 08:03:49 +00:00
$found_shipping_classes [ $values [ 'data' ] -> get_shipping_class () ] = ( $values [ 'data' ] -> get_price () * $values [ 'quantity' ] );
2012-04-13 11:16:24 +00:00
}
2012-05-16 08:03:49 +00:00
}
2012-04-13 11:16:24 +00:00
}
}
2012-05-16 08:03:49 +00:00
$found_shipping_classes = array_unique ( $found_shipping_classes );
2012-11-19 14:48:40 +00:00
$matched = false ;
2012-04-13 11:16:24 +00:00
// For each found class, add up the costs and fees
2012-05-16 08:03:49 +00:00
foreach ( $found_shipping_classes 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 );
2012-11-19 14:48:40 +00:00
$matched = true ;
} elseif ( $this -> cost !== '' ) {
// Class not set so we use default rate if its set
2012-04-13 11:16:24 +00:00
$cost += $this -> cost ;
$fee += $this -> get_fee ( $this -> fee , $class_price );
2012-11-19 14:48:40 +00:00
$matched = true ;
2012-05-16 08:03:49 +00:00
}
}
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
2012-11-19 14:48:40 +00:00
if ( $matched )
return $cost + $fee ;
else
return null ;
2012-04-13 11:16:24 +00:00
}
2012-08-14 22:43:48 +00:00
/**
* item_shipping function .
*
* @ access public
* @ param array $package
* @ return array
*/
2012-04-13 11:16:24 +00:00
function item_shipping ( $package ) {
// Per item shipping so we pass an array of costs (per item) instead of a single value
$costs = array ();
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-19 14:48:40 +00:00
$fee = $cost = 0 ;
2012-05-16 08:03:49 +00:00
if ( isset ( $this -> flat_rates [ $shipping_class ] ) ) {
$cost = $this -> flat_rates [ $shipping_class ][ 'cost' ];
$fee = $this -> get_fee ( $this -> flat_rates [ $shipping_class ][ 'fee' ], $_product -> get_price () );
2012-11-19 14:48:40 +00:00
$matched = true ;
} 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-19 14:48:40 +00:00
if ( $matched )
return $costs ;
else
return null ;
2012-04-13 11:16:24 +00:00
}
2011-11-28 15:50:19 +00:00
2012-08-14 22:43:48 +00:00
2011-11-28 15:50:19 +00:00
/**
2012-08-14 22:43:48 +00:00
* Admin Panel Options
2011-11-28 15:50:19 +00:00
* - Options for bits like 'title' and availability on a country - by - country basis
*
* @ since 1.0 . 0
2012-08-14 22:43:48 +00:00
* @ access public
* @ return void
2011-11-28 15:50:19 +00:00
*/
public function admin_options () {
2011-12-02 18:54:52 +00:00
global $woocommerce ;
2011-08-10 17:11:11 +00:00
?>
2012-01-31 18:13:32 +00:00
< h3 >< ? php echo $this -> admin_page_heading ; ?> </h3>
< p >< ? php echo $this -> admin_page_description ; ?> </p>
2011-08-13 13:57:48 +00:00
< table class = " form-table " >
2011-08-10 17:11:11 +00:00
< ? php
2011-11-28 15:50:19 +00:00
// Generate the HTML For the settings form.
$this -> generate_settings_html ();
2011-12-02 18:54:52 +00:00
?>
< tr valign = " top " >
2012-05-16 08:03:49 +00:00
< th scope = " row " class = " titledesc " >< ? php _e ( 'Flat Rates' , 'woocommerce' ); ?> :</th>
2012-01-31 18:13:32 +00:00
< td class = " forminp " id = " <?php echo $this->id ; ?>_flat_rates " >
2011-12-02 18:54:52 +00:00
< table class = " shippingrows widefat " cellspacing = " 0 " >
< thead >
< tr >
< th class = " check-column " >< input type = " checkbox " ></ th >
2012-05-16 08:03:49 +00:00
< 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>
2011-12-02 18:54:52 +00:00
</ tr >
</ thead >
< tfoot >
< tr >
2012-05-16 08:03:49 +00:00
< th colspan = " 2 " >< a href = " # " class = " add button " >< ? php _e ( '+ Add Flat Rate' , 'woocommerce' ); ?> </a></th>
< th colspan = " 2 " >< small >< ? php _e ( 'Add rates for shipping classes here — they will override the default costs defined above.' , 'woocommerce' ); ?> </small> <a href="#" class="remove button"><?php _e( 'Delete selected rates', 'woocommerce' ); ?></a></th>
2011-12-02 18:54:52 +00:00
</ tr >
</ tfoot >
< tbody class = " flat_rates " >
< ? php
2012-05-16 08:03:49 +00:00
$i = - 1 ;
if ( $this -> flat_rates ) {
foreach ( $this -> flat_rates as $class => $rate ) {
$i ++ ;
2011-12-02 18:54:52 +00:00
2012-05-16 08:03:49 +00:00
echo ' < tr class = " flat_rate " >
< td class = " check-column " >< input type = " checkbox " name = " select " /></ td >
< td class = " flat_rate_class " >
2012-10-17 12:46:35 +00:00
< select name = " ' . esc_attr( $this->id . '_class[' . $i . ']' ) . ' " class = " select " > ' ;
2012-08-14 22:43:48 +00:00
2012-05-16 08:03:49 +00:00
if ( $woocommerce -> shipping -> get_shipping_classes () ) {
foreach ( $woocommerce -> shipping -> get_shipping_classes () as $shipping_class ) {
echo '<option value="' . $shipping_class -> slug . '" ' . selected ( $shipping_class -> slug , $class , false ) . '>' . $shipping_class -> name . '</option>' ;
}
} else {
2012-10-16 09:45:33 +00:00
echo '<option value="">' . __ ( 'Select a class…' , 'woocommerce' ) . '</option>' ;
2012-05-16 08:03:49 +00:00
}
2012-08-14 22:43:48 +00:00
2012-05-16 08:03:49 +00:00
echo ' </ select >
</ td >
2012-11-20 17:32:50 +00:00
< td >< input type = " number " step = " any " min = " 0 " value = " ' . esc_attr( $rate['cost'] ) . ' " name = " ' . esc_attr( $this->id .'_cost[' . $i . ']' ) . ' " placeholder = " '.__( '0.00', 'woocommerce' ).' " size = " 4 " /></ td >
2012-10-17 12:46:35 +00:00
< td >< input type = " text " value = " ' . esc_attr( $rate['fee'] ) . ' " name = " ' . esc_attr( $this->id .'_fee[' . $i . ']' ) . ' " placeholder = " '.__( '0.00', 'woocommerce' ).' " size = " 4 " /></ td >
2012-05-16 08:03:49 +00:00
</ tr > ' ;
}
}
2011-12-02 18:54:52 +00:00
?>
2012-08-14 22:43:48 +00:00
</ tbody >
2011-12-02 18:54:52 +00:00
</ table >
</ td >
</ tr >
2011-11-28 15:50:19 +00:00
</ table ><!--/. form - table -->
2011-12-02 18:54:52 +00:00
< script type = " text/javascript " >
jQuery ( function () {
2012-08-14 22:43:48 +00:00
2012-01-31 18:13:32 +00:00
jQuery ( '#<?php echo $this->id; ?>_flat_rates a.add' ) . live ( 'click' , function (){
2012-08-14 22:43:48 +00:00
2012-01-31 18:13:32 +00:00
var size = jQuery ( '#<?php echo $this->id; ?>_flat_rates tbody .flat_rate' ) . size ();
2012-08-14 22:43:48 +00:00
2011-12-02 18:54:52 +00:00
jQuery ( ' < tr class = " flat_rate " > \
< td class = " check-column " >< input type = " checkbox " name = " select " /></ td > \
< td class = " flat_rate_class " > \
2012-01-31 18:13:32 +00:00
< select name = " <?php echo $this->id ; ?>_class[' + size + '] " class = " select " > \
2012-08-14 22:43:48 +00:00
< ? php
2011-12-02 18:54:52 +00:00
if ( $woocommerce -> shipping -> get_shipping_classes ()) :
foreach ( $woocommerce -> shipping -> get_shipping_classes () as $class ) :
2012-10-17 12:46:35 +00:00
echo '<option value="' . esc_attr ( $class -> slug ) . '">' . esc_js ( $class -> name ) . '</option>' ;
2011-12-02 18:54:52 +00:00
endforeach ;
else :
2012-10-16 09:45:33 +00:00
echo '<option value="">' . __ ( 'Select a class…' , 'woocommerce' ) . '</option>' ;
2011-12-02 18:54:52 +00:00
endif ;
?> \
</ select > \
</ td > \
2012-11-20 17:32:50 +00:00
< td >< input type = " number " step = " any " min = " 0 " name = " <?php echo $this->id ; ?>_cost[' + size + '] " placeholder = " 0.00 " size = " 4 " /></ td > \
2012-03-05 10:15:19 +00:00
< td >< input type = " text " name = " <?php echo $this->id ; ?>_fee[' + size + '] " placeholder = " 0.00 " size = " 4 " /></ td > \
2012-01-31 18:13:32 +00:00
</ tr > ').appendTo(' #<?php echo $this->id; ?>_flat_rates table tbody');
2012-08-14 22:43:48 +00:00
2011-12-02 18:54:52 +00:00
return false ;
});
2012-08-14 22:43:48 +00:00
2011-12-02 18:54:52 +00:00
// Remove row
2012-01-31 18:13:32 +00:00
jQuery ( '#<?php echo $this->id; ?>_flat_rates a.remove' ) . live ( 'click' , function (){
2012-10-16 09:45:33 +00:00
var answer = confirm ( " <?php _e( 'Delete the selected rates?', 'woocommerce' ); ?> " )
2011-12-02 18:54:52 +00:00
if ( answer ) {
2012-01-31 18:13:32 +00:00
jQuery ( '#<?php echo $this->id; ?>_flat_rates table tbody tr td.check-column input:checked' ) . each ( function ( i , el ){
2011-12-02 18:54:52 +00:00
jQuery ( el ) . closest ( 'tr' ) . remove ();
});
}
return false ;
});
2012-08-14 22:43:48 +00:00
2011-12-02 18:54:52 +00:00
});
</ script >
2011-11-28 15:50:19 +00:00
< ? php
} // End admin_options()
2012-08-14 22:43:48 +00:00
/**
* process_flat_rates function .
*
* @ access public
* @ return void
*/
2011-12-02 18:54:52 +00:00
function process_flat_rates () {
// 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
2012-05-16 08:03:49 +00:00
if ( isset ( $_POST [ $this -> id . '_class' ] ) ) $flat_rate_class = array_map ( 'woocommerce_clean' , $_POST [ $this -> id . '_class' ] );
if ( isset ( $_POST [ $this -> id . '_cost' ] ) ) $flat_rate_cost = array_map ( 'woocommerce_clean' , $_POST [ $this -> id . '_cost' ] );
if ( isset ( $_POST [ $this -> id . '_fee' ] ) ) $flat_rate_fee = array_map ( 'woocommerce_clean' , $_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 ++ ) {
if ( isset ( $flat_rate_class [ $i ] ) && isset ( $flat_rate_cost [ $i ] ) && isset ( $flat_rate_fee [ $i ] ) ) {
2012-08-14 22:43:48 +00:00
2011-12-02 18:54:52 +00:00
$flat_rate_cost [ $i ] = number_format ( $flat_rate_cost [ $i ], 2 , '.' , '' );
2012-08-14 22:43:48 +00:00
2011-12-02 18:54:52 +00:00
// Add to flat rates array
$flat_rates [ 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 ();
}
2012-08-14 22:43:48 +00:00
/**
* get_flat_rates function .
*
* @ access public
* @ return void
*/
2011-12-02 18:54:52 +00:00
function get_flat_rates () {
2012-05-16 08:03:49 +00:00
$this -> flat_rates = array_filter ( ( array ) get_option ( $this -> flat_rate_option ) );
2011-12-02 18:54:52 +00:00
}
2011-08-10 17:11:11 +00:00
}
2012-08-14 22:43:48 +00:00
/**
* add_flat_rate_method function .
*
2012-08-15 18:15:06 +00:00
* @ package WooCommerce / Classes / Shipping
2012-08-14 22:43:48 +00:00
* @ access public
* @ param array $methods
* @ return array
*/
2011-08-10 17:11:11 +00:00
function add_flat_rate_method ( $methods ) {
2012-08-14 22:43:48 +00:00
$methods [] = 'WC_Flat_Rate' ;
return $methods ;
2011-08-10 17:11:11 +00:00
}
2012-08-14 22:43:48 +00:00
add_filter ( 'woocommerce_shipping_methods' , 'add_flat_rate_method' );