WooCommerce Sequential Order Numbers - Version 1.2.1

Version Description

  • Minor updates due to WooCommerce 1.5.5 release
Download this release

Release Info

Developer FoxRunSoftware
Plugin Icon WooCommerce Sequential Order Numbers
Version 1.2.1
Comparing to
See all releases

Code changes from version 1.2.0 to 1.2.1

readme.txt CHANGED
@@ -3,7 +3,7 @@ Contributors: FoxRunSoftware
3
  Tags: woocommerce, order number
4
  Requires at least: 3.3
5
  Tested up to: 3.3
6
- Stable tag: 1.2.0
7
 
8
  This plugin extends the WooCommerce e-commerce plugin by setting sequential order numbers for new orders.
9
 
@@ -11,7 +11,7 @@ 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
- This plugin requires WooCommerce 1.5.3 or greater.
15
 
16
  = Feedback =
17
  * I am open to your suggestions and feedback - Thank you for using or trying out one of my plugins!
@@ -28,6 +28,9 @@ This plugin requires WooCommerce 1.5.3 or greater.
28
 
29
  == Changelog ==
30
 
 
 
 
31
  = 1.2.0 =
32
  * Added support for the order tracking page
33
 
3
  Tags: woocommerce, order number
4
  Requires at least: 3.3
5
  Tested up to: 3.3
6
+ Stable tag: 1.2.1
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
+ This plugin requires WooCommerce 1.5.5 or greater.
15
 
16
  = Feedback =
17
  * I am open to your suggestions and feedback - Thank you for using or trying out one of my plugins!
28
 
29
  == Changelog ==
30
 
31
+ = 1.2.1 =
32
+ * Minor updates due to WooCommerce 1.5.5 release
33
+
34
  = 1.2.0 =
35
  * Added support for the order tracking page
36
 
woocommerce-sequential-order-numbers.php CHANGED
@@ -5,7 +5,7 @@ Plugin URI: http://www.foxrunsoftware.net/articles/wordpress/woocommerce-sequent
5
  Description: Provides sequential order numbers for WooCommerce orders
6
  Author: Justin Stern
7
  Author URI: http://www.foxrunsoftware.net
8
- Version: 1.2.0
9
 
10
  Copyright: © 2012 Justin Stern (email : justin@foxrunsoftware.net)
11
  License: GNU General Public License v3.0
@@ -18,23 +18,23 @@ Version: 1.2.0
18
  if ( ! function_exists( 'is_woocommerce_active' ) ) require_once( 'woo-includes/woo-functions.php' );
19
 
20
 
