From 9818796d3ef8d1dc691c90ac17601781460618d7 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 15 Dec 2015 15:49:35 +0000 Subject: [PATCH] Removing selection priority This option was added so you could sort methods in a custom order, but choose a different default. Doing this per zone would add extra complexity, and this has also proven problematic when a method registers multiple rates. This commit removes usage of the setting. We should use the display order and choose the top method by default. It think this balances ease of use with control. --- .../settings/class-wc-settings-shipping.php | 64 ------------------ includes/class-wc-shipping.php | 67 +++---------------- 2 files changed, 10 insertions(+), 121 deletions(-) diff --git a/includes/admin/settings/class-wc-settings-shipping.php b/includes/admin/settings/class-wc-settings-shipping.php index a21582be57c..c793ea64ee6 100644 --- a/includes/admin/settings/class-wc-settings-shipping.php +++ b/includes/admin/settings/class-wc-settings-shipping.php @@ -29,7 +29,6 @@ class WC_Settings_Shipping extends WC_Settings_Page { add_filter( 'woocommerce_settings_tabs_array', array( $this, 'add_settings_page' ), 20 ); add_action( 'woocommerce_sections_' . $this->id, array( $this, 'output_sections' ) ); add_action( 'woocommerce_settings_' . $this->id, array( $this, 'output' ) ); - add_action( 'woocommerce_admin_field_shipping_methods', array( $this, 'shipping_methods_setting' ) ); add_action( 'woocommerce_settings_save_' . $this->id, array( $this, 'save' ) ); } @@ -151,10 +150,6 @@ class WC_Settings_Shipping extends WC_Settings_Page { 'type' => 'multi_select_countries' ), - array( - 'type' => 'shipping_methods', - ), - array( 'type' => 'sectionend', 'id' => 'shipping_options' ), ) ); @@ -187,64 +182,6 @@ class WC_Settings_Shipping extends WC_Settings_Page { } } - /** - * Output shipping method settings. - */ - public function shipping_methods_setting() { - $selection_priority = get_option( 'woocommerce_shipping_method_selection_priority', array() ); - ?> - - - - - - - - - - - - - - - shipping->load_shipping_methods() as $key => $method ) : ?> - - - - - - - - - - - - - - - -
 
- - - has_settings() ) : ?> - get_title() ); ?> - has_settings() ) : ?> - - id ); ?> - - enabled ) : ?> - - - - - - - -
 
- - - get_settings() ); - $wc_shipping->process_admin_options(); } else { foreach ( $wc_shipping->get_shipping_methods() as $method_id => $method ) { diff --git a/includes/class-wc-shipping.php b/includes/class-wc-shipping.php index 725dc1f94fc..74de27138ee 100644 --- a/includes/class-wc-shipping.php +++ b/includes/class-wc-shipping.php @@ -193,46 +193,24 @@ class WC_Shipping { * @return string */ private function get_default_method( $available_methods, $current_chosen_method = false ) { - $selection_priority = get_option( 'woocommerce_shipping_method_selection_priority', array() ); - if ( ! empty( $available_methods ) ) { - - // Is a method already chosen? - if ( ! empty( $current_chosen_method ) && ! isset( $available_methods[ $current_chosen_method ] ) ) { - foreach ( $available_methods as $method_key => $method ) { - if ( strpos( $method->id, $current_chosen_method ) === 0 ) { - return $method->id; + if ( ! empty( $current_chosen_method ) ) { + if ( isset( $available_methods[ $current_chosen_method ] ) ) { + return $available_methods[ $current_chosen_method ]->id; + } else { + foreach ( $available_methods as $method_key => $method ) { + if ( strpos( $method->id, $current_chosen_method ) === 0 ) { + return $method->id; + } } } } - - // Order by priorities and costs - $prioritized_methods = array(); - - foreach ( $available_methods as $method_key => $method ) { - // Some IDs contain : if they have multiple rates so use $method->method_id - $priority = isset( $selection_priority[ $method->method_id ] ) ? absint( $selection_priority[ $method->method_id ] ): 1; - - if ( empty( $prioritized_methods[ $priority ] ) ) { - $prioritized_methods[ $priority ] = array(); - } - - $prioritized_methods[ $priority ][ $method_key ] = $method->cost; - } - - ksort( $prioritized_methods ); - $prioritized_methods = current( $prioritized_methods ); - asort( $prioritized_methods ); - - return current( array_keys( $prioritized_methods ) ); + return current( $available_methods )->id; } - - return false; + return ''; } /** - * Calculate shipping costs. - * * Calculate shipping for (multiple) packages of cart items. * * @param array $packages multi-dimensional array of cart items to calc shipping for @@ -368,7 +346,6 @@ class WC_Shipping { /** * Get packages. - * * @return array */ public function get_packages() { @@ -387,30 +364,6 @@ class WC_Shipping { $this->packages = array(); } - /** - * Process admin options. - * - * Saves options on the shipping setting page. - */ - public function process_admin_options() { - $method_order = isset( $_POST['method_order'] ) ? $_POST['method_order'] : ''; - $method_priority = isset( $_POST['method_priority'] ) ? $_POST['method_priority'] : ''; - $order = array(); - $selection_priority = array(); - - if ( is_array( $method_order ) && sizeof( $method_order ) > 0 ) { - $loop = 0; - foreach ( $method_order as $method_id ) { - $order[ $method_id ] = $loop; - $selection_priority[ $method_id ] = absint( $method_priority[ $method_id ] ); - $loop ++; - } - } - - update_option( 'woocommerce_shipping_method_selection_priority', $selection_priority ); - update_option( 'woocommerce_shipping_method_order', $order ); - } - /** * @deprecated 2.6.0 Was previously used to determine sort order of methods, but this is now controlled by zones and thus unused. */