Skip to content

Commit c733a1b

Browse files
authored
Merge pull request #12 from skyverge/wc-2.7-compat-10
WooCommerce 2.7 Compatibility; closes #10
2 parents 0e531f6 + bf0247a commit c733a1b

File tree

8 files changed

+319
-43
lines changed

8 files changed

+319
-43
lines changed

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"name": "woocommerce-sequential-order-numbers",
3-
"version": "1.7.0",
4-
"author": "SkyVerge Team",
3+
"version": "1.8.0",
4+
"author": "SkyVerge",
55
"homepage": "http://skyverge.com",
66
"repository": {
77
"type": "git",

readme.txt

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
=== WooCommerce Sequential Order Numbers ===
2-
Contributors: SkyVerge, maxrice, tamarazuk, chasewiseman, nekojira
3-
Tags: woocommerce, order number
2+
Contributors: SkyVerge, maxrice, tamarazuk, chasewiseman, nekojira, beka.rice
3+
Tags: woocommerce, order number, sequential order number, woocommerce orders
44
Requires at least: 4.1
5-
Tested up to: 4.6.1.
6-
Requires WooCommerce at least: 2.4.13
7-
Tested WooCommerce up to: 2.6.n
8-
Stable tag: 1.7.0
5+
Tested up to: 4.7.3
6+
Requires WooCommerce at least: 2.5.5
7+
Tested WooCommerce up to: 3.0
8+
Stable tag: 1.8.0
99

1010
This plugin extends WooCommerce by setting sequential order numbers for new orders.
1111

1212
== Description ==
1313

1414
This plugin extends WooCommerce by automatically 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.
1515

16-
**This plugin requires WooCommerce 2.4.13 or newer.**
16+
**This plugin requires WooCommerce 2.5.5 or newer.**
1717

1818
> No configuration needed! The plugin is so easy to use, there aren't even any settings. Activate it, and orders will automatically become sequential.
1919

@@ -27,11 +27,11 @@ We do support our free plugins and extensions, but please understand that suppor
2727

2828
= Sequential Order Numbers Pro =
2929

30-
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, which is available in the [WooThemes Store](http://www.woothemes.com/products/sequential-order-numbers-pro/).
30+
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, which is available in the [WooCommerce Store](http://woocommerce.com/products/sequential-order-numbers-pro/).
3131

3232
= More Details =
3333
- See the [product page](http://www.skyverge.com/product/woocommerce-sequential-order-numbers/) for full details.
34-
- Check out the [Pro Version](http://www.woothemes.com/products/sequential-order-numbers-pro/).
34+
- Check out the [Pro Version](http://woocommerce.com/products/sequential-order-numbers-pro/).
3535
- View more of SkyVerge's [free WooCommerce extensions](http://profiles.wordpress.org/skyverge/)
3636
- View all [SkyVerge WooCommerce extensions](http://www.skyverge.com/shop/)
3737

@@ -103,8 +103,10 @@ $order_number = $order->get_order_number();
103103

104104
== Changelog ==
105105

106-
= 1.7.0-1 - 2016.nn.nn =
106+
= 1.8.0 - 2017.03.23 =
107107
* Fix - Admin orderby was not properly scoped to orders, props [@brandondove](https://github.com/brandondove)
108+
* Misc - Added support for WooCommerce 3.0
109+
* Misc - Removed support for WooCommerce 2.4
108110

109111
= 1.7.0 - 2016.05.24 =
110112
* Misc - Added support for WooCommerce 2.6

woocommerce-sequential-order-numbers.php

Lines changed: 96 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,19 @@
55
* Description: Provides sequential order numbers for WooCommerce orders
66
* Author: SkyVerge
77
* Author URI: http://www.skyverge.com
8-
* Version: 1.7.0-1
8+
* Version: 1.8.0
99
* Text Domain: woocommerce-sequential-order-numbers
1010
* Domain Path: /i18n/languages/
1111
*
12-
* Copyright: (c) 2012-2016 SkyVerge, Inc. ([email protected])
12+
* Copyright: (c) 2012-2017, SkyVerge, Inc. ([email protected])
1313
*
1414
* License: GNU General Public License v3.0
1515
* License URI: http://www.gnu.org/licenses/gpl-3.0.html
1616
*
1717
* @package WC-Sequential-Order-Numbers
1818
* @author SkyVerge
1919
* @category Plugin
20-
* @copyright Copyright (c) 2012-2016, SkyVerge, Inc.
20+
* @copyright Copyright (c) 2012-2017, SkyVerge, Inc.
2121
* @license http://www.gnu.org/licenses/gpl-3.0.html GNU General Public License v3.0
2222
*/
2323

@@ -32,7 +32,7 @@ class WC_Seq_Order_Number {
3232

3333

3434
/** version number */
35-
const VERSION = '1.7.0-1';
35+
const VERSION = '1.8.0';
3636

3737
/** @var \WC_Seq_Order_Number single instance of this plugin */
3838
protected static $instance;
@@ -41,7 +41,7 @@ class WC_Seq_Order_Number {
4141
const VERSION_OPTION_NAME = 'woocommerce_seq_order_number_db_version';
4242

4343
/** minimum required wc version */
44-
const MINIMUM_WC_VERSION = '2.4.13';
44+
const MINIMUM_WC_VERSION = '2.5.5';
4545

4646

4747
/**
@@ -174,27 +174,28 @@ public function find_order_by_order_number( $order_number ) {
174174
return 0;
175175
}
176176

177-
if ( $order->order_number ) {
177+
if ( self::get_order_meta( $order, '_order_number' ) ) {
178178
// _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
179179
return 0;
180180
}
181181

182-
return $order->id;
182+
return self::get_order_prop( $order, 'id' );
183183
}
184184

185185

186186
/**
187187
* Set the _order_number field for the newly created order
188188
*
189189
* @param int $post_id post identifier
190-
* @param WP_Post $post post object
190+
* @param \WP_Post $post post object
191191
*/
192192
public function set_sequential_order_number( $post_id, $post ) {
193193
global $wpdb;
194194

195195
if ( 'shop_order' === $post->post_type && 'auto-draft' !== $post->post_status ) {
196196

197-
$order_number = get_post_meta( $post_id, '_order_number', true );
197+
$order = wc_get_order( $post_id );
198+
$order_number = self::get_order_meta( $order, '_order_number' );
198199

199200
if ( '' === $order_number ) {
200201

@@ -203,6 +204,7 @@ public function set_sequential_order_number( $post_id, $post ) {
203204
for ( $i = 0; $i < 3 && ! $success; $i++ ) {
204205

205206
// this seems to me like the safest way to avoid order number clashes
207+
// By the time this is outdated, it's likely no longer needed anyway {BR 2017-03-08}
206208
$query = $wpdb->prepare( "
207209
INSERT INTO {$wpdb->postmeta} (post_id, meta_key, meta_value)
208210
SELECT %d, '_order_number', IF( MAX( CAST( meta_value as UNSIGNED ) ) IS NULL, 1, MAX( CAST( meta_value as UNSIGNED ) ) + 1 )
@@ -227,8 +229,8 @@ public function set_sequential_order_number( $post_id, $post ) {
227229
*/
228230
public function get_order_number( $order_number, $order ) {
229231

230-
if ( $order->order_number ) {
231-
return $order->order_number;
232+
if ( self::get_order_meta( $order, '_order_number' ) ) {
233+
$order_number = self::get_order_meta( $order, '_order_number' );
232234
}
233235

234236
return $order_number;
@@ -245,7 +247,7 @@ public function get_order_number( $order_number, $order ) {
245247
* @return array associative array of orderby parameteres
246248
*/
247249
public function woocommerce_custom_shop_order_orderby( $vars ) {
248-
global $typenow, $wp_query;
250+
global $typenow;
249251

250252
if ( 'shop_order' !== $typenow ) {
251253
return $vars;
@@ -309,6 +311,8 @@ protected static function get_wc_subscriptions_version() {
309311
/**
310312
* Returns true if the installed version of WooCommerce Subscriptions is 2.0.0 or greater
311313
*
314+
* TODO: Drop Subs < 2.0 support with WC 3.1 compat {BR 2017-03-21}
315+
*
312316
* @since 1.5.1
313317
* @return boolean
314318
*/
@@ -321,9 +325,9 @@ protected static function is_wc_subscriptions_version_gte_2_0() {
321325
* Sets an order number on a subscriptions-created order
322326
*
323327
* @since 1.3
324-
* @param WC_Order $renewal_order the new renewal order object
325-
* @param WC_Order $original_order the original order object (Subscriptions 2.0+: Subscription object)
326-
* @return void|WC_Order Void for Subscriptions 1.5, renewal order instance for Subscriptions 2.0+
328+
* @param \WC_Order $renewal_order the new renewal order object
329+
* @param \WC_Order $original_order the original order object (Subscriptions 2.0+: Subscription object)
330+
* @return void|\WC_Order Void for Subscriptions 1.5, renewal order instance for Subscriptions 2.0+
327331
*/
328332
public function subscriptions_set_sequential_order_number( $renewal_order, $original_order ) {
329333

@@ -388,39 +392,90 @@ public static function is_woocommerce_active() {
388392
}
389393

390394

391-
/** Compatibility Methods ******************************************************/
395+
/**
396+
* Helper method to get order properties pre and post WC 3.0.
397+
*
398+
* TODO: Remove this when WooCommerce 3.0+ is required and remove helpers {BR 2017-03-08}
399+
*
400+
* @param \WC_Order $order the order for which to get data
401+
* @param string $prop the order property to get
402+
* @param string $context the context for the property, 'edit' or 'view'
403+
* @return mixed the order property
404+
*/
405+
protected static function get_order_prop( WC_Order $order, $prop, $context = 'edit' ) {
406+
407+
$value = '';
408+
409+
if ( self::is_wc_version_gte_3_0() ) {
410+
411+
if ( is_callable( array( $order, "get_{$prop}" ) ) ) {
412+
$value = $order->{"get_{$prop}"}( $context );
413+
}
414+
415+
} else {
416+
417+
// if this is the 'view' context and there is an accessor method, use it
418+
if ( is_callable( array( $order, "get_{$prop}" ) ) && 'view' === $context ) {
419+
$value = $order->{"get_{$prop}"}();
420+
} else {
421+
$value = $order->$prop;
422+
}
423+
}
424+
425+
return $value;
426+
}
392427

393428

394429
/**
395-
* Helper method to get the version of the currently installed WooCommerce
430+
* Helper method to get order meta pre and post WC 3.0.
396431
*
397-
* @since 1.3.2
398-
* @return string woocommerce version number or null
432+
* TODO: Remove this when WooCommerce 3.0+ is required and remove helpers {BR 2017-03-08}
433+
*
434+
* @param \WC_Order $order the order object
435+
* @param string $key the meta key
436+
* @param bool $single whether to get the meta as a single item. Defaults to `true`
437+
* @param string $context if 'view' then the value will be filtered
438+
* @return mixed the order property
399439
*/
400-
private static function get_wc_version() {
401-
return defined( 'WC_VERSION' ) && WC_VERSION ? WC_VERSION : null;
440+
protected static function get_order_meta( WC_Order $order, $key = '', $single = true, $context = 'edit' ) {
441+
442+
if ( self::is_wc_version_gte_3_0() ) {
443+
444+
$value = $order->get_meta( $key, $single, $context );
445+
446+
} else {
447+
448+
$order_id = is_callable( array( $order, 'get_id' ) ) ? $order->get_id() : $order->id;
449+
$value = get_post_meta( $order_id, $key, $single );
450+
451+
}
452+
453+
return $value;
402454
}
403455

404456

457+
/** Compatibility Methods ******************************************************/
458+
459+
405460
/**
406-
* Returns true if the installed version of WooCommerce is 2.5 or greater
461+
* Helper method to get the version of the currently installed WooCommerce
407462
*
408-
* @since 1.6.0
409-
* @return boolean true if the installed version of WooCommerce is 2.5 or greater
463+
* @since 1.3.2
464+
* @return string woocommerce version number or null
410465
*/
411-
public static function is_wc_version_gte_2_5() {
412-
return self::get_wc_version() && version_compare( self::get_wc_version(), '2.5', '>=' );
466+
private static function get_wc_version() {
467+
return defined( 'WC_VERSION' ) && WC_VERSION ? WC_VERSION : null;
413468
}
414469

415470

416471
/**
417-
* Returns true if the installed version of WooCommerce is 2.6 or greater
472+
* Returns true if the installed version of WooCommerce is 3.0 or greater
418473
*
419-
* @since 1.7.0
420-
* @return boolean true if the installed version of WooCommerce is 2.6 or greater
474+
* @since 1.8.0
475+
* @return boolean true if the installed version of WooCommerce is 3.0 or greater
421476
*/
422-
public static function is_wc_version_gte_2_6() {
423-
return self::get_wc_version() && version_compare( self::get_wc_version(), '2.6', '>=' );
477+
private static function is_wc_version_gte_3_0() {
478+
return self::get_wc_version() && version_compare( self::get_wc_version(), '3.0', '>=' );
424479
}
425480

426481

@@ -468,8 +523,11 @@ public function render_update_notices() {
468523

469524
/**
470525
* Run every time. Used since the activation hook is not executed when updating a plugin
526+
*
527+
* @since 1.0.0
471528
*/
472529
private function install() {
530+
473531
$installed_version = get_option( WC_Seq_Order_Number::VERSION_OPTION_NAME );
474532

475533
if ( ! $installed_version ) {
@@ -499,6 +557,8 @@ private function install() {
499557

500558
foreach( $order_ids as $order_id ) {
501559

560+
// TODO: I'm not changing this right now so I don't have to instantiate a new order object for each update
561+
// and if orders move away from posts this plugin doesn't matter anyway {BR 2017-03-08}
502562
if ( '' === get_post_meta( $order_id, '_order_number', true ) ) {
503563
add_post_meta( $order_id, '_order_number', $order_id );
504564
}
@@ -524,6 +584,8 @@ private function install() {
524584

525585
/**
526586
* Run when plugin version number changes
587+
*
588+
* 1.0.0
527589
*/
528590
private function upgrade( $installed_version ) {
529591
// upgrade code goes here
@@ -546,6 +608,9 @@ function wc_sequential_order_numbers() {
546608

547609
/**
548610
* The WC_Seq_Order_Number global object
611+
*
612+
* TODO: Remove the global with WC 3.1 compat {BR 2017-03-21}
613+
*
549614
* @deprecated 1.7.0
550615
* @name $wc_seq_order_number
551616
* @global WC_Seq_Order_Number $GLOBALS['wc_seq_order_number']

wp-assets/banner-1544x500.png

-17 KB
Loading

wp-assets/banner-772x250.png

-35.1 KB
Loading

wp-assets/icon-128x128.png

-7.48 KB
Binary file not shown.

wp-assets/icon-256x256.png

-15.4 KB
Binary file not shown.

0 commit comments

Comments
 (0)