BW compat for additional rates
This commit is contained in:
parent
d24461a9a5
commit
803714d088
|
@ -22,6 +22,7 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
|
|||
$this->method_description = __( 'Flat Rate Shipping lets you charge a fixed rate for shipping.', 'woocommerce' );
|
||||
|
||||
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
|
||||
add_action( 'woocommerce_flat_rate_shipping_add_rate', array( $this, 'calculate_extra_shipping' ), 10, 3 );
|
||||
|
||||
$this->init();
|
||||
}
|
||||
|
@ -40,6 +41,9 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
|
|||
$this->countries = $this->get_option( 'countries' );
|
||||
$this->type = $this->get_option( 'type' );
|
||||
$this->tax_status = $this->get_option( 'tax_status' );
|
||||
|
||||
// @deprecated in 2.4.0
|
||||
$this->options = $this->get_option( 'options', false );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -129,7 +133,7 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
|
|||
* $method->add_rate( $new_rate );
|
||||
* }
|
||||
*/
|
||||
do_action( 'woocommerce_flat_rate_shipping_add_rate', $this, $rate );
|
||||
do_action( 'woocommerce_flat_rate_shipping_add_rate', $this, $rate, $package );
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -169,4 +173,110 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
|
|||
|
||||
return $found_shipping_classes;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds extra calculated flat rates
|
||||
*
|
||||
* @deprecated 2.4.0
|
||||
*
|
||||
* Additonal rates defined like this:
|
||||
* Option Name | Additional Cost [+- Percents%] | Per Cost Type (order, class, or item)
|
||||
*/
|
||||
public function calculate_extra_shipping( $method, $rate, $package ) {
|
||||
if ( $this->options ) {
|
||||
$options = array_filter( (array) explode( "\n", $this->options ) );
|
||||
|
||||
foreach ( $options as $option ) {
|
||||
$this_option = array_map( 'trim', explode( WC_DELIMITER, $option ) );
|
||||
if ( sizeof( $this_option ) !== 3 ) {
|
||||
continue;
|
||||
}
|
||||
$extra_rate = $rate;
|
||||
$extra_rate['id'] = $this->id . ':' . urldecode( sanitize_title( $this_option[0] ) );
|
||||
$extra_rate['label'] = $this_option[0];
|
||||
$extra_cost = $this->get_extra_cost( $this_option[1], $this_option[2], $package );
|
||||
if ( is_array( $extra_rate['cost'] ) ) {
|
||||
$extra_rate['cost']['order'] = $extra_rate['cost']['order'] + $extra_cost;
|
||||
} else {
|
||||
$extra_rate['cost'] += $extra_cost;
|
||||
}
|
||||
$this->add_rate( $extra_rate );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculate the percentage adjustment for each shipping rate.
|
||||
*
|
||||
* @deprecated 2.4.0
|
||||
* @param float $cost
|
||||
* @param float $percent_adjustment
|
||||
* @param string $percent_operator
|
||||
* @param float $base_price
|
||||
* @return float
|
||||
*/
|
||||
public 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;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get extra cost
|
||||
*
|
||||
* @deprecated 2.4.0
|
||||
* @param string $cost_string
|
||||
* @param string $type
|
||||
* @param array $package
|
||||
* @return float
|
||||
*/
|
||||
public function get_extra_cost( $cost_string, $type, $package ) {
|
||||
$cost = $cost_string;
|
||||
$cost_percent = false;
|
||||
$pattern =
|
||||
'/' . // 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, $cost_string, $this_cost_matches ) ) {
|
||||
$cost_operator = $this_cost_matches[2];
|
||||
$cost_percent = $this_cost_matches[3] / 100;
|
||||
$cost = $this_cost_matches[1];
|
||||
}
|
||||
switch ( $type ) {
|
||||
case 'class' :
|
||||
$cost = $cost * sizeof( $this->find_shipping_classes( $package ) );
|
||||
break;
|
||||
case 'item' :
|
||||
$cost = $cost * $this->get_package_item_qty( $package );
|
||||
break;
|
||||
}
|
||||
if ( $cost_percent ) {
|
||||
switch ( $type ) {
|
||||
case 'class' :
|
||||
$shipping_classes = $this->find_shipping_classes( $package );
|
||||
foreach ( $shipping_classes as $shipping_class => $items ){
|
||||
foreach ( $items as $item_id => $values ) {
|
||||
$cost = $this->calc_percentage_adjustment( $cost, $cost_percent, $cost_operator, $values['line_total'] );
|
||||
}
|
||||
}
|
||||
break;
|
||||
case 'item' :
|
||||
foreach ( $package['contents'] as $item_id => $values ) {
|
||||
$cost = $this->calc_percentage_adjustment( $cost, $cost_percent, $cost_operator, $values['line_total'] );
|
||||
}
|
||||
break;
|
||||
case 'order' :
|
||||
$cost = $this->calc_percentage_adjustment( $cost, $cost_percent, $cost_operator, $package['contents_cost'] );
|
||||
break;
|
||||
}
|
||||
}
|
||||
return $cost;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -75,4 +75,15 @@ if ( WC()->shipping->get_shipping_classes() ) {
|
|||
}
|
||||
}
|
||||
|
||||
if ( $this->get_option( 'options', false ) ) {
|
||||
$settings['options'] = array(
|
||||
'title' => __( 'Additional Rates', 'woocommerce' ),
|
||||
'type' => 'textarea',
|
||||
'description' => __( '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' ),
|
||||
'default' => '',
|
||||
'desc_tip' => true,
|
||||
'placeholder' => __( 'Option Name | Additional Cost [+- Percents%] | Per Cost Type (order, class, or item)', 'woocommerce' )
|
||||
);
|
||||
}
|
||||
|
||||
return $settings;
|
||||
|
|
Loading…
Reference in New Issue