21
- if (is_woocommerce_active()) {
22
- if (!class_exists('WC_Seq_Order_Number')) {
23
 
24
  class WC_Seq_Order_Number {
25
- const VERSION = "1.2.0";
26
  const VERSION_OPTION_NAME = "woocommerce_seq_order_number_db_version";
27
 
28
  public function __construct() {
29
 
30
  // actions/filters
31
- add_action('woocommerce_init', array(&$this, 'woocommerce_loaded' ));
32
- add_action('wp_insert_post', array(&$this, 'set_sequential_order_number'), 10, 2);
33
- add_filter('woocommerce_order_number', array(&$this, 'get_order_number'), 10, 2);
34
- add_filter('woocommerce_email_subject_new_order', array(&$this, 'email_subject_new_order'), 10, 2);
35
 
36
  // Installation
37
- if (is_admin() && !defined('DOING_AJAX')) $this->install();
38
  }
39
 
40
 
@@ -49,18 +49,17 @@ if (is_woocommerce_active()) {
49
  remove_shortcode( 'woocommerce_order_tracking', 'get_woocommerce_order_tracking' );
50
  add_shortcode( 'woocommerce_order_tracking', array( &$this, 'get_woocommerce_order_tracking' ) );
51
 
52
- if (is_admin()) {
53
  // Override a bunch of admin functionality to support the sequential order numbers on the backend, unfortunately...
54
- remove_action('manage_shop_order_posts_custom_column', 'woocommerce_custom_order_columns', 2);
55
- add_action('manage_shop_order_posts_custom_column', array(&$this,'woocommerce_custom_order_columns'), 2);
56
 
57
- remove_filter( 'request', 'woocommerce_custom_shop_order_orderby' );
58
- add_filter( 'request', array(&$this, 'woocommerce_custom_shop_order_orderby' ));
59
 
60
  remove_filter( 'parse_query', 'woocommerce_shop_order_search_custom_fields' );
61
- add_filter( 'parse_query', array(&$this, 'woocommerce_shop_order_search_custom_fields' ));
62
 
63
- add_action( 'add_meta_boxes', array(&$this, 'woocommerce_meta_boxes'), 20 );
64
  }
65
  }
66
 
@@ -78,6 +77,9 @@ if (is_woocommerce_active()) {
78
  /**
79
  * Order Tracking page shortcode, largely unchanged from the original
80
  * this one just searches by order_number
 
 
 
81
  */
82
  public function woocommerce_order_tracking( $atts ) {
83
  global $woocommerce;
@@ -127,6 +129,9 @@ if (is_woocommerce_active()) {
127
  /**
128
  * Largely unchanged from the WooCommerce original, just one point
129
  * change identified below
 
 
 
130
  */
131
  function woocommerce_custom_order_columns($column) {
132
 
@@ -193,7 +198,7 @@ if (is_woocommerce_active()) {
193
  endif;
194
  break;
195
  case "total_cost" :
196
- echo woocommerce_price($order->order_total);
197
  break;
198
  case "order_date" :
199
 
@@ -242,21 +247,15 @@ if (is_woocommerce_active()) {
242
 
243
 
244
  /**
245
- * Largely unchanged from the WooCommerce original, just changed orderby
246
- * ID to operate on the meta _order_number
247
  */
248
  function woocommerce_custom_shop_order_orderby( $vars ) {
249
  global $typenow, $wp_query;
250
- if ($typenow!='shop_order') return $vars;
251
 
252
  // Sorting
253
- if (isset( $vars['orderby'] )) :
254
- if ( 'order_total' == $vars['orderby'] ) :
255
- $vars = array_merge( $vars, array(
256
- 'meta_key' => '_order_total',
257
- 'orderby' => 'meta_value_num'
258
- ) );
259
- elseif ( 'ID' == $vars['orderby'] ) : // JES - added this
260
  $vars = array_merge( $vars, array(
261
  'meta_key' => '_order_number',
262
  'orderby' => 'meta_value_num'
@@ -271,6 +270,9 @@ if (is_woocommerce_active()) {
271
 
272
  /**
273
  * Largely unchanged from original, just one point change noted below
 
 
 
274
  */
275
  function woocommerce_shop_order_search_custom_fields( $wp ) {
276
  global $pagenow, $wpdb;
@@ -344,13 +346,16 @@ if (is_woocommerce_active()) {
344
  */
345
  function woocommerce_meta_boxes() {
346
  remove_meta_box( 'woocommerce-order-data', 'shop_order', 'normal' );
347
- add_meta_box( 'woocommerce-order-data', __('Order Data', 'woocommerce'), array(&$this,'woocommerce_order_data_meta_box'), 'shop_order', 'normal', 'high' );
348
  }
349
 
350
 
351
  /**
352
  * Largely unchanged from the WooCommerce original, just one point
353
  * change identified below
 
 
 
354
  */
355
  function woocommerce_order_data_meta_box($post) {
356
 
@@ -597,15 +602,15 @@ if (is_woocommerce_active()) {
597
  function set_sequential_order_number( $post_id, $post ) {
598
  global $wpdb;
599
 
600
- if ($post->post_type == 'shop_order' ) {
601
- $order_number = get_post_meta($post_id, '_order_number', true);
602
- if($order_number == "") {
603
 
604
  // attempt the query up to 3 times for a much higher success rate if it fails (due to Deadlock)
605
  $success = false;
606
- for($i = 0; $i < 3 && !$success; $i++) {
607
  // this seems to me like the safest way to avoid order number clashes
608
- $success = $wpdb->query($wpdb->prepare('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"'));
609
  }
610
  }
611
  }
@@ -615,9 +620,9 @@ if (is_woocommerce_active()) {
615
  /**
616
  * Filter to return our _order_number field rather than the post ID
617
  */
618
- function get_order_number($order_number, $order) {
619
- if(isset($order->order_custom_fields['_order_number'])) {
620
- return '#'.$order->order_custom_fields['_order_number'][0];
621
  }
622
  return $order_number;
623
  }
@@ -626,8 +631,8 @@ if (is_woocommerce_active()) {
626
  /**
627
  * Fix the admin new order email
628
  */
629
- function email_subject_new_order($subject, $order) {
630
- $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);
631
 
632
  return sprintf( __( '[%s] New Customer Order (%s)', 'woocommerce' ), $blogname, $order->get_order_number() );
633
  }
@@ -680,32 +685,32 @@ if (is_woocommerce_active()) {
680
  * Run every time. Used since the activation hook is not executed when updating a plugin
681
  */
682
  private function install() {
683
- $installed_version = get_option(WC_Seq_Order_Number::VERSION_OPTION_NAME);
684
 
685
- if(!$installed_version) {
686
  // initial install, set the order number for all existing orders to the post id
687
- $orders = get_posts(array('numberposts' => '', 'post_type' => 'shop_order'));
688
- if(is_array($orders)) {
689
- foreach($orders as $order) {
690
- if(get_post_meta($order->ID, '_order_number', true) == '') {
691
- add_post_meta($order->ID, '_order_number', $order->ID);
692
  }
693
  }
694
  }
695
  }
696
 
697
- if($installed_version != WC_Seq_Order_Number::VERSION) {
698
- $this->upgrade($installed_version);
699
 
700
  // new version number
701
- update_option(WC_Seq_Order_Number::VERSION_OPTION_NAME, WC_Seq_Order_Number::VERSION);
702
  }
703
  }
704
 
705
  /**
706
  * Run when plugin version number changes
707
  */
708
- private function upgrade($installed_version) {
709
  // upgrade code goes here
710
  }
711
  }
5
  Description: Provides sequential order numbers for WooCommerce orders
6
  Author: Justin Stern
7
  Author URI: http://www.foxrunsoftware.net
8
+ Version: 1.2.1
9
 
10
  Copyright: © 2012 Justin Stern (email : justin@foxrunsoftware.net)
11
  License: GNU General Public License v3.0
18
  if ( ! function_exists( 'is_woocommerce_active' ) ) require_once( 'woo-includes/woo-functions.php' );
19
 
20
 
21
+ if ( is_woocommerce_active() ) {
22
+ if ( ! class_exists( 'WC_Seq_Order_Number' ) ) {
23
 
24
  class WC_Seq_Order_Number {
25
+ const VERSION = "1.2.1";
26
  const VERSION_OPTION_NAME = "woocommerce_seq_order_number_db_version";
27
 
28
  public function __construct() {
29
 
30
  // actions/filters
31
+ add_action( 'woocommerce_init', array( &$this, 'woocommerce_loaded' ) );
32
+ add_action( 'wp_insert_post', array( &$this, 'set_sequential_order_number' ), 10, 2 );
33
+ add_filter( 'woocommerce_order_number', array( &$this, 'get_order_number' ), 10, 2);
34
+ add_filter( 'woocommerce_email_subject_new_order', array( &$this, 'email_subject_new_order' ), 10, 2 );
35
 
36
  // Installation
37
+ if ( is_admin() && ! defined( 'DOING_AJAX' ) ) $this->install();
38
  }
39
 
40
 
49
  remove_shortcode( 'woocommerce_order_tracking', 'get_woocommerce_order_tracking' );
50
  add_shortcode( 'woocommerce_order_tracking', array( &$this, 'get_woocommerce_order_tracking' ) );
51
 
52
+ if ( is_admin() ) {
53
  // Override a bunch of admin functionality to support the sequential order numbers on the backend, unfortunately...
54
+ remove_action( 'manage_shop_order_posts_custom_column', 'woocommerce_custom_order_columns', 2 );
55
+ add_action( 'manage_shop_order_posts_custom_column', array( &$this, 'woocommerce_custom_order_columns' ), 2 );
56
 
57
+ add_filter( 'request', array( &$this, 'woocommerce_custom_shop_order_orderby' ), 20 );
 
58
 
59
  remove_filter( 'parse_query', 'woocommerce_shop_order_search_custom_fields' );
60
+ add_filter( 'parse_query', array( &$this, 'woocommerce_shop_order_search_custom_fields' ) );
61
 
62
+ add_action( 'add_meta_boxes', array( &$this, 'woocommerce_meta_boxes' ), 20 );
63
  }
64
  }
65
 
77
  /**
78
  * Order Tracking page shortcode, largely unchanged from the original
79
  * this one just searches by order_number
80
+ *
81
+ * Code based on WooCommerce woocommerce_order_tracking()
82
+ * @see woocommerce/shortcodes/shortcode-order_tracking.php
83
  */
84
  public function woocommerce_order_tracking( $atts ) {
85
  global $woocommerce;
129
  /**
130
  * Largely unchanged from the WooCommerce original, just one point
131
  * change identified below
132
+ *
133
+ * Code based on WooCommerce 1.5.5 woocommerce_custom_order_columns()
134
+ * @see woocommerce/admin/post-types/shop_order.php
135
  */
136
  function woocommerce_custom_order_columns($column) {
137
 
198
  endif;
199
  break;
200
  case "total_cost" :
201
+ echo $order->get_formatted_order_total();
202
  break;
203
  case "order_date" :
204
 
247
 
248
 
249
  /**
250
+ * Orderby ID operates on the meta _order_number
 
251
  */
252
  function woocommerce_custom_shop_order_orderby( $vars ) {
253
  global $typenow, $wp_query;
254
+ if ( $typenow != 'shop_order' ) return $vars;
255
 
256
  // Sorting
257
+ if ( isset( $vars['orderby'] ) ) :
258
+ if ( 'ID' == $vars['orderby'] ) :
 
 
 
 
 
259
  $vars = array_merge( $vars, array(
260
  'meta_key' => '_order_number',
261
  'orderby' => 'meta_value_num'
270
 
271
  /**
272
  * Largely unchanged from original, just one point change noted below
273
+ *
274
+ * Code based on WooCommerce 1.5.5 woocommerce_shop_order_search_custom_fields()
275
+ * @see woocommerce/admin/post-types/shop_order.php
276
  */
277
  function woocommerce_shop_order_search_custom_fields( $wp ) {
278
  global $pagenow, $wpdb;
346
  */
347
  function woocommerce_meta_boxes() {
348
  remove_meta_box( 'woocommerce-order-data', 'shop_order', 'normal' );
349
+ add_meta_box( 'woocommerce-order-data', __( 'Order Data', 'woocommerce' ), array( &$this,'woocommerce_order_data_meta_box' ), 'shop_order', 'normal', 'high' );
350
  }
351
 
352
 
353
  /**
354
  * Largely unchanged from the WooCommerce original, just one point
355
  * change identified below
356
+ *
357
+ * Code based on WooCommerce 1.5.5 woocommerce_order_data_meta_box()
358
+ * @see woocommerce/admin/post-types/writepanels/writepanel-order_data.php
359
  */
360
  function woocommerce_order_data_meta_box($post) {
361
 
602
  function set_sequential_order_number( $post_id, $post ) {
603
  global $wpdb;
604
 
605
+ if ( $post->post_type == 'shop_order' ) {
606
+ $order_number = get_post_meta( $post_id, '_order_number', true );
607
+ if ( $order_number == "" ) {
608
 
609
  // attempt the query up to 3 times for a much higher success rate if it fails (due to Deadlock)
610
  $success = false;
611
+ for ( $i = 0; $i < 3 && ! $success; $i++ ) {
612
  // this seems to me like the safest way to avoid order number clashes
613
+ $success = $wpdb->query( $wpdb->prepare( '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"' ) );
614
  }
615
  }
616
  }
620
  /**
621
  * Filter to return our _order_number field rather than the post ID
622
  */
623
+ function get_order_number( $order_number, $order ) {
624
+ if ( isset( $order->order_custom_fields['_order_number'] ) ) {
625
+ return '#' . $order->order_custom_fields['_order_number'][0];
626
  }
627
  return $order_number;
628
  }
631
  /**
632
  * Fix the admin new order email
633
  */
634
+ function email_subject_new_order( $subject, $order ) {
635
+ $blogname = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES );
636
 
637
  return sprintf( __( '[%s] New Customer Order (%s)', 'woocommerce' ), $blogname, $order->get_order_number() );
638
  }
685
  * Run every time. Used since the activation hook is not executed when updating a plugin
686
  */
687
  private function install() {
688
+ $installed_version = get_option( WC_Seq_Order_Number::VERSION_OPTION_NAME );
689
 
690
+ if ( ! $installed_version ) {
691
  // initial install, set the order number for all existing orders to the post id
692
+ $orders = get_posts( array( 'numberposts' => '', 'post_type' => 'shop_order' ) );
693
+ if ( is_array( $orders ) ) {
694
+ foreach( $orders as $order ) {
695
+ if ( get_post_meta( $order->ID, '_order_number', true ) == '' ) {
696
+ add_post_meta( $order->ID, '_order_number', $order->ID );
697
  }
698
  }
699
  }
700
  }
701
 
702
+ if ( $installed_version != WC_Seq_Order_Number::VERSION ) {
703
+ $this->upgrade( $installed_version );
704
 
705
  // new version number
706
+ update_option( WC_Seq_Order_Number::VERSION_OPTION_NAME, WC_Seq_Order_Number::VERSION );
707
  }
708
  }
709
 
710
  /**
711
  * Run when plugin version number changes
712
  */
713
+ private function upgrade( $installed_version ) {
714
  // upgrade code goes here
715
  }
716
  }