From 06587191f0ffcc473317994a36a2cf9bf9ccb441 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Wed, 29 Feb 2012 11:49:39 +0000 Subject: [PATCH] Flat rate addons --- classes/shipping/class-wc-flat-rate.php | 53 ++++++++++++++++++- classes/shipping/class-wc-shipping-method.php | 12 +++++ readme.txt | 1 + 3 files changed, 65 insertions(+), 1 deletion(-) diff --git a/classes/shipping/class-wc-flat-rate.php b/classes/shipping/class-wc-flat-rate.php index 0e5198183eb..10fb7a35177 100644 --- a/classes/shipping/class-wc-flat-rate.php +++ b/classes/shipping/class-wc-flat-rate.php @@ -39,6 +39,10 @@ class WC_Flat_Rate extends WC_Shipping_Method { $this->tax_status = $this->settings['tax_status']; $this->cost = $this->settings['cost']; $this->fee = $this->settings['fee']; + $this->options = isset($this->settings['options']) ? $this->settings['options'] : ''; + + // Get options + $this->options = (array) explode("\n", $this->options); // Load Flat rates $this->get_flat_rates(); @@ -118,6 +122,12 @@ class WC_Flat_Rate extends WC_Shipping_Method { 'description' => __('Fee excluding tax. Enter an amount, e.g. 2.50, or a percentage, e.g. 5%. Leave blank to disable.', 'woocommerce'), 'default' => '' ), + 'options' => array( + 'title' => __( 'Shipping Options', 'woocommerce' ), + 'type' => 'textarea', + 'description' => __('Optional extra shipping options with additional costs (one per line). Format: Option Name|Cost|Cost is per-order (yes or no). Example: Priority Mail|6.95|yes. If per-order is set to no, it will use "Calculation Type" setting.', 'woocommerce'), + 'default' => '' + ), ); } // End init_form_fields() @@ -232,6 +242,7 @@ class WC_Flat_Rate extends WC_Shipping_Method { // Per item shipping so we pass an array of costs (per item) instead of a single value $costs = array(); + $total_quantity = 0; // Shipping per item foreach ($woocommerce->cart->get_cart() as $item_id => $values) : @@ -252,6 +263,8 @@ class WC_Flat_Rate extends WC_Shipping_Method { $costs[$item_id] = (( $cost + $fee ) * $values['quantity']); + $total_quantity += $values['quantity']; + endif; endforeach; @@ -263,9 +276,47 @@ class WC_Flat_Rate extends WC_Shipping_Method { ); endif; - + // Register the rate $this->add_rate( $rate ); + + // Add any extra rates + if (sizeof($this->options) > 0) foreach ($this->options as $option) { + + $this_option = explode('|', $option); + + if (sizeof($this_option)!==3) continue; + + $extra_rate = $rate; + + $extra_rate['id'] = $this->id . ':' . sanitize_title($this_option[0]); + $extra_rate['label'] = $this_option[0]; + + $per_order_cost = ($this_option[2]=='yes') ? 1 : 0; + $this_cost = $this_option[1]; + + if (is_array($extra_rate['cost'])) { + + if ($per_order_cost) { + $extra_rate['cost']['order'] = $this_cost; + } else { + // Per-product shipping + $extra_rate['cost']['order'] = $this_cost * $total_quantity; + } + + } else { + + // If using shipping per class, multiple the cost by the classes we found + if (!$per_order_cost && $this->type=='class') { + $this_cost = $this_cost * $found_shipping_classes; + } + + $extra_rate['cost'] = $extra_rate['cost'] + $this_cost; + } + + $this->add_rate( $extra_rate ); + + } } /** diff --git a/classes/shipping/class-wc-shipping-method.php b/classes/shipping/class-wc-shipping-method.php index 748d5f0a334..2e47a737286 100644 --- a/classes/shipping/class-wc-shipping-method.php +++ b/classes/shipping/class-wc-shipping-method.php @@ -86,6 +86,18 @@ class WC_Shipping_Method extends WC_Settings_API { endforeach; endforeach; + + // Add any cost for the order - order costs are in the key 'order' + if (isset($cost['order'])) { + + $rates = $_tax->get_shipping_tax_rates(); + $item_taxes = $_tax->calc_shipping_tax( $cost['order'], $rates ); + + // Sum the item taxes + foreach (array_keys($taxes + $item_taxes) as $key) : + $taxes[$key] = (isset($item_taxes[$key]) ? $item_taxes[$key] : 0) + (isset($taxes[$key]) ? $taxes[$key] : 0); + endforeach; + } endif; diff --git a/readme.txt b/readme.txt index a4e02cb1d3e..28c022acc6a 100644 --- a/readme.txt +++ b/readme.txt @@ -150,6 +150,7 @@ Yes you can! Join in on our [GitHub repository](http://github.com/woothemes/wooc * Downloadable Product Permissions management via orders page - grant and revoke access to files * See how many times a download has been downloaded * Option for downloadable files to expire after X days +* Define extra flat rate addons - e.g. priority shipping for an extra $5 * PayPal standard goes straight to PayPal via a GET request - no more pay page/forms * Better mixed cart handling - option to give access to downloads after payment (processing order status) * Added basic API for payment gateways to hook into (for IPN etc)