2011-08-10 17:11:11 +00:00
< ? php
/**
* Flat Rate Shipping Method
*
* A simple shipping method for a flat fee per item or per order
*
* @ class flat_rate
* @ package WooCommerce
* @ category Shipping
* @ author WooThemes
*/
class flat_rate extends woocommerce_shipping_method {
2011-09-06 11:18:22 +00:00
function __construct () {
2011-08-10 17:11:11 +00:00
$this -> id = 'flat_rate' ;
2011-09-05 11:56:26 +00:00
$this -> method_title = __ ( 'Flat rate' , 'woothemes' );
2011-08-10 17:11:11 +00:00
$this -> enabled = get_option ( 'woocommerce_flat_rate_enabled' );
$this -> title = get_option ( 'woocommerce_flat_rate_title' );
$this -> availability = get_option ( 'woocommerce_flat_rate_availability' );
$this -> countries = get_option ( 'woocommerce_flat_rate_countries' );
$this -> type = get_option ( 'woocommerce_flat_rate_type' );
$this -> tax_status = get_option ( 'woocommerce_flat_rate_tax_status' );
$this -> cost = get_option ( 'woocommerce_flat_rate_cost' );
$this -> fee = get_option ( 'woocommerce_flat_rate_handling_fee' );
2011-09-15 16:37:38 +00:00
add_action ( 'woocommerce_update_options_shipping_methods' , array ( & $this , 'process_admin_options' ));
2011-08-10 17:11:11 +00:00
add_option ( 'woocommerce_flat_rate_availability' , 'all' );
add_option ( 'woocommerce_flat_rate_title' , 'Flat Rate' );
add_option ( 'woocommerce_flat_rate_tax_status' , 'taxable' );
}
2011-09-06 11:18:22 +00:00
function calculate_shipping () {
2011-09-06 11:11:22 +00:00
global $woocommerce ;
2011-08-10 17:11:11 +00:00
$_tax = & new woocommerce_tax ();
$this -> shipping_total = 0 ;
$this -> shipping_tax = 0 ;
if ( $this -> type == 'order' ) :
// Shipping for whole order
2011-09-06 11:11:22 +00:00
$this -> shipping_total = $this -> cost + $this -> get_fee ( $this -> fee , $woocommerce -> cart -> cart_contents_total );
2011-08-10 17:11:11 +00:00
if ( get_option ( 'woocommerce_calc_taxes' ) == 'yes' && $this -> tax_status == 'taxable' ) :
$rate = $_tax -> get_shipping_tax_rate ();
if ( $rate > 0 ) :
$tax_amount = $_tax -> calc_shipping_tax ( $this -> shipping_total , $rate );
$this -> shipping_tax = $this -> shipping_tax + $tax_amount ;
endif ;
endif ;
else :
// Shipping per item
2011-11-06 13:45:18 +00:00
if ( sizeof ( $woocommerce -> cart -> get_cart ()) > 0 ) : foreach ( $woocommerce -> cart -> get_cart () as $item_id => $values ) :
2011-08-10 17:11:11 +00:00
$_product = $values [ 'data' ];
if ( $_product -> exists () && $values [ 'quantity' ] > 0 ) :
$item_shipping_price = ( $this -> cost + $this -> get_fee ( $this -> fee , $_product -> get_price () )) * $values [ 'quantity' ];
// Only count 'psysical' products
2011-11-05 19:03:03 +00:00
if ( $_product -> needs_shipping () ) :
2011-08-10 17:11:11 +00:00
$this -> shipping_total = $this -> shipping_total + $item_shipping_price ;
if ( $_product -> is_shipping_taxable () && $this -> tax_status == 'taxable' ) :
2011-10-07 22:24:11 +00:00
$rate = $_tax -> get_shipping_tax_rate ( $_product -> get_tax_class () );
2011-08-10 17:11:11 +00:00
if ( $rate > 0 ) :
$tax_amount = $_tax -> calc_shipping_tax ( $item_shipping_price , $rate );
$this -> shipping_tax = $this -> shipping_tax + $tax_amount ;
endif ;
endif ;
endif ;
endif ;
endforeach ; endif ;
endif ;
}
2011-09-06 11:18:22 +00:00
function admin_options () {
2011-09-06 11:11:22 +00:00
global $woocommerce ;
2011-08-10 17:11:11 +00:00
?>
2011-08-13 13:57:48 +00:00
< h3 >< ? php _e ( 'Flat Rates' , 'woothemes' ); ?> </h3>
< p >< ? php _e ( 'Flat rates let you define a standard rate per item, or per order.' , 'woothemes' ); ?> </p>
< table class = " form-table " >
2011-08-19 20:11:04 +00:00
< tr valign = " top " >
2011-08-13 16:07:10 +00:00
< th scope = " row " class = " titledesc " >< ? php _e ( 'Enable/disable' , 'woothemes' ) ?> </th>
2011-08-13 13:57:48 +00:00
< td class = " forminp " >
2011-08-13 16:07:10 +00:00
< fieldset >< legend class = " screen-reader-text " >< span >< ? php _e ( 'Enable/disable' , 'woothemes' ) ?> </span></legend>
< label for = " woocommerce_flat_rate_enabled " >
2011-08-13 13:57:48 +00:00
< input name = " woocommerce_flat_rate_enabled " id = " woocommerce_flat_rate_enabled " type = " checkbox " value = " 1 " < ? php checked ( get_option ( 'woocommerce_flat_rate_enabled' ), 'yes' ); ?> /> <?php _e('Enable Flat Rate', 'woothemes') ?></label><br>
</ fieldset >
</ td >
</ tr >
2011-08-19 20:11:04 +00:00
< tr valign = " top " >
2011-08-13 13:57:48 +00:00
< th scope = " row " class = " titledesc " >< ? php _e ( 'Method Title' , 'woothemes' ) ?> </th>
< td class = " forminp " >
< input type = " text " name = " woocommerce_flat_rate_title " id = " woocommerce_flat_rate_title " style = " min-width:50px; " value = " <?php if ( $value = get_option('woocommerce_flat_rate_title')) echo $value ; else echo 'Flat Rate'; ?> " /> < span class = " description " >< ? php _e ( 'This controls the title which the user sees during checkout.' , 'woothemes' ) ?> </span>
</ td >
</ tr >
2011-08-19 20:11:04 +00:00
< tr valign = " top " >
2011-08-13 13:57:48 +00:00
< th scope = " row " class = " titledesc " >< ? php _e ( 'Type' , 'woothemes' ) ?> </th>
< td class = " forminp " >
< select name = " woocommerce_flat_rate_type " id = " woocommerce_flat_rate_type " style = " min-width:100px; " >
< option value = " order " < ? php if ( get_option ( 'woocommerce_flat_rate_type' ) == 'order' ) echo 'selected="selected"' ; ?> ><?php _e('Per Order', 'woothemes'); ?></option>
< option value = " item " < ? php if ( get_option ( 'woocommerce_flat_rate_type' ) == 'item' ) echo 'selected="selected"' ; ?> ><?php _e('Per Item', 'woothemes'); ?></option>
</ select >
</ td >
</ tr >
< ? php $_tax = new woocommerce_tax (); ?>
2011-08-19 20:11:04 +00:00
< tr valign = " top " >
2011-08-13 13:57:48 +00:00
< th scope = " row " class= " titledesc " ><?php _e('Tax Status', 'woothemes') ?></th>
< td class = " forminp " >
< select name = " woocommerce_flat_rate_tax_status " >
< option value = " taxable " < ? php if ( get_option ( 'woocommerce_flat_rate_tax_status' ) == 'taxable' ) echo 'selected="selected"' ; ?> ><?php _e('Taxable', 'woothemes'); ?></option>
< option value = " none " < ? php if ( get_option ( 'woocommerce_flat_rate_tax_status' ) == 'none' ) echo 'selected="selected"' ; ?> ><?php _e('None', 'woothemes'); ?></option>
</ select >
</ td >
</ tr >
2011-08-19 20:11:04 +00:00
< tr valign = " top " >
2011-08-13 13:57:48 +00:00
< th scope = " row " class = " titledesc " >< ? php _e ( 'Cost' , 'woothemes' ) ?> </th>
< td class = " forminp " >
2011-09-19 06:20:43 +00:00
< input type = " text " name = " woocommerce_flat_rate_cost " id = " woocommerce_flat_rate_cost " style = " min-width:50px; " value = " <?php echo esc_attr( get_option( 'woocommerce_flat_rate_cost' ) ); ?> " /> < span class = " description " >< ? php _e ( 'Cost excluding tax. Enter an amount, e.g. 2.50.' , 'woothemes' ) ?> </span>
2011-08-13 13:57:48 +00:00
</ td >
</ tr >
2011-08-19 20:11:04 +00:00
< tr valign = " top " >
2011-08-13 13:57:48 +00:00
< th scope = " row " class = " titledesc " >< ? php _e ( 'Handling Fee' , 'woothemes' ) ?> </th>
< td class = " forminp " >
2011-09-19 06:20:43 +00:00
< input type = " text " name = " woocommerce_flat_rate_handling_fee " id = " woocommerce_flat_rate_handling_fee " style = " min-width:50px; " value = " <?php echo esc_attr( get_option( 'woocommerce_flat_rate_handling_fee' ) ); ?> " /> < span class = " description " >< ? php _e ( 'Fee excluding tax. Enter an amount, e.g. 2.50, or a percentage, e.g. 5%. Leave blank to disable.' , 'woothemes' ) ?> </span>
2011-08-13 13:57:48 +00:00
</ td >
</ tr >
2011-08-19 20:11:04 +00:00
< tr valign = " top " >
2011-08-13 13:57:48 +00:00
< th scope = " row " class = " titledesc " >< ? php _e ( 'Method availability' , 'woothemes' ) ?> </th>
< td class = " forminp " >
< select name = " woocommerce_flat_rate_availability " id = " woocommerce_flat_rate_availability " style = " min-width:100px; " >
< option value = " all " < ? php if ( get_option ( 'woocommerce_flat_rate_availability' ) == 'all' ) echo 'selected="selected"' ; ?> ><?php _e('All allowed countries', 'woothemes'); ?></option>
< option value = " specific " < ? php if ( get_option ( 'woocommerce_flat_rate_availability' ) == 'specific' ) echo 'selected="selected"' ; ?> ><?php _e('Specific Countries', 'woothemes'); ?></option>
</ select >
</ td >
</ tr >
< ? php
2011-09-06 11:11:22 +00:00
$countries = $woocommerce -> countries -> countries ;
2011-08-13 13:57:48 +00:00
asort ( $countries );
$selections = get_option ( 'woocommerce_flat_rate_countries' , array ());
?> <tr class="multi_select_countries">
< th scope = " row " class = " titledesc " >< ? php _e ( 'Specific Countries' , 'woothemes' ); ?> </th>
< td class = " forminp " >
< div class = " multi_select_countries " >< ul >< ? php
if ( $countries ) foreach ( $countries as $key => $val ) :
echo '<li><label><input type="checkbox" name="woocommerce_flat_rate_countries[]" value="' . $key . '" ' ;
if ( in_array ( $key , $selections )) echo 'checked="checked"' ;
echo ' />' . __ ( $val , 'woothemes' ) . '</label></li>' ;
endforeach ;
?> </ul></div>
</ td >
</ tr >
</ table >
2011-08-10 17:11:11 +00:00
< script type = " text/javascript " >
jQuery ( function () {
jQuery ( 'select#woocommerce_flat_rate_availability' ) . change ( function (){
if ( jQuery ( this ) . val () == " specific " ) {
jQuery ( this ) . parent () . parent () . next ( 'tr.multi_select_countries' ) . show ();
} else {
jQuery ( this ) . parent () . parent () . next ( 'tr.multi_select_countries' ) . hide ();
}
}) . change ();
});
</ script >
< ? php
}
2011-09-06 11:18:22 +00:00
function process_admin_options () {
2011-08-10 17:11:11 +00:00
2011-09-03 22:52:11 +00:00
if ( isset ( $_POST [ 'woocommerce_flat_rate_tax_status' ])) update_option ( 'woocommerce_flat_rate_tax_status' , woocommerce_clean ( $_POST [ 'woocommerce_flat_rate_tax_status' ])); else delete_option ( 'woocommerce_flat_rate_tax_status' );
2011-08-10 17:11:11 +00:00
2011-08-13 13:57:48 +00:00
if ( isset ( $_POST [ 'woocommerce_flat_rate_enabled' ])) update_option ( 'woocommerce_flat_rate_enabled' , 'yes' ); else update_option ( 'woocommerce_flat_rate_enabled' , 'no' );
2011-09-03 22:52:11 +00:00
if ( isset ( $_POST [ 'woocommerce_flat_rate_title' ])) update_option ( 'woocommerce_flat_rate_title' , woocommerce_clean ( $_POST [ 'woocommerce_flat_rate_title' ])); else delete_option ( 'woocommerce_flat_rate_title' );
if ( isset ( $_POST [ 'woocommerce_flat_rate_type' ])) update_option ( 'woocommerce_flat_rate_type' , woocommerce_clean ( $_POST [ 'woocommerce_flat_rate_type' ])); else delete_option ( 'woocommerce_flat_rate_type' );
if ( isset ( $_POST [ 'woocommerce_flat_rate_cost' ])) update_option ( 'woocommerce_flat_rate_cost' , woocommerce_clean ( $_POST [ 'woocommerce_flat_rate_cost' ])); else delete_option ( 'woocommerce_flat_rate_cost' );
if ( isset ( $_POST [ 'woocommerce_flat_rate_handling_fee' ])) update_option ( 'woocommerce_flat_rate_handling_fee' , woocommerce_clean ( $_POST [ 'woocommerce_flat_rate_handling_fee' ])); else delete_option ( 'woocommerce_flat_rate_handling_fee' );
2011-08-10 17:11:11 +00:00
2011-09-03 22:52:11 +00:00
if ( isset ( $_POST [ 'woocommerce_flat_rate_availability' ])) update_option ( 'woocommerce_flat_rate_availability' , woocommerce_clean ( $_POST [ 'woocommerce_flat_rate_availability' ])); else delete_option ( 'woocommerce_flat_rate_availability' );
2011-08-10 17:11:11 +00:00
if ( isset ( $_POST [ 'woocommerce_flat_rate_countries' ])) $selected_countries = $_POST [ 'woocommerce_flat_rate_countries' ]; else $selected_countries = array ();
update_option ( 'woocommerce_flat_rate_countries' , $selected_countries );
}
}
function add_flat_rate_method ( $methods ) {
$methods [] = 'flat_rate' ; return $methods ;
}
add_filter ( 'woocommerce_shipping_methods' , 'add_flat_rate_method' );