WooCommerce Sequential Order Numbers - Version 1.3

Version Description

  • 2013.04.26 =
  • Feature - Improved WooCommerce Subscriptions compatibility
  • Feature - Improved WooCommerce Pre-Orders compatibility
  • General code cleanup and refactor
Download this release

Release Info

Developer SkyVerge
Plugin Icon WooCommerce Sequential Order Numbers
Version 1.3
Comparing to
See all releases

Code changes from version 1.2.4 to 1.3

Files changed (2) hide show
  1. readme.txt +39 -34
  2. woocommerce-sequential-order-numbers.php +296 -200
readme.txt CHANGED
@@ -1,9 +1,9 @@
1
  === WooCommerce Sequential Order Numbers ===
2
- Contributors: FoxRunSoftware
3
  Tags: woocommerce, order number
4
  Requires at least: 3.3
5
- Tested up to: 3.5
6
- Stable tag: 1.2.4
7
 
8
  This plugin extends the WooCommerce e-commerce plugin by setting sequential order numbers for new orders.
9
 
@@ -11,21 +11,21 @@ This plugin extends the WooCommerce e-commerce plugin by setting sequential orde
11
 
12
  This plugin extends the WooCommerce e-commerce plugin by setting sequential order numbers for new orders. If there are existing orders at the time of installation, the sequential order numbers will start with the highest current order number.
13
 
