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