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
|
|
|
/**
|
|
|
|
* 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-06-12 15:19:43 +00:00
|
|
|
* @version 2.4.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
|
|
|
|
2014-11-19 23:28:18 +00:00
|
|
|
/**
|
2015-06-12 15:19:43 +00:00
|
|
|
* Constructor
|
2012-08-14 22:43:48 +00:00
|
|
|
*/
|
2014-11-19 22:22:32 +00:00
|
|
|
public function __construct() {
|
2015-06-12 15:19:43 +00:00
|
|
|
$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' );
|
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' ) );
|
2012-04-13 11:16:24 +00:00
|
|
|
|
2013-08-12 21:09:04 +00:00
|
|
|
$this->init();
|
|
|
|
}
|
2012-08-14 22:43:48 +00:00
|
|
|
|
2013-08-12 21:09:04 +00:00
|
|
|
/**
|
|
|
|
* init function.
|
|
|
|
*/
|
2014-11-19 22:22:32 +00:00
|
|
|
public function init() {
|
2011-11-28 15:50:19 +00:00
|
|
|
// Load the settings.
|
2013-01-02 13:38:33 +00:00
|
|
|
$this->init_form_fields();
|
2011-11-28 15:50:19 +00:00
|
|
|
$this->init_settings();
|
2012-08-14 22:43:48 +00:00
|
|
|
|
2011-11-28 15:50:19 +00:00
|
|
|
// Define user set variables
|
2015-06-12 15:19:43 +00:00
|
|
|
$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' );
|
2013-08-12 21:09:04 +00:00
|
|
|
}
|
2011-11-28 15:50:19 +00:00
|
|
|
|
2013-08-12 21:09:04 +00:00
|
|
|
/**
|
2015-06-12 15:19:43 +00:00
|
|
|
* Initialise Settings Form Fields
|
2013-08-12 21:09:04 +00:00
|
|
|
*/
|
2014-11-19 22:22:32 +00:00
|
|
|
public function init_form_fields() {
|
|
|
|
$this->form_fields = include( 'includes/settings-flat-rate.php' );
|
2013-08-12 21:09:04 +00:00
|
|
|
}
|
2012-08-14 22:43:48 +00:00
|
|
|
|
2015-06-12 15:19:43 +00:00
|
|
|
/**
|
|
|
|
* Evaluate a cost from a sum/string
|
|
|
|
* @param string $sum
|
|
|
|
* @param array $args
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
protected function evalulate_cost( $sum, $args = array() ) {
|
|
|
|
include_once( 'includes/class-wc-eval-math.php' );
|
|
|
|
|
|
|
|
$sum = str_replace(
|
|
|
|
array(
|
|
|
|
'[qty]',
|
|
|
|
'[cost]'
|
|
|
|
),
|
|
|
|
array(
|
|
|
|
$args['qty'],
|
|
|
|
$args['cost']
|
|
|
|
),
|
|
|
|
$sum
|
|
|
|
);
|
|
|
|
|
|
|
|
$m = new WC_Eval_Math;
|
|
|
|
return $m->evaluate( $sum );
|
|
|
|
}
|
|
|
|
|
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(
|
2014-11-19 22:22:32 +00:00
|
|
|
'id' => $this->id,
|
|
|
|
'label' => $this->title,
|
|
|
|
'cost' => 0,
|
|
|
|
);
|
|
|
|
|
2015-06-12 15:19:43 +00:00
|
|
|
// Calculate the costs
|
|
|
|
$rate['cost'] = $this->evalulate_cost( $this->get_option( 'cost' ), array(
|
|
|
|
'qty' => $this->get_package_item_qty( $package ),
|
|
|
|
'cost' => $package['contents_cost']
|
|
|
|
) );
|
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 );
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2015-06-12 15:19:43 +00:00
|
|
|
foreach ( $found_shipping_classes as $shipping_class => $products ) {
|
|
|
|
$class_cost = $this->get_option( 'class_cost_' . $shipping_class, '' );
|
2012-11-27 16:22:47 +00:00
|
|
|
|
2015-06-12 15:19:43 +00:00
|
|
|
if ( $class_cost ) {
|
|
|
|
$rate['cost'] += $this->evalulate_cost( $class_cost, array(
|
|
|
|
'qty' => array_sum( wp_list_pluck( $products, 'quantity' ) ),
|
|
|
|
'cost' => array_sum( wp_list_pluck( $products, 'line_total' ) )
|
|
|
|
) );
|
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-12 15:19:43 +00:00
|
|
|
// Add the rate
|
|
|
|
$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_flat_rate_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
|
|
|
/**
|
|
|
|
* Get items in package
|
|
|
|
* @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
|
|
|
}
|