@@ -47,22 +47,29 @@ public function delete( &$order, $args = array() ) {
4747 }
4848
4949 /**
50- * Read order data.
50+ * Read order data from the custom orders table .
5151 *
52- * @param WC_Order $order The order object, passed by reference.
52+ * If the order does not yet exist, the plugin will attempt to migrate it automatically. This
53+ * behavior can be modified via the "wc_custom_order_table_automatic_migration" filter.
54+ *
55+ * @param WC_Order $order The order object, passed by reference.
5356 * @param object $post_object The post object.
5457 */
5558 protected function read_order_data ( &$ order , $ post_object ) {
56- global $ wpdb ;
57-
5859 $ data = $ this ->get_order_data_from_table ( $ order );
5960
6061 if ( ! empty ( $ data ) ) {
6162 $ order ->set_props ( $ data );
62-
6363 } else {
64- // Automatically backfill order data from meta, but allow for disabling.
65- if ( apply_filters ( 'wc_custom_order_table_automatic_migration ' , true ) ) {
64+ /**
65+ * Toggle the ability for WooCommerce Custom Orders Table to automatically migrate orders.
66+ *
67+ * @param bool $migrate Whether or not orders should automatically be migrated once they
68+ * have been loaded.
69+ */
70+ $ migrate = apply_filters ( 'wc_custom_order_table_automatic_migration ' , true );
71+
72+ if ( $ migrate ) {
6673 $ this ->populate_from_meta ( $ order );
6774 }
6875 }
@@ -75,7 +82,7 @@ protected function read_order_data( &$order, $post_object ) {
7582 *
7683 * @param WC_Order $order The order object.
7784 *
78- * @return object The order row, as an associative array.
85+ * @return array The order row, as an associative array.
7986 */
8087 public function get_order_data_from_table ( $ order ) {
8188 global $ wpdb ;
@@ -86,11 +93,21 @@ public function get_order_data_from_table( $order ) {
8693 $ order ->get_id ()
8794 ), ARRAY_A ); // WPCS: DB call OK.
8895
96+ // Return early if there's no matching row in the orders table.
97+ if ( empty ( $ data ) ) {
98+ return array ();
99+ }
100+
101+ $ post = get_post ( $ order ->get_id () );
102+
89103 // Expand anything that might need assistance.
90104 if ( isset ( $ data ['prices_include_tax ' ] ) ) {
91105 $ data ['prices_include_tax ' ] = wc_string_to_bool ( $ data ['prices_include_tax ' ] );
92106 }
93107
108+ // Append additional data.
109+ $ data ['customer_note ' ] = $ post ->post_excerpt ;
110+
94111 return $ data ;
95112 }
96113
@@ -354,10 +371,14 @@ public function search_orders( $term ) {
354371 public function populate_from_meta ( &$ order , $ delete = false ) {
355372 global $ wpdb ;
356373
357- $ table_data = $ this ->get_order_data_from_table ( $ order );
358- $ order = WooCommerce_Custom_Orders_Table::populate_order_from_post_meta ( $ order );
374+ try {
375+ $ table_data = $ this ->get_order_data_from_table ( $ order );
376+ $ order = WooCommerce_Custom_Orders_Table::populate_order_from_post_meta ( $ order );
359377
360- $ this ->update_post_meta ( $ order );
378+ $ this ->update_post_meta ( $ order );
379+ } catch ( WC_Data_Exception $ e ) {
380+ return new WP_Error ( 'woocommerce-custom-order-table-migration ' , $ e ->getMessage () );
381+ }
361382
362383 if ( $ wpdb ->last_error ) {
363384 return new WP_Error ( 'woocommerce-custom-order-table-migration ' , $ wpdb ->last_error );
0 commit comments