14
- If you like this plugin, but are looking for the ability to set the starting number, or to add a custom prefix/suffix to your order numbers (ie, you'd prefer something like WT101UK, WT102UK, etc) please consider my newly released paid WooCommerce Sequential Order Numbers Pro plugin, available from the [WooThemes Store](http://www.woothemes.com/products/sequential-order-numbers-pro/)
15
 
16
  This plugin requires WooCommerce 1.5.6 or greater.
17
 
18
  = Feedback =
19
- * I am open to your suggestions and feedback - Thank you for using or trying out one of my plugins!
20
- * Drop me a line at [www.foxrunsoftware.com](http://www.foxrunsoftware.com)
21
 
22
  = More =
23
- * Vist the [WooCommerce Sequential Order Number plugin page](http://www.foxrunsoftware.net/articles/wordpress/woocommerce-sequential-order-numbers/) for further details and a discussion of the implementation. [Also see my other plugins](http://www.foxrunsoftware.com) or see [my WordPress.org profile page](http://profiles.wordpress.org/foxrunsoftware/)
24
  * Find the enhanced WooCommerce Sequential Order Numbers Pro now available from [WooThemes](http://www.woothemes.com/products/sequential-order-numbers-pro/)
25
 
26
  == Installation ==
27
 
28
- 1. Upload the entire 'woocommerce-sequential-order-number' folder to the '/wp-content/plugins/' directory
29
  2. Activate the plugin through the 'Plugins' menu in WordPress
30
  3. Order numbers will continue sequentially from the current highest order number, or from 1 if no orders have been placed yet
31
 
@@ -33,50 +33,55 @@ This plugin requires WooCommerce 1.5.6 or greater.
33
 
34
  = Can I start the order numbers at a particular number? =
35
 
36
- This free version does not have that functionality, but now you can with the paid [WooCommerce Sequential Order Numbers Pro](http://www.woothemes.com/products/sequential-order-numbers-pro/)
37
 
38
  = Can I set an order number prefix/suffix? =
39
 
40
- This free version does not have that functionality, but now you can with the paid [WooCommerce Sequential Order Numbers Pro](http://www.woothemes.com/products/sequential-order-numbers-pro/)
41
 
42
  == Changelog ==
43
 
44
- = 1.2.4 =
45
- * WordPress 3.5 compatibility fix
46
- * Order numbers not assigned to temporary auto-draft orders created from the admin
 
47
 
48
- = 1.2.3 =
49
- * Removed WooCommerce functions, which caused a compatibility issue with other WooCommerce plugins
 
50
 
51
- = 1.2.2 =
52
- * Takes advantage of new action hooks/filters available in WooCommerce 1.5.6
53
- * Bug fix on installation to stores with more than 10 existing orders
54
 
55
- = 1.2.1 =
56
- * Minor updates due to WooCommerce 1.5.5 release
 
57
 
58
- = 1.2.0 =
59
- * Added support for the order tracking page
60
 
61
- = 1.1.2 =
62
- * Minor updates due to WooCommerce 1.5.4 release
63
 
64
- = 1.1.1 =
65
- * Order number in the subject line of the admin new order email is fixed
66
 
67
- = 1.1.0 =
68
- * Search by order number
69
 
70
- = 1.0.1 =
71
- * small bug fix
72
 
73
- = 1.0.0 =
 
 
 
74
  * Initial Release
75
 
76
  == Upgrade Notice ==
77
 
78
- = 1.2.2 =
79
  This version requires WooCommerce 1.5.6
80
 
81
- = 1.2.1 =
82
- This version requires WooCommerce 1.5.5
1
  === WooCommerce Sequential Order Numbers ===
2
+ Contributors: SkyVerge
3
  Tags: woocommerce, order number
4
  Requires at least: 3.3
5
+ Tested up to: 3.5.1
6
+ Stable tag: 1.3
7
 
8
  This plugin extends the WooCommerce e-commerce plugin by setting sequential order numbers for new orders.
9
 
11
 
12
  This plugin extends the WooCommerce e-commerce plugin by setting sequential order numbers for new orders. If there are existing orders at the time of installation, the sequential order numbers will start with the highest current order number.
13
 
14
+ If you like this plugin, but are looking for the ability to set the starting number, or to add a custom prefix/suffix to your order numbers (ie, you'd prefer something like WT101UK, WT102UK, etc) please consider our premium WooCommerce Sequential Order Numbers Pro plugin, available from the [WooThemes Store](http://www.woothemes.com/products/sequential-order-numbers-pro/)
15
 
16
  This plugin requires WooCommerce 1.5.6 or greater.
17
 
18
  = Feedback =
19
+ * We are open to your suggestions and feedback - Thank you for using or trying out one of our plugins!
20
+ * Drop us a line at [www.skyverge.com](http://www.skyverge.com)
21
 
22
  = More =
23
+ * Vist the [WooCommerce Sequential Order Number plugin page](http://www.skyverge.com/blog/woocommerce-sequential-order-numbers/) for further details and a discussion of the implementation. [Also see our other plugins](http://www.skyverge.com) or see [our WordPress.org profile page](http://profiles.wordpress.org/skyverge/)
24
  * Find the enhanced WooCommerce Sequential Order Numbers Pro now available from [WooThemes](http://www.woothemes.com/products/sequential-order-numbers-pro/)
25
 
26
  == Installation ==
27
 
28
+ 1. Upload the entire 'woocommerce-sequential-order-numbers' folder to the '/wp-content/plugins/' directory
29
  2. Activate the plugin through the 'Plugins' menu in WordPress
30
  3. Order numbers will continue sequentially from the current highest order number, or from 1 if no orders have been placed yet
31
 
33
 
34
  = Can I start the order numbers at a particular number? =
35
 
36
+ This free version does not have that functionality, but now you can with the premium [WooCommerce Sequential Order Numbers Pro](http://www.woothemes.com/products/sequential-order-numbers-pro/)
37
 
38
  = Can I set an order number prefix/suffix? =
39
 
40
+ This free version does not have that functionality, but now you can with the premium [WooCommerce Sequential Order Numbers Pro](http://www.woothemes.com/products/sequential-order-numbers-pro/)
41
 
42
  == Changelog ==
43
 
44
+ = 1.3 - 2013.04.26 =
45
+ * Feature - Improved WooCommerce Subscriptions compatibility
46
+ * Feature - Improved WooCommerce Pre-Orders compatibility
47
+ * General code cleanup and refactor
48
 
49
+ = 1.2.4 - 2012.12.14 =
50
+ * Fix - WordPress 3.5 compatibility fix
51
+ * Fix - Order numbers not assigned to temporary auto-draft orders created from the admin
52
 
53
+ = 1.2.3 - 2012.06.06 =
54
+ * Fix - Removed WooCommerce functions, which caused a compatibility issue with other WooCommerce plugins
 
55
 
56
+ = 1.2.2 - 2012.05.25 =
57
+ * Tweak - Takes advantage of new action hooks/filters available in WooCommerce 1.5.6
58
+ * Fix - Bug fix on installation to stores with more than 10 existing orders
59
 
60
+ = 1.2.1 - 2012.05.13 =
61
+ * Tweak - Minor updates due to WooCommerce 1.5.5 release
62
 
63
+ = 1.2.0 - 2012.04.21 =
64
+ * Feature - Added support for the order tracking page
65
 
66
+ = 1.1.2 - 2012.04.18 =
67
+ * Tweak - Minor updates due to WooCommerce 1.5.4 release
68
 
69
+ = 1.1.1 - 2012.04.02 =
70
+ * Fix - Order number in the subject line of the admin new order email is fixed
71
 
72
+ = 1.1.0 - 2012.04.02 =
73
+ * Feature - Search by order number
74
 
75
+ = 1.0.1 - 2012.04.02 =
76
+ * Fix - small bug fix
77
+
78
+ = 1.0.0 - 2012.04.02 =
79
  * Initial Release
80
 
81
  == Upgrade Notice ==
82
 
83
+ = 1.2.2 - 2012.05.25 =
84
  This version requires WooCommerce 1.5.6
85
 
86
+ = 1.2.1 - 2012.05.13 =
87
+ This version requires WooCommerce 1.5.5
woocommerce-sequential-order-numbers.php CHANGED
@@ -1,210 +1,306 @@
1
  <?php
2
- /*
3
- Plugin Name: WooCommerce Sequential Order Numbers
4
- Plugin URI: http://www.foxrunsoftware.net/articles/wordpress/woocommerce-sequential-order-numbers/
5
- Description: Provides sequential order numbers for WooCommerce orders
6
- Author: Justin Stern
7
- Author URI: http://www.foxrunsoftware.net
8
- Version: 1.2.4
9
-
10
- Copyright: 2012 Justin Stern (email : justin@foxrunsoftware.net)
11
- License: GNU General Public License v3.0
12
- License URI: http://www.gnu.org/licenses/gpl-3.0.html
13
- */
 
 
 
 
 
 
 
 
 
 
 
 
 
14
 
15
  /**
16
- * Required functions
17
- **/
18
- if ( in_array( 'woocommerce/woocommerce.php', apply_filters( 'active_plugins', get_option( 'active_plugins' ) ) ) ) {
19
-
20
- if ( ! class_exists( 'WC_Seq_Order_Number' ) ) {
21
-
22
- class WC_Seq_Order_Number {
23
- const VERSION = "1.2.4";
24
- const VERSION_OPTION_NAME = "woocommerce_seq_order_number_db_version";
25
-
26
- public function __construct() {
27
-
28
- // set the custom order number on the new order. we hook into wp_insert_post for orders which are created
29
- // from the frontend, and we hook into woocommerce_process_shop_order_meta for admin-created orders
30
- add_action( 'wp_insert_post', array( &$this, 'set_sequential_order_number' ), 10, 2 );
31
- add_action( 'woocommerce_process_shop_order_meta', array( &$this, 'set_sequential_order_number' ), 10, 2 );
32
-
33
- // return our custom order number for display
34
- add_filter( 'woocommerce_order_number', array( &$this, 'get_order_number' ), 10, 2);
35
-
36
- // order tracking page search by order number
37
- add_filter( 'woocommerce_shortcode_order_tracking_order_id', array( &$this, 'find_order_by_order_number' ) );
38
-
39
- if ( is_admin() ) {
40
- add_filter( 'request', array( &$this, 'woocommerce_custom_shop_order_orderby' ), 20 );
41
- add_filter( 'woocommerce_shop_order_search_fields', array( &$this, 'woocommerce_shop_order_search_fields' ) );
42
- }
43
-
44
- // Installation
45
- if ( is_admin() && ! defined( 'DOING_AJAX' ) ) $this->install();
46
- }
47
-
48
-
49
- /**
50
- * Search for an order with order_number $order_number
51
- *
52
- * @param string $order_number order number to search for
53
- *
54
- * @return int post_id for the order identified by $order_number, or 0
55
- */
56
- public function find_order_by_order_number( $order_number ) {
57
-
58
- // search for the order by custom order number
59
- $query_args = array(
60
- 'numberposts' => 1,
61
- 'meta_key' => '_order_number',
62
- 'meta_value' => $order_number,
63
- 'post_type' => 'shop_order',
64
- 'post_status' => 'publish',
65
- 'fields' => 'ids'
66
- );
67
-
68
- list( $order_id ) = get_posts( $query_args );
69
-
70
- // order was found
71
- if ( $order_id !== null ) return $order_id;
72
-
73
- // if we didn't find the order, then it may be that this plugin was disabled and an order was placed in the interim
74
- $order = new WC_Order( $order_number );
75
- if ( isset( $order->order_custom_fields['_order_number'][0] ) ) {
76
- // _order_number was set, so this is not an old order, it's a new one that just happened to have post_id that matched the searched-for order_number
77
- return 0;
78
- }
79
-
80
- return $order->id;
81
- }
82
-
83
-
84
- /**
85
- * Set the _order_number field for the newly created order
86
- *
87
- * @param int $post_id post identifier
88
- * @param object $post post object
89
- */
90
- public function set_sequential_order_number( $post_id, $post ) {
91
- global $wpdb;
92
-
93
- if ( $post->post_type == 'shop_order' && $post->post_status != 'auto-draft' ) {
94
- $order_number = get_post_meta( $post_id, '_order_number', true );
95
- if ( $order_number == "" ) {
96
-
97
- // attempt the query up to 3 times for a much higher success rate if it fails (due to Deadlock)
98
- $success = false;
99
- for ( $i = 0; $i < 3 && ! $success; $i++ ) {
100
- // this seems to me like the safest way to avoid order number clashes
101
- $success = $wpdb->query( 'INSERT INTO ' . $wpdb->postmeta . ' (post_id,meta_key,meta_value) SELECT ' . $post_id . ',"_order_number",if(max(cast(meta_value as UNSIGNED)) is null,1,max(cast(meta_value as UNSIGNED))+1) from ' . $wpdb->postmeta . ' where meta_key="_order_number"' );
102
- }
103
- }
104
- }
105
- }
106
-
107
-
108
- /**
109
- * Filter to return our _order_number field rather than the post ID,
110
- * for display.
111
- *
112
- * @param string $order_number the order id with a leading hash
113
- * @param WC_Order $order the order object
114
- *
115
- * @return string custom order number, with leading hash
116
- */
117
- public function get_order_number( $order_number, $order ) {
118
- if ( isset( $order->order_custom_fields['_order_number'] ) ) {
119
- return '#' . $order->order_custom_fields['_order_number'][0];
 
120
  }
121
- return $order_number;
122
- }
123
-
124
-
125
- /** Admin filters ******************************************************/
126
-
127
-
128
- /**
129
- * Admin order table orderby ID operates on our meta _order_number
130
- *
131
- * @param array $vars associative array of orderby parameteres
132
- *
133
- * @return array associative array of orderby parameteres
134
- */
135
- public function woocommerce_custom_shop_order_orderby( $vars ) {
136
- global $typenow, $wp_query;
137
- if ( $typenow != 'shop_order' ) return $vars;
138
-
139
- // Sorting
140
- if ( isset( $vars['orderby'] ) ) :
141
- if ( 'ID' == $vars['orderby'] ) :
142
- $vars = array_merge( $vars, array(
143
- 'meta_key' => '_order_number',
144
- 'orderby' => 'meta_value_num'
145
- ) );
146
- endif;
147
-
148
- endif;
149
-
150
- return $vars;
151
- }
152
-
153
-
154
- /**
155
- * Add our custom _order_number to the set of search fields so that
156
- * the admin search functionality is maintained
157
- *
158
- * @param array $search_fields array of post meta fields to search by
159
- *
160
- * @return array of post meta fields to search by
161
- */
162
- public function woocommerce_shop_order_search_fields( $search_fields ) {
163
-
164
- array_push( $search_fields, '_order_number' );
165
-
166
- return $search_fields;
167
  }
168
-
169
-
170
- /** Lifecycle methods ******************************************************/
171
-
172
-
173
- /**
174
- * Run every time. Used since the activation hook is not executed when updating a plugin
175
- */
176
- private function install() {
177
- $installed_version = get_option( WC_Seq_Order_Number::VERSION_OPTION_NAME );
178
-
179
- if ( ! $installed_version ) {
180
- // initial install, set the order number for all existing orders to the post id
181
- $orders = get_posts( array( 'numberposts' => '', 'post_type' => 'shop_order', 'nopaging' => true ) );
182
- if ( is_array( $orders ) ) {
183
- foreach( $orders as $order ) {
184
- if ( get_post_meta( $order->ID, '_order_number', true ) == '' ) {
185
- add_post_meta( $order->ID, '_order_number', $order->ID );
186
- }
187
- }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
188
  }
189
  }
190
-
191
- if ( $installed_version != WC_Seq_Order_Number::VERSION ) {
192
- $this->upgrade( $installed_version );
193
-
194
- // new version number
195
- update_option( WC_Seq_Order_Number::VERSION_OPTION_NAME, WC_Seq_Order_Number::VERSION );
196
- }
197
- }
198
-
199
-
200
- /**
201
- * Run when plugin version number changes
202
- */
203
- private function upgrade( $installed_version ) {
204
- // upgrade code goes here
205
  }
206
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
207
  }
208
-
209
- $GLOBALS['wc_seq_order_number'] = new WC_Seq_Order_Number();
210
- }
1
  <?php
2
+ /**
3
+ * Plugin Name: WooCommerce Sequential Order Numbers
4
+ * Plugin URI: http://www.skyverge.com/blog/woocommerce-sequential-order-numbers/
5
+ * Description: Provides sequential order numbers for WooCommerce orders
6
+ * Author: SkyVerge
7
+ * Author URI: http://www.skyverge.com
8
+ * Version: 1.3
9
+ *
10
+ * Copyright: (c) 2012-2013 SkyVerge, Inc. (info@skyverge.com)
11
+ *
12
+ * License: GNU General Public License v3.0
13
+ * License URI: http://www.gnu.org/licenses/gpl-3.0.html
14
+ *
15
+ * @package WC-Sequential-Order-Numbers
16
+ * @author SkyVerge
17
+ * @category Plugin
18
+ * @copyright Copyright (c) 2012-2013, SkyVerge, Inc.
19
+ * @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
20
+ */
21
+
22
+ if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
23
+
24
+ // Check if WooCommerce is active
25
+ if ( ! WC_Seq_Order_Number::is_woocommerce_active() )
26
+ return;
27
 
28
  /**
29
+ * The WC_Seq_Order_Number global object
30
+ * @name $wc_seq_order_number
31
+ * @global WC_Seq_Order_Number $GLOBALS['wc_seq_order_number']
32
+ */
33
+ $GLOBALS['wc_seq_order_number'] = new WC_Seq_Order_Number();
34
+
35
+ class WC_Seq_Order_Number {
36
+
37
+ /** version number */
38
+ const VERSION = "1.3";
39
+
40
+ /** version option name */
41
+ const VERSION_OPTION_NAME = "woocommerce_seq_order_number_db_version";
42
+
43
+ public function __construct() {
44
+
45
+ // set the custom order number on the new order. we hook into wp_insert_post for orders which are created
46
+ // from the frontend, and we hook into woocommerce_process_shop_order_meta for admin-created orders
47
+ add_action( 'wp_insert_post', array( $this, 'set_sequential_order_number' ), 10, 2 );
48
+ add_action( 'woocommerce_process_shop_order_meta', array( $this, 'set_sequential_order_number' ), 10, 2 );
49
+
50
+ // return our custom order number for display
51
+ add_filter( 'woocommerce_order_number', array( $this, 'get_order_number' ), 10, 2);
52
+
53
+ // order tracking page search by order number
54
+ add_filter( 'woocommerce_shortcode_order_tracking_order_id', array( $this, 'find_order_by_order_number' ) );
55
+
56
+ // WC Subscriptions support: prevent unnecessary order meta from polluting parent renewal orders, and set order number for subscription orders
57
+ add_filter( 'woocommerce_subscriptions_renewal_order_meta_query', array( $this, 'subscriptions_remove_renewal_order_meta' ), 10, 4 );
58
+ add_action( 'woocommerce_subscriptions_renewal_order_created', array( $this, 'subscriptions_set_sequential_order_number' ), 10, 4 );
59
+
60
+ if ( is_admin() ) {
61
+ add_filter( 'request', array( $this, 'woocommerce_custom_shop_order_orderby' ), 20 );
62
+ add_filter( 'woocommerce_shop_order_search_fields', array( $this, 'custom_search_fields' ) );
63
+
64
+ // sort by underlying _order_number on the Pre-Orders table
65
+ add_filter( 'wc_pre_orders_edit_pre_orders_request', array( $this, 'custom_orderby' ) );
66
+ add_filter( 'wc_pre_orders_search_fields', array( $this, 'custom_search_fields' ) );
67
+ }
68
+
69
+ // Installation
70
+ if ( is_admin() && ! defined( 'DOING_AJAX' ) ) $this->install();
71
+ }
72
+
73
+
74
+ /**
75
+ * Search for an order with order_number $order_number
76
+ *
77
+ * @param string $order_number order number to search for
78
+ *
79
+ * @return int post_id for the order identified by $order_number, or 0
80
+ */
81
+ public function find_order_by_order_number( $order_number ) {
82
+
83
+ // search for the order by custom order number
84
+ $query_args = array(
85
+ 'numberposts' => 1,
86
+ 'meta_key' => '_order_number',
87
+ 'meta_value' => $order_number,
88
+ 'post_type' => 'shop_order',
89
+ 'post_status' => 'publish',
90
+ 'fields' => 'ids',
91
+ );
92
+
93
+ list( $order_id ) = get_posts( $query_args );
94
+
95
+ // order was found
96
+ if ( $order_id !== null ) return $order_id;
97
+
98
+ // if we didn't find the order, then it may be that this plugin was disabled and an order was placed in the interim
99
+ $order = new WC_Order( $order_number );
100
+ if ( isset( $order->order_custom_fields['_order_number'][0] ) ) {
101
+ // _order_number was set, so this is not an old order, it's a new one that just happened to have post_id that matched the searched-for order_number
102
+ return 0;
103
+ }
104
+
105
+ return $order->id;
106
+ }
107
+
108
+
109
+ /**
110
+ * Set the _order_number field for the newly created order
111
+ *
112
+ * @param int $post_id post identifier
113
+ * @param object $post post object
114
+ */
115
+ public function set_sequential_order_number( $post_id, $post ) {
116
+ global $wpdb;
117
+
118
+ if ( 'shop_order' == $post->post_type && 'auto-draft' != $post->post_status ) {
119
+ $order_number = get_post_meta( $post_id, '_order_number', true );
120
+ if ( "" == $order_number ) {
121
+
122
+ // attempt the query up to 3 times for a much higher success rate if it fails (due to Deadlock)
123
+ $success = false;
124
+ for ( $i = 0; $i < 3 && ! $success; $i++ ) {
125
+ // this seems to me like the safest way to avoid order number clashes
126
+ $query = $wpdb->prepare( "
127
+ INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value)
128
+ SELECT %d, '_order_number', IF( MAX( CAST( meta_value as UNSIGNED ) ) IS NULL, 1, MAX( CAST( meta_value as UNSIGNED ) ) + 1 )
129
+ FROM {$wpdb->postmeta}
130
+ WHERE meta_key='_order_number'",
131
+ $post_id );
132
+
133
+ $success = $wpdb->query( $query );
134
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
135
  }
136
+ }
137
+ }
138
+
139
+
140
+ /**
141
+ * Filter to return our _order_number field rather than the post ID,
142
+ * for display.
143
+ *
144
+ * @param string $order_number the order id with a leading hash
145
+ * @param WC_Order $order the order object
146
+ *
147
+ * @return string custom order number, with leading hash
148
+ */
149
+ public function get_order_number( $order_number, $order ) {
150
+ if ( isset( $order->order_custom_fields['_order_number'] ) ) {
151
+ return '#' . $order->order_custom_fields['_order_number'][0];
152
+ }
153
+
154
+ return $order_number;
155
+ }
156
+
157
+
158
+ /** Admin filters ******************************************************/
159
+
160
+
161
+ /**
162
+ * Admin order table orderby ID operates on our meta _order_number
163
+ *
164
+ * @param array $vars associative array of orderby parameteres
165
+ *
166
+ * @return array associative array of orderby parameteres
167
+ */
168
+ public function woocommerce_custom_shop_order_orderby( $vars ) {
169
+ global $typenow, $wp_query;
170
+ if ( 'shop_order' == $typenow ) return $vars;
171
+
172
+ return $this->custom_orderby( $vars );
173
+ }
174
+
175
+
176
+ /**
177
+ * Mofifies the given $args argument to sort on our meta integral _order_number
178
+ *
179
+ * @since 1.3
180
+ * @param array $vars associative array of orderby parameteres
181
+ * @return array associative array of orderby parameteres
182
+ */
183
+ public function custom_orderby( $args ) {
184
+ // Sorting
185
+ if ( isset( $args['orderby'] ) && 'ID' == $args['orderby'] ) {
186
+ $args = array_merge( $args, array(
187
+ 'meta_key' => '_order_number', // sort on numerical portion for better results
188
+ 'orderby' => 'meta_value_num',
189
+ ) );
190
+ }
191
+
192
+ return $args;
193
+ }
194
+
195
+
196
+ /**
197
+ * Add our custom _order_number to the set of search fields so that
198
+ * the admin search functionality is maintained
199
+ *
200
+ * @param array $search_fields array of post meta fields to search by
201
+ *
202
+ * @return array of post meta fields to search by
203
+ */
204
+ public function custom_search_fields( $search_fields ) {
205
+
206
+ array_push( $search_fields, '_order_number' );
207
+
208
+ return $search_fields;
209
+ }
210
+
211
+
212
+ /** 3rd Party Plugin Support ******************************************************/
213
+
214
+
215
+ /**
216
+ * Sets an order number on a subscriptions-created order
217
+ *
218
+ * @since 1.3
219
+ *
220
+ * @param WC_Order $renewal_order the new renewal order object
221
+ * @param WC_Order $original_order the original order object
222
+ * @param int $product_id the product post identifier
223
+ * @param string $new_order_role the role the renewal order is taking, one of 'parent' or 'child'
224
+ */
225
+ public function subscriptions_set_sequential_order_number( $renewal_order, $original_order, $product_id, $new_order_role ) {
226
+ $order_post = get_post( $renewal_order->id );
227
+ $this->set_sequential_order_number( $order_post->ID, $order_post );
228
+ }
229
+
230
+
231
+ /**
232
+ * Don't copy over order number meta when creating a parent or child renewal order
233
+ *
234
+ * @since 1.3
235
+ *
236
+ * @param array $order_meta_query query for pulling the metadata
237
+ * @param int $original_order_id Post ID of the order being used to purchased the subscription being renewed
238
+ * @param int $renewal_order_id Post ID of the order created for renewing the subscription
239
+ * @param string $new_order_role The role the renewal order is taking, one of 'parent' or 'child'
240
+ * @return string
241
+ */
242
+ public function subscriptions_remove_renewal_order_meta( $order_meta_query, $original_order_id, $renewal_order_id, $new_order_role ) {
243
+
244
+ $order_meta_query .= " AND meta_key NOT IN ( '_order_number' )";
245
+
246
+ return $order_meta_query;
247
+ }
248
+
249
+
250
+ /** Helper Methods ******************************************************/
251
+
252
+
253
+ /**
254
+ * Checks if WooCommerce is active
255
+ *
256
+ * @since 1.3
257
+ * @return bool true if WooCommerce is active, false otherwise
258
+ */
259
+ public static function is_woocommerce_active() {
260
+
261
+ $active_plugins = (array) get_option( 'active_plugins', array() );
262
+
263
+ if ( is_multisite() )
264
+ $active_plugins = array_merge( $active_plugins, get_site_option( 'active_sitewide_plugins', array() ) );
265
+
266
+ return in_array( 'woocommerce/woocommerce.php', $active_plugins ) || array_key_exists( 'woocommerce/woocommerce.php', $active_plugins );
267
+ }
268
+
269
+
270
+ /** Lifecycle methods ******************************************************/
271
+
272
+
273
+ /**
274
+ * Run every time. Used since the activation hook is not executed when updating a plugin
275
+ */
276
+ private function install() {
277
+ $installed_version = get_option( WC_Seq_Order_Number::VERSION_OPTION_NAME );
278
+
279
+ if ( ! $installed_version ) {
280
+ // initial install, set the order number for all existing orders to the post id
281
+ $orders = get_posts( array( 'numberposts' => '', 'post_type' => 'shop_order', 'nopaging' => true ) );
282
+ if ( is_array( $orders ) ) {
283
+ foreach( $orders as $order ) {
284
+ if ( '' == get_post_meta( $order->ID, '_order_number', true ) ) {
285
+ add_post_meta( $order->ID, '_order_number', $order->ID );
286
  }
287
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
288
  }
289
  }
290
+
291
+ if ( $installed_version != WC_Seq_Order_Number::VERSION ) {
292
+ $this->upgrade( $installed_version );
293
+
294
+ // new version number
295
+ update_option( WC_Seq_Order_Number::VERSION_OPTION_NAME, WC_Seq_Order_Number::VERSION );
296
+ }
297
+ }
298
+
299
+
300
+ /**
301
+ * Run when plugin version number changes
302
+ */
303
+ private function upgrade( $installed_version ) {
304
+ // upgrade code goes here
305
  }
306
+ }