11<?php
2-
32/**
43 * CLI Tool for migrating order data to/from custom table.
54 *
6- * @version 1.0.0
7- * @category Class
5+ * @package WooCommerce_Custom_Order_Tables
6+ * @author Liquid Web
87 */
9- class WC_Custom_Order_Table_CLI extends WP_CLI_Command
10- {
11-
12- private $ count ;
13-
14- /**
15- * Count how many orders have yet to be migrated.
16- *
17- * ## EXAMPLES
18- *
19- * wp wc-order-table count
20- *
21- */
22- public function count () {
23- global $ wpdb ;
24-
25- $ order_table = wc_custom_order_table ()->get_table_name ();
26-
27- $ order_count = $ wpdb ->get_var ( $ wpdb ->prepare ("
28- SELECT COUNT(1)
29- FROM {$ wpdb ->posts } p
30- LEFT JOIN {$ order_table } o ON p.ID = o.order_id
31- WHERE p.post_type IN ('%s')
32- AND o.order_id IS NULL
33- ORDER BY p.post_date DESC
34- " , implode (', ' , wc_get_order_types ('reports ' ))));
35-
36- WP_CLI ::log ( sprintf ( __ ( '%d orders to be migrated. ' , 'wc-custom-order-table ' ), $ order_count ) );
37-
38- return $ order_count ;
39- }
40-
41- /**
42- * Migrate order data to the custom order table.
43- *
44- * ## OPTIONS
45- *
46- * [--batch=<batch>]
47- * : The number of orders to process.
48- * ---
49- * default: 1000
50- * ---
51- *
52- * [--page=<page>]
53- * : The page to start from.
54- * ---
55- * default: 1
56- * ---
57- *
58- * ## EXAMPLES
59- *
60- * wp wc-order-table migrate --batch=100 --page=1
61- *
62- */
63- public function migrate ($ args , $ assoc_args )
64- {
65- global $ wpdb ;
66-
67- $ orders_batch = isset ($ assoc_args ['batch ' ]) ? absint ($ assoc_args ['batch ' ]) : 1000 ;
68- $ orders_page = isset ($ assoc_args ['page ' ]) ? absint ($ assoc_args ['page ' ]) : 1 ;
69-
70- $ order_table = wc_custom_order_table ()->get_table_name ();
71-
72- $ order_count = $ this ->count ();
73-
74- $ total_pages = ceil ($ order_count / $ orders_batch );
75-
76- $ progress = \WP_CLI \Utils \make_progress_bar ('Order Data Migration ' , $ order_count );
77-
78- $ orders_sql = $ wpdb ->prepare ("
79- SELECT ID FROM {$ wpdb ->posts }
80- WHERE post_type IN ('%s')
81- ORDER BY post_date DESC
82- " , implode (', ' , wc_get_order_types ('reports ' )));
83- $ batches_processed = 0 ;
84-
85- for ($ page = $ orders_page ; $ page <= $ total_pages ; $ page ++) {
86- $ offset = ($ page * $ orders_batch ) - $ orders_batch ;
87- $ sql = $ wpdb ->prepare ($ orders_sql . ' LIMIT %d OFFSET %d ' , $ orders_batch , max ($ offset , 0 ));
88- $ orders = $ wpdb ->get_col ($ sql );
89-
90- foreach ($ orders as $ order ) {
91- // Accessing the order via wc_get_order will automatically migrate the order to the custom table.
92- wc_get_order ($ order );
93-
94- $ progress ->tick ();
95- }
96-
97- $ batches_processed ++;
98- }
99-
100- $ progress ->finish ();
101-
102- WP_CLI ::log (sprintf (__ ('%d orders processed in %d batches. ' , 'wc-custom-order-table ' ), $ order_count , $ batches_processed ));
103- }
104-
105- /**
106- * Backfill order meta data into postmeta.
107- *
108- * ## OPTIONS
109- *
110- * [--batch=<batch>]
111- * : The number of orders to process.
112- * ---
113- * default: 1000
114- * ---
115- *
116- * [--page=<page>]
117- * : The page to start from.
118- * ---
119- * default: 1
120- * ---
121- *
122- * ## EXAMPLES
123- *
124- * wp wc-order-table backfill --batch=100 --page=1
125- *
126- */
127- public function backfill ($ args , $ assoc_args )
128- {
129- global $ wpdb ;
130-
131- $ orders_batch = isset ($ assoc_args ['batch ' ]) ? absint ($ assoc_args ['batch ' ]) : 1000 ;
132- $ orders_page = isset ($ assoc_args ['page ' ]) ? absint ($ assoc_args ['page ' ]) : 1 ;
133-
134- $ order_table = wc_custom_order_table ()->get_table_name ();
135-
136- $ order_count = $ wpdb ->get_var ("SELECT COUNT(1) FROM {$ order_table } o " );
137-
138- WP_CLI ::log ( sprintf ( __ ( '%d orders to be backfilled. ' , 'wc-custom-order-table ' ), $ order_count ) );
139-
140- $ total_pages = ceil ($ order_count / $ orders_batch );
141-
142- $ progress = \WP_CLI \Utils \make_progress_bar ('Order Data Migration ' , $ order_count );
143-
144- $ orders_sql = "SELECT order_id FROM {$ order_table } o " ;
145- $ batches_processed = 0 ;
146-
147- for ($ page = $ orders_page ; $ page <= $ total_pages ; $ page ++) {
148- $ offset = ($ page * $ orders_batch ) - $ orders_batch ;
149- $ sql = $ wpdb ->prepare ($ orders_sql . ' LIMIT %d OFFSET %d ' , $ orders_batch , max ($ offset , 0 ));
150- $ orders = $ wpdb ->get_col ($ sql );
151-
152- foreach ($ orders as $ order ) {
153- // Accessing the order via wc_get_order will automatically migrate the order to the custom table.
154- $ order = wc_get_order ($ order );
155- $ order ->get_data_store ()->backfill_postmeta ( $ order );
156-
157- $ progress ->tick ();
158- }
159-
160- $ batches_processed ++;
161- }
162-
163- $ progress ->finish ();
1648
165- WP_CLI ::log (sprintf (__ ('%d orders processed in %d batches. ' , 'wc-custom-order-table ' ), $ order_count , $ batches_processed ));
166- }
167- }
9+ class WC_Custom_Order_Table_CLI extends WP_CLI_Command {
10+
11+ /**
12+ * Count how many orders have yet to be migrated.
13+ *
14+ * ## EXAMPLES
15+ *
16+ * wp wc-order-table count
17+ *
18+ * @global $wpdb
19+ */
20+ public function count () {
21+ global $ wpdb ;
22+
23+ $ order_table = wc_custom_order_table ()->get_table_name ();
24+ $ order_count = $ wpdb ->get_var ( $ wpdb ->prepare (
25+ "SELECT COUNT(1)
26+ FROM {$ wpdb ->posts } p
27+ LEFT JOIN {$ order_table } o ON p.ID = o.order_id
28+ WHERE p.post_type IN ('%s')
29+ AND o.order_id IS NULL
30+ ORDER BY p.post_date DESC " ,
31+ implode ( ', ' , wc_get_order_types ( 'reports ' ) )
32+ ) );
33+
34+ WP_CLI ::log ( sprintf ( __ ( '%d orders to be migrated. ' , 'wc-custom-order-table ' ), $ order_count ) );
35+
36+ return $ order_count ;
37+ }
38+
39+ /**
40+ * Migrate order data to the custom order table.
41+ *
42+ * ## OPTIONS
43+ *
44+ * [--batch=<batch>]
45+ * : The number of orders to process.
46+ * ---
47+ * default: 1000
48+ * ---
49+ *
50+ * [--page=<page>]
51+ * : The page to start from.
52+ * ---
53+ * default: 1
54+ * ---
55+ *
56+ * ## EXAMPLES
57+ *
58+ * wp wc-order-table migrate --batch=100 --page=1
59+ *
60+ * @global $wpdb
61+ *
62+ * @param array $args Positional arguments passed to the command.
63+ * @param array $assoc_args Associative arguments (options) passed to the command.
64+ */
65+ public function migrate ( $ args , $ assoc_args ) {
66+ global $ wpdb ;
67+
68+ $ orders_batch = isset ( $ assoc_args ['batch ' ] ) ? absint ( $ assoc_args ['batch ' ] ) : 1000 ;
69+ $ orders_page = isset ( $ assoc_args ['page ' ] ) ? absint ( $ assoc_args ['page ' ] ) : 1 ;
70+ $ order_table = wc_custom_order_table ()->get_table_name ();
71+ $ order_count = $ this ->count ();
72+ $ total_pages = ceil ( $ order_count / $ orders_batch );
73+ $ progress = \WP_CLI \Utils \make_progress_bar ( 'Order Data Migration ' , $ order_count );
74+ $ batches_processed = 0 ;
75+ $ orders_sql = $ wpdb ->prepare (
76+ "SELECT ID FROM {$ wpdb ->posts }
77+ WHERE post_type IN ('%s')
78+ ORDER BY post_date DESC " ,
79+ implode ( ', ' , wc_get_order_types ( 'reports ' ) )
80+ );
81+
82+ // Work through each batch to migrate their orders.
83+ for ( $ page = $ orders_page ; $ page <= $ total_pages ; $ page ++ ) {
84+ $ offset = ( $ page * $ orders_batch ) - $ orders_batch ;
85+ $ orders = $ wpdb ->get_col ( $ wpdb ->prepare (
86+ $ orders_sql . ' LIMIT %d OFFSET %d ' ,
87+ $ orders_batch ,
88+ max ( $ offset , 0 )
89+ ) );
90+
91+ foreach ( $ orders as $ order ) {
92+ // Accessing the order via wc_get_order will automatically migrate the order to the custom table.
93+ wc_get_order ( $ order );
94+
95+ $ progress ->tick ();
96+ }
97+
98+ $ batches_processed ++;
99+ }
100+
101+ $ progress ->finish ();
102+
103+ WP_CLI ::log ( sprintf (
104+ /* Translators: %1$d is the number of total orders, %2$d is the number of batches. */
105+ __ ( '%1$d orders processed in %2$d batches. ' , 'wc-custom-order-table ' ),
106+ $ order_count ,
107+ $ batches_processed
108+ ) );
109+ }
110+
111+ /**
112+ * Backfill order meta data into postmeta.
113+ *
114+ * ## OPTIONS
115+ *
116+ * [--batch=<batch>]
117+ * : The number of orders to process.
118+ * ---
119+ * default: 1000
120+ * ---
121+ *
122+ * [--page=<page>]
123+ * : The page to start from.
124+ * ---
125+ * default: 1
126+ * ---
127+ *
128+ * ## EXAMPLES
129+ *
130+ * wp wc-order-table backfill --batch=100 --page=1
131+ *
132+ * @global $wpdb
133+ *
134+ * @param array $args Positional arguments passed to the command.
135+ * @param array $assoc_args Associative arguments (options) passed to the command.
136+ */
137+ public function backfill ( $ args , $ assoc_args ) {
138+ global $ wpdb ;
139+
140+ $ orders_batch = isset ( $ assoc_args ['batch ' ] ) ? absint ( $ assoc_args ['batch ' ] ) : 1000 ;
141+ $ orders_page = isset ( $ assoc_args ['page ' ] ) ? absint ( $ assoc_args ['page ' ] ) : 1 ;
142+ $ order_table = wc_custom_order_table ()->get_table_name ();
143+ $ order_count = $ wpdb ->get_var ( "SELECT COUNT(1) FROM {$ order_table } o " );
144+ $ total_pages = ceil ( $ order_count / $ orders_batch );
145+ $ progress = \WP_CLI \Utils \make_progress_bar ( 'Order Data Migration ' , $ order_count );
146+ $ batches_processed = 0 ;
147+
148+ WP_CLI ::log ( sprintf ( __ ( '%d orders to be backfilled. ' , 'wc-custom-order-table ' ), $ order_count ) );
149+
150+ for ( $ page = $ orders_page ; $ page <= $ total_pages ; $ page ++ ) {
151+ $ offset = ( $ page * $ orders_batch ) - $ orders_batch ;
152+ $ orders = $ wpdb ->get_col ( $ wpdb ->prepare (
153+ "SELECT order_id FROM {$ order_table } o LIMIT %d OFFSET %d " ,
154+ $ orders_batch ,
155+ max ( $ offset , 0 )
156+ ) );
157+
158+ foreach ( $ orders as $ order ) {
159+ // Accessing the order via wc_get_order will automatically migrate the order to the custom table.
160+ $ order = wc_get_order ( $ order );
161+ $ order ->get_data_store ()->backfill_postmeta ( $ order );
162+
163+ $ progress ->tick ();
164+ }
165+
166+ $ batches_processed ++;
167+ }
168+
169+ $ progress ->finish ();
170+
171+ WP_CLI ::log ( sprintf (
172+ /* Translators: %1$d is the number of total orders, %2$d is the number of batches. */
173+ __ ( '%1$d orders processed in %2$d batches. ' , 'wc-custom-order-table ' ),
174+ $ order_count ,
175+ $ batches_processed
176+ ) );
177+ }
178+ }
0 commit comments