Merge branch 'flat-rate-shipping-refactor'

This commit is contained in:
Mike Jolley 2015-06-16 14:09:38 +01:00
commit 93fbfe0bc3
6 changed files with 675 additions and 683 deletions

View File

@ -1,117 +1,190 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
exit;
}
/**
* Flat Rate Shipping Method
*
* A simple shipping method for a flat fee per item or per order
*
* @class WC_Shipping_Flat_Rate
* @version 2.0.0
* @version 2.4.0
* @package WooCommerce/Classes/Shipping
* @author WooThemes
*/
class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
/**
* Stores an array of rates
* @var array
*/
public $flat_rates = array();
/** @var string cost passed to [fee] shortcode */
protected $fee_cost = '';
/**
* __construct function.
*
* @access public
* @return void
* Constructor
*/
public function __construct() {
$this->id = 'flat_rate';
$this->method_title = __( 'Flat Rate', 'woocommerce' );
$this->flat_rate_option = 'woocommerce_flat_rates';
$this->method_description = __( 'Flat rates let you define a standard rate per item, or per order.', 'woocommerce' );
$this->id = 'flat_rate';
$this->method_title = __( 'Flat Rate', 'woocommerce' );
$this->method_description = __( 'Flat Rate Shipping lets you charge a fixed rate for shipping.', 'woocommerce' );
$this->init();
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' ) );
add_filter( 'woocommerce_settings_api_sanitized_fields_' . $this->id, array( $this, 'save_default_costs' ) );
$this->init();
add_action( 'woocommerce_flat_rate_shipping_add_rate', array( $this, 'calculate_extra_shipping' ), 10, 3 );
}
/**
* init function.
*
* @access public
* @return void
*/
public function init() {
// Load the settings.
$this->init_form_fields();
$this->init_settings();
// Define user set variables
$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_filter( (array) explode( "\n", $this->get_option( 'options' ) ) );
// Load Flat rates
$this->get_flat_rates();
$this->title = $this->get_option( 'title' );
$this->availability = $this->get_option( 'availability' );
$this->countries = $this->get_option( 'countries' );
$this->tax_status = $this->get_option( 'tax_status' );
$this->cost = $this->get_option( 'cost' );
$this->type = $this->get_option( 'type', 'class' );
$this->options = $this->get_option( 'options', false ); // @deprecated in 2.4.0
}
/**
* Initialise Gateway Settings Form Fields
*
* @access public
* @return void
* Initialise Settings Form Fields
*/
public function init_form_fields() {
$this->form_fields = include( 'includes/settings-flat-rate.php' );
}
/**
* Evaluate a cost from a sum/string
* @param string $sum
* @param array $args
* @return string
*/
protected function evaluate_cost( $sum, $args = array() ) {
include_once( 'includes/class-wc-eval-math.php' );
add_shortcode( 'fee', array( $this, 'fee' ) );
$this->fee_cost = $args['cost'];
$sum = do_shortcode( str_replace(
array(
'[qty]',
'[cost]'
),
array(
$args['qty'],
$args['cost']
),
$sum
) );
remove_shortcode( 'fee', array( $this, 'fee' ) );
return $sum ? WC_Eval_Math::evaluate( $sum ) : 0;
}
/**
* Work out fee (shortcode)
* @param array $atts
* @return string
*/
public function fee( $atts ) {
$atts = shortcode_atts( array(
'percent' => '',
'min_fee' => ''
), $atts );
$calculated_fee = 0;
if ( $atts['percent'] ) {
$calculated_fee = $this->fee_cost * ( floatval( $atts['percent'] ) / 100 );
}
if ( $atts['min_fee'] && $calculated_fee < $atts['min_fee'] ) {
$calculated_fee = $atts['min_fee'];
}
return $calculated_fee;
}
/**
* calculate_shipping function.
*
* @param array $package (default: array())
*/
public function calculate_shipping( $package = array() ) {
$this->rates = array();
$cost_per_order = ! empty( $this->cost_per_order ) ? $this->cost_per_order : 0;
$rate = array(
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => 0,
);
if ( method_exists( $this, $this->type . '_shipping' ) ) {
$costs = call_user_func( array( $this, $this->type . '_shipping' ), $package );
// Calculate the costs
$has_costs = false; // True when a cost is set. False if all costs are blank strings.
$cost = $this->get_option( 'cost' );
if ( ! is_null( $costs ) || $costs > 0 ) {
if ( is_array( $costs ) ) {
$costs['order'] = $cost_per_order;
} else {
$costs += $cost_per_order;
}
if ( $cost !== '' ) {
$has_costs = true;
$rate['cost'] = $this->evaluate_cost( $cost, array(
'qty' => $this->get_package_item_qty( $package ),
'cost' => $package['contents_cost']
) );
}
$rate['cost'] = $costs;
$rate['calc_tax'] = 'per_' . $this->type;
// Add shipping class costs
$found_shipping_classes = $this->find_shipping_classes( $package );
$highest_class_cost = 0;
$this->add_rate( $rate );
foreach ( $found_shipping_classes as $shipping_class => $products ) {
$class_cost_string = $shipping_class ? $this->get_option( 'class_cost_' . $shipping_class, '' ) : $this->get_option( 'no_class_cost', '' );
if ( $class_cost_string === '' ) {
continue;
}
$has_costs = true;
$class_cost = $this->evaluate_cost( $class_cost_string, array(
'qty' => array_sum( wp_list_pluck( $products, 'quantity' ) ),
'cost' => array_sum( wp_list_pluck( $products, 'line_total' ) )
) );
if ( $this->type === 'class' ) {
$rate['cost'] += $class_cost;
} else {
$highest_class_cost = $class_cost > $highest_class_cost ? $class_cost : $highest_class_cost;
}
}
$this->calculate_extra_shipping( $rate, $package );
if ( $this->type === 'order' && $highest_class_cost ) {
$rate['cost'] += $highest_class_cost;
}
// Add the rate
if ( $has_costs ) {
$this->add_rate( $rate );
}
/**
* Developers can add additional flat rates based on this one via this action since @version 2.4
*
* Previously there were (overly complex) options to add additional rates however this was not user
* friendly and goes against what Flat Rate Shipping was originally intended for.
*
* This example shows how you can add an extra rate based on this flat rate via custom function:
*
* add_action( 'woocommerce_flat_rate_shipping_add_rate', 'add_another_custom_flat_rate', 10, 2 );
*
* function add_another_custom_flat_rate( $method, $rate ) {
* $new_rate = $rate;
* $new_rate['id'] .= ':' . 'custom_rate_name'; // Append a custom ID
* $new_rate['label'] = 'Rushed Shipping'; // Rename to 'Rushed Shipping'
* $new_rate['cost'] += 2; // Add $2 to the cost
*
* // Add it to WC
* $method->add_rate( $new_rate );
* }
*/
do_action( 'woocommerce_' . $this->id . '_shipping_add_rate', $this, $rate, $package );
}
/**
@ -121,265 +194,16 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
*/
public function get_package_item_qty( $package ) {
$total_quantity = 0;
foreach ( $package['contents'] as $item_id => $values ) {
if ( $values['quantity'] > 0 && $values['data']->needs_shipping() ) {
$total_quantity += $values['quantity'];
}
}
return $total_quantity;
}
/**
* Get extra cost
* @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;
}
/**
* Adds extra calculated flat rates
*
* Additonal rates defined like this:
* Option Name | Additional Cost [+- Percents%] | Per Cost Type (order, class, or item)
*/
public function calculate_extra_shipping( $rate, $package ) {
foreach ( $this->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.
*
* @access public
* @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;
}
/**
* order_shipping function.
*
* @param array $package
* @return float
*/
public function order_shipping( $package ) {
$cost = null;
$fee = null;
if ( sizeof( $this->flat_rates ) > 0 ) {
$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'];
}
// No matching classes so use defaults
} elseif ( $this->cost > $cost ) {
$cost = $this->cost;
$fee = $this->fee;
}
}
}
// Default rates if set
if ( is_null( $cost ) && $this->cost !== '' ) {
$cost = $this->cost;
$fee = $this->fee;
} elseif ( is_null( $cost ) ) {
// Set rates to 0 if nothing is set by the user
$cost = 0;
$fee = 0;
}
// Shipping for whole order
return $cost + $this->get_fee( $fee, $package['contents_cost'] );
}
/**
* class_shipping function.
*
* @access public
* @param array $package
* @return float
*/
public function class_shipping( $package ) {
$cost = null;
$fee = null;
$matched = false;
if ( sizeof( $this->flat_rates ) > 0 || $this->cost !== '' ) {
// 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 ) {
if ( ! isset( $found_shipping_classes_values[ $shipping_class ] ) ) {
$found_shipping_classes_values[ $shipping_class ] = 0;
}
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;
}
}
}
// Total
if ( $matched ) {
return $cost + $fee;
} else {
return 0;
}
}
/**
* item_shipping function.
*
* @access public
* @param array $package
* @return array
*/
public function item_shipping( $package ) {
// Per item shipping so we pass an array of costs (per item) instead of a single value
$costs = array();
$matched = false;
// Shipping per item
foreach ( $package['contents'] as $item_id => $values ) {
$_product = $values['data'];
if ( $values['quantity'] > 0 && $_product->needs_shipping() ) {
$shipping_class = $_product->get_shipping_class();
$fee = $cost = 0;
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() );
$matched = true;
} elseif ( $this->cost !== '' ) {
$cost = $this->cost;
$fee = $this->get_fee( $this->fee, $_product->get_price() );
$matched = true;
}
$costs[ $item_id ] = ( ( $cost + $fee ) * $values['quantity'] );
}
}
if ( $matched ) {
return $costs;
} else {
return 0;
}
}
/**
* Finds and returns shipping classes and the products with said class.
*
* @access public
* @param mixed $package
* @return array
*/
@ -402,79 +226,108 @@ class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
}
/**
* validate_additional_costs_field function.
* Adds extra calculated flat rates
*
* @access public
* @param mixed $key
* @return bool
*/
public function validate_additional_costs_table_field( $key ) {
return false;
}
/**
* generate_additional_costs_html function.
* @deprecated 2.4.0
*
* @access public
* @return string
* Additonal rates defined like this:
* Option Name | Additional Cost [+- Percents%] | Per Cost Type (order, class, or item)
*/
public function generate_additional_costs_table_html() {
ob_start();
include( 'includes/html-extra-costs.php' );
return ob_get_clean();
}
public function calculate_extra_shipping( $method, $rate, $package ) {
if ( $this->options ) {
$options = array_filter( (array) explode( "\n", $this->options ) );
/**
* process_flat_rates function.
*/
public function process_flat_rates() {
$flat_rates = array();
$flat_rate_class = isset( $_POST[ $this->id . '_class'] ) ? array_map( 'wc_clean', $_POST[ $this->id . '_class'] ) : array();
for ( $i = 0; $i <= max( array_merge( array( 0 ), array_keys( $flat_rate_class ) ) ); $i ++ ) {
if ( empty( $flat_rate_class[ $i ] ) ) {
continue;
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 );
}
$cost = wc_format_decimal( $_POST[ $this->id . '_cost'][ $i ] );
$fee = wc_clean( $_POST[ $this->id . '_fee'][ $i ] );
if ( ! strstr( $fee, '%' ) ) {
$fee = wc_format_decimal( $fee );
}
$flat_rates[ urldecode( sanitize_title( $flat_rate_class[ $i ] ) ) ] = array(
'cost' => $cost,
'fee' => $fee
);
}
update_option( $this->flat_rate_option, $flat_rates );
$this->get_flat_rates();
}
/**
* save_default_costs function.
* Calculate the percentage adjustment for each shipping rate.
*
* @param array $fields
* @return array
* @deprecated 2.4.0
* @param float $cost
* @param float $percent_adjustment
* @param string $percent_operator
* @param float $base_price
* @return float
*/
public function save_default_costs( $fields ) {
$fields['cost'] = wc_format_decimal( $_POST['default_cost'] );
$fields['fee'] = wc_clean( $_POST['default_fee'] );
if ( ! strstr( $fields['fee'], '%' ) ) {
$fields['fee'] = wc_format_decimal( $fields['fee'] );
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 $fields;
return $cost;
}
/**
* get_flat_rates function.
* Get extra cost
*
* @deprecated 2.4.0
* @param string $cost_string
* @param string $type
* @param array $package
* @return float
*/
public function get_flat_rates() {
$this->flat_rates = array_filter( (array) get_option( $this->flat_rate_option ) );
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;
}
}

View File

@ -0,0 +1,308 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
/**
* Class WC_Eval_Math. Supports basic math only (removed eval function)
*
* Based on EvalMath by Miles Kaufman Copyright (C) 2005 Miles Kaufmann http://www.twmagic.com/
*/
class WC_Eval_Math {
/** @var bool */
public static $suppress_errors = false;
/** @var string */
public static $last_error = null;
/** @var array */
public static $v = array( 'e' => 2.71, 'pi' => 3.14 ); // variables (and constants)
/** @var array */
public static $f = array(); // user-defined functions
/** @var array */
public static $vb = array( 'e', 'pi' ); // constants
/** @var array */
public static $fb = array(); // built-in functions
/**
* Evaluate maths string
*
* @param string $expr
* @return mixed
*/
public static function evaluate( $expr ) {
self::$last_error = null;
$expr = trim( $expr );
if ( substr( $expr, -1, 1 ) == ';' ) $expr = substr( $expr, 0, strlen( $expr )-1 ); // strip semicolons at the end
//===============
// is it a variable assignment?
if ( preg_match( '/^\s*([a-z]\w*)\s*=\s*(.+)$/', $expr, $matches ) ) {
if ( in_array( $matches[1], self::$vb ) ) { // make sure we're not assigning to a constant
return self::trigger( "cannot assign to constant '$matches[1]'" );
}
if ( ( $tmp = self::pfx( self::nfx( $matches[2] ) ) ) === false ) return false; // get the result and make sure it's good
self::$v[$matches[1]] = $tmp; // if so, stick it in the variable array
return self::$v[$matches[1]]; // and return the resulting value
//===============
// is it a function assignment?
} elseif ( preg_match( '/^\s*([a-z]\w*)\s*\(\s*([a-z]\w*(?:\s*,\s*[a-z]\w*)*)\s*\)\s*=\s*(.+)$/', $expr, $matches ) ) {
$fnn = $matches[1]; // get the function name
if ( in_array( $matches[1], self::$fb ) ) { // make sure it isn't built in
return self::trigger( "cannot redefine built-in function '$matches[1]()'" );
}
$args = explode( ",", preg_replace( "/\s+/", "", $matches[2] ) ); // get the arguments
if ( ( $stack = self::nfx( $matches[3] ) ) === false ) return false; // see if it can be converted to postfix
for ( $i = 0; $i<count( $stack ); $i++ ) { // freeze the state of the non-argument variables
$token = $stack[$i];
if ( preg_match( '/^[a-z]\w*$/', $token ) and !in_array( $token, $args ) ) {
if ( array_key_exists( $token, self::$v ) ) {
$stack[$i] = self::$v[$token];
} else {
return self::trigger( "undefined variable '$token' in function definition" );
}
}
}
self::$f[$fnn] = array( 'args'=>$args, 'func'=>$stack );
return true;
//===============
} else {
return self::pfx( self::nfx( $expr ) ); // straight up evaluation, woo
}
}
// Convert infix to postfix notation
private static function nfx( $expr ) {
$index = 0;
$stack = new WC_Eval_Math_Stack;
$output = array(); // postfix form of expression, to be passed to pfx()
// $expr = trim(strtolower($expr));
$expr = trim( $expr );
$ops = array( '+', '-', '*', '/', '^', '_' );
$ops_r = array( '+'=>0, '-'=>0, '*'=>0, '/'=>0, '^'=>1 ); // right-associative operator?
$ops_p = array( '+'=>0, '-'=>0, '*'=>1, '/'=>1, '_'=>1, '^'=>2 ); // operator precedence
$expecting_op = false; // we use this in syntax-checking the expression
// and determining when a - is a negation
if ( preg_match( "/[^\w\s+*^\/()\.,-]/", $expr, $matches ) ) { // make sure the characters are all good
return self::trigger( "illegal character '{$matches[0]}'" );
}
while ( 1 ) { // 1 Infinite Loop ;)
$op = substr( $expr, $index, 1 ); // get the first character at the current index
// find out if we're currently at the beginning of a number/variable/function/parenthesis/operand
$ex = preg_match( '/^([A-Za-z]\w*\(?|\d+(?:\.\d*)?|\.\d+|\()/', substr( $expr, $index ), $match );
//===============
if ( $op == '-' and !$expecting_op ) { // is it a negation instead of a minus?
$stack->push( '_' ); // put a negation on the stack
$index++;
} elseif ( $op == '_' ) { // we have to explicitly deny this, because it's legal on the stack
return self::trigger( "illegal character '_'" ); // but not in the input expression
//===============
} elseif ( ( in_array( $op, $ops ) or $ex ) and $expecting_op ) { // are we putting an operator on the stack?
if ( $ex ) { // are we expecting an operator but have a number/variable/function/opening parethesis?
$op = '*'; $index--; // it's an implicit multiplication
}
// heart of the algorithm:
while ( $stack->count > 0 and ( $o2 = $stack->last() ) and in_array( $o2, $ops ) and ( $ops_r[$op] ? $ops_p[$op] < $ops_p[$o2] : $ops_p[$op] <= $ops_p[$o2] ) ) {
$output[] = $stack->pop(); // pop stuff off the stack into the output
}
// many thanks: http://en.wikipedia.org/wiki/Reverse_Polish_notation#The_algorithm_in_detail
$stack->push( $op ); // finally put OUR operator onto the stack
$index++;
$expecting_op = false;
//===============
} elseif ( $op == ')' and $expecting_op ) { // ready to close a parenthesis?
while ( ( $o2 = $stack->pop() ) != '(' ) { // pop off the stack back to the last (
if ( is_null( $o2 ) ) return self::trigger( "unexpected ')'" );
else $output[] = $o2;
}
if ( preg_match( "/^([A-Za-z]\w*)\($/", $stack->last( 2 ), $matches ) ) { // did we just close a function?
$fnn = $matches[1]; // get the function name
$arg_count = $stack->pop(); // see how many arguments there were (cleverly stored on the stack, thank you)
$output[] = $stack->pop(); // pop the function and push onto the output
if ( in_array( $fnn, self::$fb ) ) { // check the argument count
if ( $arg_count > 1 )
return self::trigger( "too many arguments ($arg_count given, 1 expected)" );
} elseif ( array_key_exists( $fnn, self::$f ) ) {
if ( $arg_count != count( self::$f[$fnn]['args'] ) )
return self::trigger( "wrong number of arguments ($arg_count given, " . count( self::$f[$fnn]['args'] ) . " expected)" );
} else { // did we somehow push a non-function on the stack? this should never happen
return self::trigger( "internal error" );
}
}
$index++;
//===============
} elseif ( $op == ',' and $expecting_op ) { // did we just finish a function argument?
while ( ( $o2 = $stack->pop() ) != '(' ) {
if ( is_null( $o2 ) ) return self::trigger( "unexpected ','" ); // oops, never had a (
else $output[] = $o2; // pop the argument expression stuff and push onto the output
}
// make sure there was a function
if ( !preg_match( "/^([A-Za-z]\w*)\($/", $stack->last( 2 ), $matches ) )
return self::trigger( "unexpected ','" );
$stack->push( $stack->pop()+1 ); // increment the argument count
$stack->push( '(' ); // put the ( back on, we'll need to pop back to it again
$index++;
$expecting_op = false;
//===============
} elseif ( $op == '(' and !$expecting_op ) {
$stack->push( '(' ); // that was easy
$index++;
$allow_neg = true;
//===============
} elseif ( $ex and !$expecting_op ) { // do we now have a function/variable/number?
$expecting_op = true;
$val = $match[1];
if ( preg_match( "/^([A-Za-z]\w*)\($/", $val, $matches ) ) { // may be func, or variable w/ implicit multiplication against parentheses...
if ( in_array( $matches[1], self::$fb ) or array_key_exists( $matches[1], self::$f ) ) { // it's a func
$stack->push( $val );
$stack->push( 1 );
$stack->push( '(' );
$expecting_op = false;
} else { // it's a var w/ implicit multiplication
$val = $matches[1];
$output[] = $val;
}
} else { // it's a plain old var or num
$output[] = $val;
}
$index += strlen( $val );
//===============
} elseif ( $op == ')' ) { // miscellaneous error checking
return self::trigger( "unexpected ')'" );
} elseif ( in_array( $op, $ops ) and !$expecting_op ) {
return self::trigger( "unexpected operator '$op'" );
} else { // I don't even want to know what you did to get here
return self::trigger( "an unexpected error occured" );
}
if ( $index == strlen( $expr ) ) {
if ( in_array( $op, $ops ) ) { // did we end with an operator? bad.
return self::trigger( "operator '$op' lacks operand" );
} else {
break;
}
}
while ( substr( $expr, $index, 1 ) == ' ' ) { // step the index past whitespace (pretty much turns whitespace
$index++; // into implicit multiplication if no operator is there)
}
}
while ( !is_null( $op = $stack->pop() ) ) { // pop everything off the stack and push onto output
if ( $op == '(' ) return self::trigger( "expecting ')'" ); // if there are (s on the stack, ()s were unbalanced
$output[] = $op;
}
return $output;
}
// evaluate postfix notation
private static function pfx( $tokens, $vars = array() ) {
if ( $tokens == false ) return false;
$stack = new WC_Eval_Math_Stack;
foreach ( $tokens as $token ) { // nice and easy
// if the token is a binary operator, pop two values off the stack, do the operation, and push the result back on
if ( in_array( $token, array( '+', '-', '*', '/', '^' ) ) ) {
if ( is_null( $op2 = $stack->pop() ) ) return self::trigger( "internal error" );
if ( is_null( $op1 = $stack->pop() ) ) return self::trigger( "internal error" );
switch ( $token ) {
case '+':
$stack->push( $op1+$op2 ); break;
case '-':
$stack->push( $op1-$op2 ); break;
case '*':
$stack->push( $op1*$op2 ); break;
case '/':
if ( $op2 == 0 ) return self::trigger( "division by zero" );
$stack->push( $op1/$op2 ); break;
case '^':
$stack->push( pow( $op1, $op2 ) ); break;
}
// if the token is a unary operator, pop one value off the stack, do the operation, and push it back on
} elseif ( $token == "_" ) {
$stack->push( -1*$stack->pop() );
// if the token is a function, pop arguments off the stack, hand them to the function, and push the result back on
} elseif ( preg_match( "/^([a-z]\w*)\($/", $token, $matches ) ) { // it's a function!
$fnn = $matches[1];
// if the token is a number or variable, push it on the stack
} else {
if ( is_numeric( $token ) ) {
$stack->push( $token );
} elseif ( array_key_exists( $token, self::$v ) ) {
$stack->push( self::$v[$token] );
} elseif ( array_key_exists( $token, $vars ) ) {
$stack->push( $vars[$token] );
} else {
return self::trigger( "undefined variable '$token'" );
}
}
}
// when we're out of tokens, the stack should have a single element, the final result
if ( $stack->count != 1 ) return self::trigger( "internal error" );
return $stack->pop();
}
// trigger an error, but nicely, if need be
private static function trigger( $msg ) {
self::$last_error = $msg;
if ( ! self::$suppress_errors ) {
echo "\nError found in:";
self::debugPrintCallingFunction();
trigger_error( $msg, E_USER_WARNING );
}
return false;
}
// Prints the file name, function name, and
// line number which called your function
// (not this function, then one that called
// it to begin with)
private static function debugPrintCallingFunction() {
$file = 'n/a';
$func = 'n/a';
$line = 'n/a';
$debugTrace = debug_backtrace();
if ( isset( $debugTrace[1] ) ) {
$file = $debugTrace[1]['file'] ? $debugTrace[1]['file'] : 'n/a';
$line = $debugTrace[1]['line'] ? $debugTrace[1]['line'] : 'n/a';
}
if ( isset( $debugTrace[2] ) ) $func = $debugTrace[2]['function'] ? $debugTrace[2]['function'] : 'n/a';
echo "\n$file, $func, $line\n";
}
}
/**
* Class WC_Eval_Math_Stack
*/
class WC_Eval_Math_Stack {
/** @var array */
public $stack = array();
/** @var integer */
public $count = 0;
public function push( $val ) {
$this->stack[ $this->count ] = $val;
$this->count++;
}
public function pop() {
if ( $this->count > 0 ) {
$this->count--;
return $this->stack[ $this->count ];
}
return null;
}
public function last( $n=1 ) {
$key = $this->count - $n;
return array_key_exists( $key, $this->stack ) ? $this->stack[ $key ] : null;
}
}

View File

@ -1,105 +0,0 @@
<?php
/**
* Admin View: Extra costs table
*/
if ( ! defined( 'ABSPATH' ) ) {
exit;
}
?>
<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>
<tbody class="flat_rates">
<tr>
<td></td>
<td class="flat_rate_class"><?php _e( 'Any class', 'woocommerce' ); ?></td>
<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>
</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">';
if ( WC()->shipping->get_shipping_classes() ) {
foreach ( WC()->shipping->get_shipping_classes() as $shipping_class ) {
echo '<option value="' . esc_attr( $shipping_class->slug ) . '" '.selected($shipping_class->slug, $class, false).'>'.$shipping_class->name.'</option>';
}
} else {
echo '<option value="">'.__( 'Select a class&hellip;', 'woocommerce' ).'</option>';
}
echo '</select>
</td>
<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>
</tr>';
}
}
?>
</tbody>
<tfoot>
<tr>
<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>
</tr>
</tfoot>
</table>
<script type="text/javascript">
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>\
<td class="flat_rate_class">\
<select name="<?php echo $this->id; ?>_class[' + size + ']" class="select">\
<?php
if (WC()->shipping->get_shipping_classes()) :
foreach (WC()->shipping->get_shipping_classes() as $class) :
echo '<option value="' . esc_attr( $class->slug ) . '">' . esc_js( $class->name ) . '</option>';
endforeach;
else :
echo '<option value="">'.__( 'Select a class&hellip;', 'woocommerce' ).'</option>';
endif;
?>\
</select>\
</td>\
<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>\
</tr>').appendTo('#<?php echo $this->id; ?>_flat_rates table tbody');
return false;
});
// Remove row
jQuery('#<?php echo $this->id; ?>_flat_rates').on( 'click', 'a.remove', function(){
var answer = confirm("<?php _e( 'Delete the selected rates?', 'woocommerce' ); ?>");
if (answer) {
jQuery('#<?php echo $this->id; ?>_flat_rates table tbody tr th.check-column input:checked').each(function(i, el){
jQuery(el).closest('tr').remove();
});
}
return false;
});
});
</script>
</td>
</tr>

