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 {
public function __construct () {
$this -> id = 'flat_rate' ;
$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' );
add_action ( 'woocommerce_update_options' , array ( & $this , 'process_admin_options' ));
add_option ( 'woocommerce_flat_rate_availability' , 'all' );
add_option ( 'woocommerce_flat_rate_title' , 'Flat Rate' );
add_option ( 'woocommerce_flat_rate_tax_status' , 'taxable' );
}
public function calculate_shipping () {
$_tax = & new woocommerce_tax ();
$this -> shipping_total = 0 ;
$this -> shipping_tax = 0 ;
if ( $this -> type == 'order' ) :
// Shipping for whole order
$this -> shipping_total = $this -> cost + $this -> get_fee ( $this -> fee , woocommerce_cart :: $cart_contents_total );
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
if ( sizeof ( woocommerce_cart :: $cart_contents ) > 0 ) : foreach ( woocommerce_cart :: $cart_contents as $item_id => $values ) :
$_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
if ( $_product -> is_type ( 'simple' ) || $_product -> is_type ( 'variable' )) :
$this -> shipping_total = $this -> shipping_total + $item_shipping_price ;
if ( $_product -> is_shipping_taxable () && $this -> tax_status == 'taxable' ) :
2011-08-17 23:42:07 +00:00
$rate = $_tax -> get_shipping_tax_rate ( $_product -> 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 ;
}
public function admin_options () {
?>
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 " >
< input type = " text " name = " woocommerce_flat_rate_cost " id = " woocommerce_flat_rate_cost " style = " min-width:50px; " value = " <?php if ( $value = get_option('woocommerce_flat_rate_cost')) echo $value ; ?> " /> < span class = " description " >< ? php _e ( 'Cost excluding tax. Enter an amount, e.g. 2.50.' , '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 ( 'Handling Fee' , 'woothemes' ) ?> </th>
< td class = " forminp " >
< input type = " text " name = " woocommerce_flat_rate_handling_fee " id = " woocommerce_flat_rate_handling_fee " style = " min-width:50px; " value = " <?php if ( $value = get_option('woocommerce_flat_rate_handling_fee')) echo $value ; ?> " /> < 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>
</ 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
$countries = woocommerce_countries :: $countries ;
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
}
public function process_admin_options () {
2011-09-02 14:42:04 +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-08-10 17:11: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' );
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' );
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' );