From 63ec2b001fcdaccd2a4c44fbf6fffe2ee63255d6 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Fri, 2 Sep 2011 15:42:04 +0100 Subject: [PATCH] New shipping system to delete with multiple rates per gateway --- admin/admin-attributes.php | 2 +- admin/admin-post-types.php | 2 +- admin/admin-settings.php | 87 ++++++++++----------- assets/css/admin.css | 6 -- assets/css/admin.less | 8 +- assets/js/woocommerce.js | 2 +- classes/checkout.class.php | 11 ++- classes/countries.class.php | 2 +- classes/order.class.php | 6 +- classes/shipping/shipping-flat_rate.php | 2 +- classes/shipping/shipping-free_shipping.php | 3 +- classes/shipping/shipping.class.php | 53 +++++++++---- classes/shipping/shipping_method.class.php | 17 +--- shortcodes/shortcode-cart.php | 2 +- templates/checkout/review_order.php | 2 +- woocommerce_actions.php | 2 +- woocommerce_query.php | 6 +- woocommerce_template_functions.php | 18 +++-- woocommerce_templates.php | 2 + 19 files changed, 125 insertions(+), 108 deletions(-) diff --git a/admin/admin-attributes.php b/admin/admin-attributes.php index 490e40b8783..703adf6d287 100644 --- a/admin/admin-attributes.php +++ b/admin/admin-attributes.php @@ -21,7 +21,7 @@ function woocommerce_attributes() { if (isset($_POST['add_new_attribute']) && $_POST['add_new_attribute']) : - $attribute_name = (string) strtolower(sanitize_title($_POST['attribute_name'])); + $attribute_name = (string) sanitize_title($_POST['attribute_name']); $attribute_type = (string) $_POST['attribute_type']; $attribute_label = (string) $_POST['attribute_label']; diff --git a/admin/admin-post-types.php b/admin/admin-post-types.php index 7c6d18ecc34..280a94edc2c 100644 --- a/admin/admin-post-types.php +++ b/admin/admin-post-types.php @@ -353,7 +353,7 @@ function woocommerce_custom_order_columns($column) { echo '–'; endif; - if ($order->shipping_metho) : + if ($order->shipping_method) : echo '' . __('Shipped via ', 'woothemes') . $order->shipping_method . ''; endif; break; diff --git a/admin/admin-settings.php b/admin/admin-settings.php index a317a60ad29..ca86c2d5c8c 100644 --- a/admin/admin-settings.php +++ b/admin/admin-settings.php @@ -585,7 +585,7 @@ function woocommerce_update_options($options) { $state = end($cr); endif; - $counties_array[$country][] = $state; + $counties_array[trim($country)][] = trim($state); endforeach; @@ -840,51 +840,6 @@ function woocommerce_admin_fields($options) {

- ', + selectedList: 4 + }); + + // Tax + jQuery('#tax_rates a.add').live('click', function(){ + var size = jQuery('.taxrows .taxrow').size(); + + // Add the row + jQuery('
\ + \ + \ + % \ + ×\ +
').appendTo('#tax_rates div.taxrows'); + + // Multiselect + jQuery(".country_multiselect").multiselect({ + noneSelectedText: '', + selectedList: 4 + }); + + return false; + }); + + jQuery('#tax_rates a.remove').live('click', function(){ + var answer = confirm(""); + if (answer) { + jQuery('input', jQuery(this).parent()).val(''); + jQuery(this).parent().hide(); + } + return false; + }); + }); posted['shiptobilling'] = 'true'; + // Update shipping method to posted + $_SESSION['_chosen_shipping_method'] = $this->posted['shipping_method']; + // Billing Information foreach ($this->billing_fields as $field) : @@ -576,6 +579,12 @@ class woocommerce_checkout { endif; endif; + // Get better formatted shipping method (title/label) + $shipping_method = $this->posted['shipping_method']; + if (isset($available_methods) && isset($available_methods[$this->posted['shipping_method']])) : + $shipping_method = $available_methods[$this->posted['shipping_method']]->title; + endif; + // Update order meta update_post_meta( $order_id, '_billing_first_name', $this->posted['billing-first_name']); update_post_meta( $order_id, '_billing_last_name', $this->posted['billing-last_name']); @@ -597,7 +606,7 @@ class woocommerce_checkout { update_post_meta( $order_id, '_shipping_postcode', $shipping_postcode); update_post_meta( $order_id, '_shipping_country', $shipping_country); update_post_meta( $order_id, '_shipping_state', $shipping_state); - update_post_meta( $order_id, '_shipping_method', $this->posted['shipping_method']); + update_post_meta( $order_id, '_shipping_method', $shipping_method); update_post_meta( $order_id, '_payment_method', $this->posted['payment_method']); update_post_meta( $order_id, '_order_subtotal', number_format(woocommerce_cart::$subtotal_ex_tax, 2, '.', '')); update_post_meta( $order_id, '_order_shipping', number_format(woocommerce_cart::$shipping_total, 2, '.', '')); diff --git a/classes/countries.class.php b/classes/countries.class.php index 983f3f6b3c4..0fcba9d3e4a 100644 --- a/classes/countries.class.php +++ b/classes/countries.class.php @@ -412,7 +412,7 @@ class woocommerce_countries { ),*/ 'US' => array( 'AL' => 'Alabama' , - 'AK' => 'Alaska ' , + 'AK' => 'Alaska' , 'AZ' => 'Arizona' , 'AR' => 'Arkansas' , 'CA' => 'California' , diff --git a/classes/order.class.php b/classes/order.class.php index a92fff9af6b..cd6c5f5e991 100644 --- a/classes/order.class.php +++ b/classes/order.class.php @@ -79,6 +79,7 @@ class woocommerce_order { 'order_subtotal' => '', 'order_discount' => '', 'order_tax' => '', + 'order_shipping' => '', 'order_shipping_tax' => '', 'order_total' => '' ); @@ -156,8 +157,11 @@ class woocommerce_order { $shipping = woocommerce_price($this->order_shipping); if ($this->order_shipping_tax > 0) : - $shipping .= sprintf(__(' (ex. tax) via %s', 'woothemes'), ucwords($this->shipping_method)); + $tax_text = __('(ex. tax) ', 'woothemes'); + else : + $tax_text = ''; endif; + $shipping .= sprintf(__(' %svia %s', 'woothemes'), $tax_text, ucwords($this->shipping_method)); else : $shipping = __('Free!', 'woothemes'); diff --git a/classes/shipping/shipping-flat_rate.php b/classes/shipping/shipping-flat_rate.php index 33384caccb6..ac1afe461fa 100644 --- a/classes/shipping/shipping-flat_rate.php +++ b/classes/shipping/shipping-flat_rate.php @@ -177,7 +177,7 @@ class flat_rate extends woocommerce_shipping_method { public function process_admin_options() { - if(isset($_POST['woocommerce_flat_rate_tax_status'])) update_option('woocommerce_flat_rate_tax_status', 'yes'); else update_option('woocommerce_flat_rate_tax_status', 'no'); + if(isset($_POST['woocommerce_flat_rate_tax_status'])) update_option('woocommerce_flat_rate_tax_status', woocommerce_clean($_POST['woocommerce_flat_rate_tax_status'])); else @delete_option('woocommerce_flat_rate_tax_status'); if(isset($_POST['woocommerce_flat_rate_enabled'])) update_option('woocommerce_flat_rate_enabled', 'yes'); else update_option('woocommerce_flat_rate_enabled', 'no'); diff --git a/classes/shipping/shipping-free_shipping.php b/classes/shipping/shipping-free_shipping.php index f9947bb0e50..e2fdaed2dc8 100644 --- a/classes/shipping/shipping-free_shipping.php +++ b/classes/shipping/shipping-free_shipping.php @@ -18,7 +18,6 @@ class free_shipping extends woocommerce_shipping_method { $this->min_amount = get_option('woocommerce_free_shipping_minimum_amount'); $this->availability = get_option('woocommerce_free_shipping_availability'); $this->countries = get_option('woocommerce_free_shipping_countries'); - if (isset($_SESSION['_chosen_method_id']) && $_SESSION['_chosen_method_id']==$this->id) $this->chosen = true; add_action('woocommerce_update_options', array(&$this, 'process_admin_options')); add_option('woocommerce_free_shipping_availability', 'all'); @@ -53,7 +52,7 @@ class free_shipping extends woocommerce_shipping_method { - + diff --git a/classes/shipping/shipping.class.php b/classes/shipping/shipping.class.php index cf63126e6d4..731a279e8fe 100644 --- a/classes/shipping/shipping.class.php +++ b/classes/shipping/shipping.class.php @@ -49,23 +49,46 @@ class woocommerce_shipping { if (self::$enabled=='yes') : $_available_methods = array(); - - foreach ( self::$shipping_methods as $method ) : + + foreach ( self::$shipping_methods as $shipping_method ) : - if ($method->is_available()) $_available_methods[$method->id] = $method; + if ($shipping_method->is_available()) : + + $shipping_method->calculate_shipping(); + + // If available, put available methods/rates in the array + if ($shipping_method->multiple_rates) : + + foreach ($shipping_method->rates as $rate) : + + $method = $rate; + + $_available_methods[$method->id] = $method; + + endforeach; + + else : + + $method = $shipping_method; + + $_available_methods[$method->id] = $method; + + endif; + + endif; endforeach; - + return $_available_methods; endif; } function reset_shipping_methods() { - foreach ( self::$shipping_methods as $method ) : - $method->chosen = false; - $method->shipping_total = 0; - $method->shipping_tax = 0; + foreach ( self::$shipping_methods as $shipping_method ) : + $shipping_method->shipping_total = 0; + $shipping_method->shipping_tax = 0; + $shipping_method->rates = array(); endforeach; } @@ -78,7 +101,7 @@ class woocommerce_shipping { self::$shipping_label = null; $_cheapest_fee = ''; $_cheapest_method = ''; - if (isset($_SESSION['_chosen_method_id'])) $chosen_method = $_SESSION['_chosen_method_id']; else $chosen_method = ''; + if (isset($_SESSION['_chosen_shipping_method'])) $chosen_method = $_SESSION['_chosen_shipping_method']; else $chosen_method = ''; $calc_cheapest = false; if (!$chosen_method || empty($chosen_method)) $calc_cheapest = true; @@ -89,15 +112,14 @@ class woocommerce_shipping { if (sizeof($_available_methods)>0) : - foreach ($_available_methods as $method) : + foreach ($_available_methods as $method_id => $method) : - $method->calculate_shipping(); $fee = $method->shipping_total; if ($fee < $_cheapest_fee || !is_numeric($_cheapest_fee)) : - $_cheapest_fee = $fee; - $_cheapest_method = $method->id; + $_cheapest_fee = $fee; + $_cheapest_method = $method_id; endif; - + endforeach; // Default to cheapest @@ -107,7 +129,7 @@ class woocommerce_shipping { if ($chosen_method) : - $_available_methods[$chosen_method]->choose(); + $_SESSION['_chosen_shipping_method'] = $chosen_method; self::$shipping_total = $_available_methods[$chosen_method]->shipping_total; self::$shipping_tax = $_available_methods[$chosen_method]->shipping_tax; self::$shipping_label = $_available_methods[$chosen_method]->title; @@ -120,6 +142,7 @@ class woocommerce_shipping { } function reset_shipping() { + unset($_SESSION['_chosen_shipping_method']); self::$shipping_total = 0; self::$shipping_tax = 0; self::$shipping_label = null; diff --git a/classes/shipping/shipping_method.class.php b/classes/shipping/shipping_method.class.php index ba638a394ca..97d41285fb5 100644 --- a/classes/shipping/shipping_method.class.php +++ b/classes/shipping/shipping_method.class.php @@ -16,13 +16,14 @@ class woocommerce_shipping_method { var $availability; var $countries; var $type; - var $cost = 0; var $fee = 0; var $min_amount = null; var $enabled = false; - var $chosen = false; var $shipping_total = 0; var $shipping_tax = 0; + var $cost = 0; // Stores cost if theres only one + var $multiple_rates = false; + var $rates = array(); // When a method has more than one cost/choice it will be in this array of titles/costs public function is_available() { @@ -55,17 +56,7 @@ class woocommerce_shipping_method { return $fee; endif; } - - public function is_chosen() { - if ($this->chosen) return true; - return false; - } - - public function choose() { - $this->chosen = true; - $_SESSION['_chosen_method_id'] = $this->id; - } - + public function admin_options() {} public function process_admin_options() {} diff --git a/shortcodes/shortcode-cart.php b/shortcodes/shortcode-cart.php index b2753073632..e6a57c105aa 100644 --- a/shortcodes/shortcode-cart.php +++ b/shortcodes/shortcode-cart.php @@ -26,7 +26,7 @@ function woocommerce_cart( $atts ) { // Update Shipping elseif (isset($_POST['calc_shipping']) && $_POST['calc_shipping'] && woocommerce::verify_nonce('cart')) : - unset($_SESSION['_chosen_method_id']); + unset($_SESSION['_chosen_shipping_method']); $country = $_POST['calc_shipping_country']; $state = $_POST['calc_shipping_state']; diff --git a/templates/checkout/review_order.php b/templates/checkout/review_order.php index f55a83ef9ee..3ceb0f65877 100755 --- a/templates/checkout/review_order.php +++ b/templates/checkout/review_order.php @@ -29,7 +29,7 @@ echo '