From d1daafc05507b316e457f14d48e9263e83acfd7c Mon Sep 17 00:00:00 2001
From: Reinhold Kainhofer <reinhold@kainhofer.com>
Date: Tue, 14 Apr 2015 01:03:53 +0200
Subject: [PATCH] Make sure the order numbers config section comes before all
 payment gateways; some small cleanup

---
 woocommerce-advanced-ordernumbers.php | 25 ++++++++++++++++++-------
 1 file changed, 18 insertions(+), 7 deletions(-)

diff --git a/woocommerce-advanced-ordernumbers.php b/woocommerce-advanced-ordernumbers.php
index 512e951..37d42f8 100644
--- a/woocommerce-advanced-ordernumbers.php
+++ b/woocommerce-advanced-ordernumbers.php
@@ -16,6 +16,7 @@ WC tested up to: 2.3
 /**
  * The structure of this plugin originally followed the tutorial, although much of the plugin has been rewritten since then:
  * http://www.yaconiello.com/blog/how-to-write-wordpress-plugin/
+ * A lot of coding ideas were also taken directly from the way things are implemented in WooCommerce itself.
  */
  
 if ( ! defined( 'ABSPATH' ) ) { 
@@ -111,11 +112,11 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
 
 				// register actions
 				add_filter( 'woocommerce_get_sections_checkout', array($this, 'add_admin_section'));
+				// The checkout settings page assumes all subpages are payment gateways, so we have to override this and manually pass our settings:
+				add_action( 'woocommerce_settings_checkout', array( $this, 'settings_output' ) );
+				add_action( 'woocommerce_settings_save_checkout', array( $this, 'settings_save' ) );
 				// Add the ordernumber post meta to the search in the backend
 				add_filter( 'woocommerce_shop_order_search_fields', array($this, 'order_search_fields'));
-				// The checkout page assumes all subpages are payment gateways, so we have to override this:
-				add_action( 'woocommerce_settings_checkout', array( $this, 'output' ) );
-				add_action( 'woocommerce_settings_save_checkout', array( $this, 'save' ) );
 				// Sort the order list in the backend by order number rather than ID, make sure this is called LAST so we modify the defaults passed as arguments
 				add_filter( 'manage_edit-shop_order_sortable_columns', array( $this, 'modify_order_column_sortkey' ), 9999 );
 
@@ -131,12 +132,22 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
 			// Deactivate the plugin
 			public static function deactivate() {}
 
+			/**
+			 * Insert our own section in the checkout setting page. Rearrange the sections array to make sure our settings
+			 * come second place, directly after the default page with the '' key and before all the payment gateways
+			 */
 			function add_admin_section($sections) {
-				$sections['ordernumber'] = __( 'Order Numbers', 'woocommerce-advanced-ordernumbers');
-				return $sections;
+				$newsections = array();
+				foreach ($sections as $sec => $name ) {
+					$newsections[$sec] = $name;
+					if ($sec == '') {
+						$newsections['ordernumber'] = __( 'Order Numbers', 'woocommerce-advanced-ordernumbers');
+					}
+				}
+				return $newsections;
 			}
 			
-			public function output() {
+			public function settings_output() {
 				global $current_section;
 				if ($current_section == 'ordernumber') {
 					$settings = $this->settings;
@@ -144,7 +155,7 @@ if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', g
 				}
 			}
 
-			public function save() {
+			public function settings_save() {
 				global $current_section;
 				if ($current_section == 'ordernumber') {
 					$settings = $this->settings;
-- 
GitLab