2011-08-10 17:11:11 +00:00
|
|
|
<?php
|
2014-09-20 19:44:32 +00:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
2015-06-12 15:19:43 +00:00
|
|
|
exit;
|
2014-09-20 19:44:32 +00:00
|
|
|
}
|
2013-02-20 17:14:46 +00:00
|
|
|
|
2011-08-10 17:11:11 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Flat Rate Shipping Method.
|
2012-08-14 22:43:48 +00:00
|
|
|
*
|
2012-12-31 18:25:09 +00:00
|
|
|
* @class WC_Shipping_Flat_Rate
|
2015-12-16 16:09:52 +00:00
|
|
|
* @version 2.6.0
|
2012-08-14 22:43:48 +00:00
|
|
|
* @package WooCommerce/Classes/Shipping
|
|
|
|
* @author WooThemes
|
|
|
|
*/
|
2012-12-31 18:25:09 +00:00
|
|
|
class WC_Shipping_Flat_Rate extends WC_Shipping_Method {
|
2012-08-14 22:43:48 +00:00
|
|
|
|
2015-06-16 10:37:58 +00:00
|
|
|
/** @var string cost passed to [fee] shortcode */
|
|
|
|
protected $fee_cost = '';
|
2015-06-15 14:24:51 +00:00
|
|
|
|
2014-11-19 23:28:18 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Constructor.
|
2012-08-14 22:43:48 +00:00
|
|
|
*/
|
2015-12-16 15:16:52 +00:00
|
|
|
public function __construct( $instance_id = 0 ) {
|
|
|
|
$this->id = 'flat_rate';
|
|
|
|
$this->instance_id = absint( $instance_id );
|
|
|
|
$this->method_title = __( 'Flat Rate', 'woocommerce' );
|
2015-12-18 14:24:34 +00:00
|
|
|
$this->method_description = __( 'Lets you charge a fixed rate for shipping.', 'woocommerce' );
|
2015-12-16 15:16:52 +00:00
|
|
|
$this->supports = array(
|
2015-12-16 13:52:46 +00:00
|
|
|
'shipping-zones',
|
2016-03-24 18:31:39 +00:00
|
|
|
'instance-settings',
|
|
|
|
'instance-settings-modal',
|
2015-12-16 13:52:46 +00:00
|
|
|
);
|
2015-06-16 10:37:58 +00:00
|
|
|
$this->init();
|
2012-04-13 11:16:24 +00:00
|
|
|
|
2012-12-15 11:53:32 +00:00
|
|
|
add_action( 'woocommerce_update_options_shipping_' . $this->id, array( $this, 'process_admin_options' ) );
|
2013-08-12 21:09:04 +00:00
|
|
|
}
|
2012-08-14 22:43:48 +00:00
|
|
|
|
2013-08-12 21:09:04 +00:00
|
|
|
/**
|
2015-12-16 13:52:46 +00:00
|
|
|
* init user set variables.
|
2013-08-12 21:09:04 +00:00
|
|
|
*/
|
2014-11-19 22:22:32 +00:00
|
|
|
public function init() {
|
2015-12-16 13:52:46 +00:00
|
|
|
$this->instance_form_fields = include( 'includes/settings-flat-rate.php' );
|
|
|
|
$this->title = $this->get_option( 'title' );
|
|
|
|
$this->tax_status = $this->get_option( 'tax_status' );
|
|
|
|
$this->cost = $this->get_option( 'cost' );
|
|
|
|
$this->type = $this->get_option( 'type', 'class' );
|
2013-08-12 21:09:04 +00:00
|
|
|
}
|
2012-08-14 22:43:48 +00:00
|
|
|
|
2015-06-12 15:19:43 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Evaluate a cost from a sum/string.
|
2015-06-12 15:19:43 +00:00
|
|
|
* @param string $sum
|
|
|
|
* @param array $args
|
|
|
|
* @return string
|
|
|
|
*/
|
2015-06-16 13:08:46 +00:00
|
|
|
protected function evaluate_cost( $sum, $args = array() ) {
|
2016-06-06 16:06:26 +00:00
|
|
|
include_once( WC()->plugin_path() . '/includes/libraries/class-wc-eval-math.php' );
|
2015-06-12 15:19:43 +00:00
|
|
|
|
2016-01-11 13:51:46 +00:00
|
|
|
// Allow 3rd parties to process shipping cost arguments
|
2016-01-13 13:35:24 +00:00
|
|
|
$args = apply_filters( 'woocommerce_evaluate_shipping_cost_args', $args, $sum, $this );
|
2016-01-11 15:51:37 +00:00
|
|
|
$locale = localeconv();
|
2016-07-19 14:52:29 +00:00
|
|
|
$decimals = array( wc_get_price_decimal_separator(), $locale['decimal_point'], $locale['mon_decimal_point'], ',' );
|
2015-06-15 14:24:51 +00:00
|
|
|
$this->fee_cost = $args['cost'];
|
|
|
|
|
2015-08-24 14:26:08 +00:00
|
|
|
// Expand shortcodes
|
|
|
|
add_shortcode( 'fee', array( $this, 'fee' ) );
|
|
|
|
|
|
|
|
$sum = do_shortcode( str_replace(
|
2015-06-12 15:19:43 +00:00
|
|
|
array(
|
|
|
|
'[qty]',
|
|
|
|
'[cost]'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
$args['qty'],
|
|
|
|
$args['cost']
|
|
|
|
),
|
|
|
|
$sum
|
2015-08-24 14:26:08 +00:00
|
|
|
) );
|
2015-06-12 15:19:43 +00:00
|
|
|
|
2015-06-15 14:24:51 +00:00
|
|
|
remove_shortcode( 'fee', array( $this, 'fee' ) );
|
|
|
|
|
2015-08-24 14:26:08 +00:00
|
|
|
// Remove whitespace from string
|
|
|
|
$sum = preg_replace( '/\s+/', '', $sum );
|
|
|
|
|
|
|
|
// Remove locale from string
|
|
|
|
$sum = str_replace( $decimals, '.', $sum );
|
|
|
|
|
|
|
|
// Trim invalid start/end characters
|
|
|
|
$sum = rtrim( ltrim( $sum, "\t\n\r\0\x0B+*/" ), "\t\n\r\0\x0B+-*/" );
|
|
|
|
|
|
|
|
// Do the math
|
2015-06-15 14:24:51 +00:00
|
|
|
return $sum ? WC_Eval_Math::evaluate( $sum ) : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Work out fee (shortcode).
|
2015-06-15 14:24:51 +00:00
|
|
|
* @param array $atts
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function fee( $atts ) {
|
|
|
|
$atts = shortcode_atts( array(
|
|
|
|
'percent' => '',
|
2016-06-16 11:23:02 +00:00
|
|
|
'min_fee' => '',
|
|
|
|
'max_fee' => '',
|
2015-06-15 14:24:51 +00:00
|
|
|
), $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'];
|
|
|
|
}
|
|
|
|
|
2016-06-16 11:23:02 +00:00
|
|
|
if ( $atts['max_fee'] && $calculated_fee > $atts['max_fee'] ) {
|
|
|
|
$calculated_fee = $atts['max_fee'];
|
|
|
|
}
|
|
|
|
|
2015-06-15 14:24:51 +00:00
|
|
|
return $calculated_fee;
|
2015-06-12 15:19:43 +00:00
|
|
|
}
|
|
|
|
|
2013-08-12 21:09:04 +00:00
|
|
|
/**
|
|
|
|
* calculate_shipping function.
|
|
|
|
*
|
|
|
|
* @param array $package (default: array())
|
|
|
|
*/
|
2014-11-19 22:22:32 +00:00
|
|
|
public function calculate_shipping( $package = array() ) {
|
2015-06-12 15:19:43 +00:00
|
|
|
$rate = array(
|
2016-04-25 16:06:35 +00:00
|
|
|
'id' => $this->get_rate_id(),
|
2016-04-20 14:03:10 +00:00
|
|
|
'label' => $this->title,
|
|
|
|
'cost' => 0,
|
|
|
|
'package' => $package,
|
2014-11-19 22:22:32 +00:00
|
|
|
);
|
|
|
|
|
2015-06-12 15:19:43 +00:00
|
|
|
// Calculate the costs
|
2015-06-16 13:07:28 +00:00
|
|
|
$has_costs = false; // True when a cost is set. False if all costs are blank strings.
|
|
|
|
$cost = $this->get_option( 'cost' );
|
|
|
|
|
|
|
|
if ( $cost !== '' ) {
|
|
|
|
$has_costs = true;
|
2015-06-16 13:08:46 +00:00
|
|
|
$rate['cost'] = $this->evaluate_cost( $cost, array(
|
2015-06-16 13:07:28 +00:00
|
|
|
'qty' => $this->get_package_item_qty( $package ),
|
2016-04-20 14:03:10 +00:00
|
|
|
'cost' => $package['contents_cost'],
|
2015-06-16 13:07:28 +00:00
|
|
|
) );
|
|
|
|
}
|
2014-11-19 22:22:32 +00:00
|
|
|
|
2015-06-12 15:19:43 +00:00
|
|
|
// Add shipping class costs
|
|
|
|
$found_shipping_classes = $this->find_shipping_classes( $package );
|
2015-06-15 14:24:51 +00:00
|
|
|
$highest_class_cost = 0;
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2015-06-12 15:19:43 +00:00
|
|
|
foreach ( $found_shipping_classes as $shipping_class => $products ) {
|
2015-11-30 11:15:16 +00:00
|
|
|
// Also handles BW compatibility when slugs were used instead of ids
|
|
|
|
$shipping_class_term = get_term_by( 'slug', $shipping_class, 'product_shipping_class' );
|
2015-11-30 12:15:49 +00:00
|
|
|
$class_cost_string = $shipping_class_term && $shipping_class_term->term_id ? $this->get_option( 'class_cost_' . $shipping_class_term->term_id, $this->get_option( 'class_cost_' . $shipping_class, '' ) ) : $this->get_option( 'no_class_cost', '' );
|
2015-06-15 14:24:51 +00:00
|
|
|
|
2015-06-16 13:07:28 +00:00
|
|
|
if ( $class_cost_string === '' ) {
|
2015-06-15 14:24:51 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-06-16 13:07:28 +00:00
|
|
|
$has_costs = true;
|
2015-06-16 13:08:46 +00:00
|
|
|
$class_cost = $this->evaluate_cost( $class_cost_string, array(
|
2015-06-15 14:24:51 +00:00
|
|
|
'qty' => array_sum( wp_list_pluck( $products, 'quantity' ) ),
|
|
|
|
'cost' => array_sum( wp_list_pluck( $products, 'line_total' ) )
|
|
|
|
) );
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2015-06-15 14:24:51 +00:00
|
|
|
if ( $this->type === 'class' ) {
|
|
|
|
$rate['cost'] += $class_cost;
|
|
|
|
} else {
|
|
|
|
$highest_class_cost = $class_cost > $highest_class_cost ? $class_cost : $highest_class_cost;
|
2014-02-24 17:17:33 +00:00
|
|
|
}
|
2014-11-19 22:22:32 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2015-06-15 14:24:51 +00:00
|
|
|
if ( $this->type === 'order' && $highest_class_cost ) {
|
|
|
|
$rate['cost'] += $highest_class_cost;
|
|
|
|
}
|
|
|
|
|
2015-06-12 15:19:43 +00:00
|
|
|
// Add the rate
|
2015-06-16 13:07:28 +00:00
|
|
|
if ( $has_costs ) {
|
2016-04-20 07:59:19 +00:00
|
|
|
$this->add_rate( $rate );
|
2015-06-16 13:07:28 +00:00
|
|
|
}
|
2015-06-12 15:19:43 +00:00
|
|
|
|
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Developers can add additional flat rates based on this one via this action since @version 2.4.
|
2015-06-12 15:19:43 +00:00
|
|
|
*
|
2015-11-03 13:31:20 +00:00
|
|
|
* Previously there were (overly complex) options to add additional rates however this was not user.
|
2015-06-12 15:19:43 +00:00
|
|
|
* 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;
|
2015-11-03 13:31:20 +00:00
|
|
|
* $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.
|
2015-06-12 15:19:43 +00:00
|
|
|
*
|
2015-11-03 13:31:20 +00:00
|
|
|
* // Add it to WC.
|
2015-06-12 15:19:43 +00:00
|
|
|
* $method->add_rate( $new_rate );
|
2015-11-03 13:31:20 +00:00
|
|
|
* }.
|
2015-06-12 15:19:43 +00:00
|
|
|
*/
|
2016-04-20 07:59:19 +00:00
|
|
|
do_action( 'woocommerce_' . $this->id . '_shipping_add_rate', $this, $rate );
|
2014-11-19 22:22:32 +00:00
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2014-11-19 22:22:32 +00:00
|
|
|
/**
|
2015-11-03 13:31:20 +00:00
|
|
|
* Get items in package.
|
2014-11-19 22:22:32 +00:00
|
|
|
* @param array $package
|
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
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'];
|
2014-02-24 17:17:33 +00:00
|
|
|
}
|
2014-11-19 22:22:32 +00:00
|
|
|
}
|
|
|
|
return $total_quantity;
|
|
|
|
}
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2013-08-12 21:09:04 +00:00
|
|
|
/**
|
|
|
|
* Finds and returns shipping classes and the products with said class.
|
|
|
|
* @param mixed $package
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function find_shipping_classes( $package ) {
|
|
|
|
$found_shipping_classes = array();
|
2013-04-16 12:01:55 +00:00
|
|
|
|
2014-11-19 22:22:32 +00:00
|
|
|
foreach ( $package['contents'] as $item_id => $values ) {
|
|
|
|
if ( $values['data']->needs_shipping() ) {
|
|
|
|
$found_class = $values['data']->get_shipping_class();
|
2013-04-16 12:01:55 +00:00
|
|
|
|
2014-11-19 22:22:32 +00:00
|
|
|
if ( ! isset( $found_shipping_classes[ $found_class ] ) ) {
|
|
|
|
$found_shipping_classes[ $found_class ] = array();
|
2013-04-16 12:01:55 +00:00
|
|
|
}
|
2014-11-19 22:22:32 +00:00
|
|
|
|
|
|
|
$found_shipping_classes[ $found_class ][ $item_id ] = $values;
|
2013-04-16 12:01:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $found_shipping_classes;
|
2013-08-12 21:09:04 +00:00
|
|
|
}
|
2013-08-25 11:59:58 +00:00
|
|
|
}
|