View File

@ -4,10 +4,12 @@ if ( ! defined( 'ABSPATH' ) ) {
exit;
}
$cost_desc = __( 'Enter a cost (excl. tax) or sum, e.g. <code>10 * [qty]</code>.', 'woocommerce' ) . '<br/>' . __( 'Supports the following placeholders: <code>[qty]</code> = number of items, <code>[cost]</code> = cost of items, <code>[fee percent="10" min="20"]</code> = Percentage based fee.', 'woocommerce' );
/**
* Settings for flat rate shipping
*/
return array(
$settings = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'woocommerce' ),
'type' => 'checkbox',
@ -50,54 +52,68 @@ return array(
'options' => array(
'taxable' => __( 'Taxable', 'woocommerce' ),
'none' => _x( 'None', 'Tax status', 'woocommerce' )
),
)
),
'cost_per_order' => array(
'title' => __( 'Cost per order', 'woocommerce' ),
'type' => 'price',
'placeholder' => wc_format_localized_price( 0 ),
'description' => __( 'Enter a cost (excluding tax) per order, e.g. 5.00. Default is 0.', 'woocommerce' ),
'cost' => array(
'title' => __( 'Cost', 'woocommerce' ),
'type' => 'text',
'placeholder' => '',
'description' => $cost_desc,
'default' => '',
'desc_tip' => true
),
'additional_costs' => array(
'title' => __( 'Additional Costs', 'woocommerce' ),
)
);
if ( WC()->shipping->get_shipping_classes() ) {
$settings[ 'class_costs' ] = array(
'title' => __( 'Shipping Class Costs', 'woocommerce' ),
'type' => 'title',
'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' ),
'description' => sprintf( __( 'These costs can optionally be added based on the %sproduct shipping class%s.', 'woocommerce' ), '<a href="' . admin_url( 'edit-tags.php?taxonomy=product_shipping_class&post_type=product' ) . '">', '</a>' )
);
foreach ( WC()->shipping->get_shipping_classes() as $shipping_class ) {
$settings[ 'class_cost_' . $shipping_class->slug ] = array(
'title' => sprintf( __( '"%s" Shipping Class Cost', 'woocommerce' ), esc_html( $shipping_class->name ) ),
'type' => 'text',
'placeholder' => __( 'N/A', 'woocommerce' ),
'description' => $cost_desc,
'default' => '',
'desc_tip' => true
);
}
$settings[ 'no_class_cost' ] = array(
'title' => __( 'No Shipping Class Cost', 'woocommerce' ),
'type' => 'text',
'placeholder' => __( 'N/A', 'woocommerce' ),
'description' => $cost_desc,
'default' => '',
'desc_tip' => true
);
$settings[ 'type' ] = array(
'title' => __( 'Calculation Type', 'woocommerce' ),
'type' => 'select',
'class' => 'wc-enhanced-select',
'default' => 'order',
'default' => 'class',
'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' ),
'class' => __( 'Per Class: Charge shipping for each shipping class individually', 'woocommerce' ),
'order' => __( 'Per Order: Charge shipping for the most expensive shipping class', 'woocommerce' ),
),
),
'additional_costs_table' => array(
'type' => 'additional_costs_table'
),
'minimum_fee' => array(
'title' => __( 'Minimum Handling Fee', 'woocommerce' ),
'type' => 'price',
'placeholder' => wc_format_localized_price( 0 ),
'description' => __( 'Enter a minimum fee amount. Fee\'s less than this will be increased. Leave blank to disable.', 'woocommerce' ),
'default' => '',
'desc_tip' => true
),
'addons' => array(
'title' => __( 'Add-on Rates', 'woocommerce' ),
);
}
if ( $this->get_option( 'options', false ) ) {
$settings[ 'additional_rates' ] = array(
'title' => __( 'Additional Rates', 'woocommerce' ),
'type' => 'title',
'description' => __( 'Add-on rates are extra shipping options with additional costs (based on the flat rate).', 'woocommerce' )
),
'options' => array(
'title' => __( 'Rates', 'woocommerce' ),
'description' => __( 'These rates are extra shipping options with additional costs (based on the flat rate).', 'woocommerce' ),
);
$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;

View File

@ -1,44 +1,30 @@
<?php
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
exit;
}
if ( ! class_exists( 'WC_Shipping_International_Delivery' ) ) :
/**
* International Shipping Method based on Flat Rate shipping
* International Delivery - Based on the Flat Rate Shipping Method
*
* A simple shipping method for a flat fee per item or per order.
*
* @class WC_Shipping_International_Delivery
* @version 2.3.0
* @class WC_Shipping_Flat_Rate
* @version 2.4.0
* @package WooCommerce/Classes/Shipping
* @author WooThemes
*/
class WC_Shipping_International_Delivery extends WC_Shipping_Flat_Rate {
public $id = 'international_delivery';
/**
* __construct function.
*
* @access public
* @return void
* Constructor
*/
public function __construct() {
$this->id = 'international_delivery';
$this->flat_rate_option = 'woocommerce_international_delivery_flat_rates';
$this->method_title = __( 'International Delivery', 'woocommerce' );
$this->method_description = __( 'International delivery based on flat rate shipping.', 'woocommerce' );
$this->id = 'international_delivery';
$this->method_title = __( 'International Flat Rate', 'woocommerce' );
$this->method_description = __( 'International Flat Rate Shipping lets you charge a fixed rate for shipping.', 'woocommerce' );
$this->init();
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' ) );
$this->init();
}
/**
* Initialise Gateway Settings Form Fields
*
@ -46,174 +32,39 @@ class WC_Shipping_International_Delivery extends WC_Shipping_Flat_Rate {
* @return void
*/
public function init_form_fields() {
$this->form_fields = array(
'enabled' => array(
'title' => __( 'Enable/Disable', 'woocommerce' ),
'type' => 'checkbox',
'label' => __( 'Enable this shipping method', 'woocommerce' ),
'default' => 'no'
),
'title' => array(
'title' => __( 'Method Title', 'woocommerce' ),
'type' => 'text',
'description' => __( 'This controls the title which the user sees during checkout.', 'woocommerce' ),
'default' => __( 'International Delivery', 'woocommerce' ),
'desc_tip' => true,
),
'availability' => array(
'title' => __( 'Availability', 'woocommerce' ),
'type' => 'select',
'class' => 'wc-enhanced-select',
'description' => '',
'default' => 'including',
'options' => array(
'including' => __( 'Selected countries', 'woocommerce' ),
'excluding' => __( 'Excluding selected countries', 'woocommerce' ),
)
),
'countries' => array(
'title' => __( 'Countries', 'woocommerce' ),
'type' => 'multiselect',
'class' => 'wc-enhanced-select',
'css' => 'width: 450px;',
'default' => '',
'options' => WC()->countries->get_shipping_countries(),
'custom_attributes' => array(
'data-placeholder' => __( 'Select some countries', 'woocommerce' )
)
),
'tax_status' => array(
'title' => __( 'Tax Status', 'woocommerce' ),
'type' => 'select',
'class' => 'wc-enhanced-select',
'default' => 'taxable',
'options' => array(
'taxable' => __( 'Taxable', 'woocommerce' ),
'none' => _x( 'None', 'Tax status', 'woocommerce' )
)
),
'type' => array(
'title' => __( 'Cost Added...', 'woocommerce' ),
'type' => 'select',
'class' => 'wc-enhanced-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' ),
),
),
'cost' => array(
'title' => __( 'Cost', 'woocommerce' ),
'type' => 'price',
'placeholder' => wc_format_localized_price( 0 ),
'description' => __( 'Cost excluding tax. Enter an amount, e.g. 2.50. Default is 0', 'woocommerce' ),
'default' => '',
'desc_tip' => true
),
'fee' => array(
'title' => __( 'Handling Fee', 'woocommerce' ),
'type' => 'text',
'description' => __( 'Fee excluding tax. Enter an amount, e.g. 2.50, or a percentage, e.g. 5%. Leave blank to disable.', 'woocommerce' ),
'default' => '',
'desc_tip' => true,
'placeholder' => __( 'N/A', 'woocommerce' )
),
'minimum_fee' => array(
'title' => __( 'Minimum Handling Fee', 'woocommerce' ),
'type' => 'decimal',
'description' => __( 'Enter a minimum fee amount. Fee\'s less than this will be increased. Leave blank to disable.', 'woocommerce' ),
'default' => '',
'desc_tip' => true,
'placeholder' => __( 'N/A', 'woocommerce' )
),
);
parent::init_form_fields();
$this->form_fields['availability'] = array(
'title' => __( 'Availability', 'woocommerce' ),
'type' => 'select',
'class' => 'wc-enhanced-select',
'description' => '',
'default' => 'including',
'options' => array(
'including' => __( 'Selected countries', 'woocommerce' ),
'excluding' => __( 'Excluding selected countries', 'woocommerce' ),
)
);
}
/**
* is_available function.
*
* @access public
* @param mixed $package
* @param array $package
* @return bool
*/
public function is_available( $package ) {
if ( "no" === $this->enabled ) {
return false;
}
if ( 'including' === $this->availability ) {
if ( is_array( $this->countries ) && ! in_array( $package['destination']['country'], $this->countries ) ) {
return false;
}
} else {
if ( is_array( $this->countries ) && ( in_array( $package['destination']['country'], $this->countries ) || ! $package['destination']['country'] ) ) {
return false;
}
}
return apply_filters( 'woocommerce_shipping_' . $this->id . '_is_available', true, $package );
}
/**
* calculate_shipping function.
*
* @access public
* @param array $package (default: array())
* @return void
*/
public function calculate_shipping( $package = array() ) {
$this->rates = array();
if ( 'order' === $this->type ) {
$shipping_total = $this->order_shipping( $package );
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => $shipping_total ? $shipping_total : 0,
);
} elseif ( 'class' === $this->type ) {
$shipping_total = $this->class_shipping( $package );
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => $shipping_total ? $shipping_total : 0,
);
} elseif ( 'item' === $this->type ) {
$costs = $this->item_shipping( $package );
if ( ! is_array( $costs ) ) {
$costs = array();
}
$rate = array(
'id' => $this->id,
'label' => $this->title,
'cost' => $costs,
'calc_tax' => 'per_item',
);
}
if ( isset( $rate ) ) {
$this->add_rate( $rate );
}
}
}
endif;

View File

@ -14,9 +14,72 @@ if ( ! defined( 'ABSPATH' ) ) {
global $wpdb;
// Maintain the old coupon logic for upgrades
/**
* Coupon discount calculations
* Maintain the old coupon logic for upgrades
*/
update_option( 'woocommerce_calc_discounts_sequentially', 'yes' );
/**
* Flat Rate Shipping
* Update legacy options to new math based options.
*/
$shipping_methods = array(
'woocommerce_flat_rates' => new WC_Shipping_Flat_Rate(),
'woocommerce_international_delivery_flat_rates' => new WC_Shipping_International_Delivery()
);
foreach ( $shipping_methods as $flat_rate_option_key => $shipping_method ) {
// Stop this running more than once if routine is repeated
if ( version_compare( $shipping_method->get_option( 'version', 0 ), '2.4.0', '<' ) ) {
$has_classes = sizeof( WC()->shipping->get_shipping_classes() ) > 0;
$cost_key = $has_classes ? 'no_class_cost' : 'cost';
$min_fee = $shipping_method->get_option( 'minimum_fee' );
$math_cost_strings = array( 'cost' => array(), 'no_class_cost' => array() );
$math_cost_strings[ $cost_key ][] = $shipping_method->get_option( 'cost' );
if ( $fee = $shipping_method->get_option( 'fee' ) ) {
$math_cost_strings[ $cost_key ][] = strstr( $fee, '%' ) ? '[fee percent="' . str_replace( '%', '', $fee ) . '" min="' . esc_attr( $min_fee ) . '"]' : $fee;
}
foreach ( WC()->shipping->get_shipping_classes() as $shipping_class ) {
$rate_key = 'class_cost_' . $shipping_class->slug;
$math_cost_strings[ $rate_key ] = $math_cost_strings[ 'no_class_cost' ];
}
if ( $flat_rates = array_filter( (array) get_option( $flat_rate_option_key, array() ) ) ) {
foreach ( $flat_rates as $shipping_class => $rate ) {
$rate_key = 'class_cost_' . $shipping_class;
if ( $rate['cost'] || $rate['fee'] ) {
$math_cost_strings[ $rate_key ][] = $rate['cost'];
$math_cost_strings[ $rate_key ][] = strstr( $rate['fee'], '%' ) ? '[fee percent="' . str_replace( '%', '', $rate['fee'] ) . '" min="' . esc_attr( $min_fee ) . '"]' : $rate['fee'];
}
}
}
if ( 'item' === $shipping_method->type ) {
foreach ( $math_cost_strings as $key => $math_cost_string ) {
$math_cost_strings[ $key ] = array_filter( $math_cost_strings[ $key ] );
if ( $math_cost_strings[ $key ] ) {
$math_cost_strings[ $key ][0] = '( ' . $math_cost_strings[ $key ][0];
$math_cost_strings[ $key ][ sizeof( $math_cost_strings[ $key ] ) - 1 ] .= ' ) * [qty]';
}
}
}
$math_cost_strings[ 'cost' ][] = $shipping_method->get_option( 'cost_per_order' );
// Save settings
foreach ( $math_cost_strings as $option_id => $math_cost_string ) {
$shipping_method->settings[ $option_id ] = implode( ' + ', $math_cost_string );
}
$shipping_method->settings['version'] = '2.4.0';
$shipping_method->settings['type'] = 'item' === $shipping_method->settings['type'] ? 'class' : $shipping_method->settings['type'];
update_option( $shipping_method->plugin_id . $shipping_method->id . '_settings', $shipping_method->settings );
}
}
/**
* Update the old user API keys to the new Apps keys
*/
@ -58,7 +121,10 @@ if ( ! empty( $apps_keys ) ) {
}
}
// Make sure order.update webhooks get the woocommerce_order_edit_status hook
/**
* Webhooks
* Make sure order.update webhooks get the woocommerce_order_edit_status hook
*/
$order_update_webhooks = get_posts( array(
'posts_per_page' => -1,
'post_type' => 'shop_webhook',
@ -70,7 +136,10 @@ foreach ( $order_update_webhooks as $order_update_webhook ) {
$webhook->set_topic( 'order.updated' );
}
// Update fully refunded orders to ensure they have a refund line item so reports add up
/**
* Refunds for full refunded orders.
* Update fully refunded orders to ensure they have a refund line item so reports add up.
*/
$refunded_orders = get_posts( array(
'posts_per_page' => -1,
'post_type' => 'shop_order',