Skip to content
This repository was archived by the owner on Jun 15, 2022. It is now read-only.

Commit 09c6d00

Browse files
authored
Merge pull request #82 from liquidweb/fix/customer-notes
Ensure the customer_notes property is populated
2 parents a4034b0 + 74bb51e commit 09c6d00

File tree

3 files changed

+73
-23
lines changed

3 files changed

+73
-23
lines changed

includes/class-wc-order-data-store-custom-table.php

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -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 );

includes/class-wc-order-refund-data-store-custom-table.php

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,25 @@
1515
class WC_Order_Refund_Data_Store_Custom_Table extends WC_Order_Refund_Data_Store_CPT {
1616

1717
/**
18-
* Read refund data.
18+
* Read refund data from the custom orders table.
1919
*
20-
* @param WC_Order_Refund $refund The refund object, passed by reference.
21-
* @param object $post_object The post object.
20+
* If the refund does not yet exist, the plugin will attempt to migrate it automatically. This
21+
* behavior can be modified via the "wc_custom_order_table_automatic_migration" filter.
22+
*
23+
* @param WC_Refund $order The refund object, passed by reference.
24+
* @param object $post_object The post object.
2225
*/
23-
protected function read_order_data( &$refund, $post_object ) {
24-
global $wpdb;
25-
26-
$data = $this->get_order_data_from_table( $refund );
26+
protected function read_order_data( &$order, $post_object ) {
27+
$data = $this->get_order_data_from_table( $order );
2728

2829
if ( ! empty( $data ) ) {
29-
$refund->set_props( $data );
30-
30+
$order->set_props( $data );
3131
} else {
32-
// Automatically backfill refund data from meta, but allow for disabling.
33-
if ( apply_filters( 'wc_custom_order_table_automatic_migration', true ) ) {
34-
$this->populate_from_meta( $refund );
32+
/** This filter is defined in class-wc-order-data-store-custom-table.php. */
33+
$migrate = apply_filters( 'wc_custom_order_table_automatic_migration', true );
34+
35+
if ( $migrate ) {
36+
$this->populate_from_meta( $order );
3537
}
3638
}
3739
}

tests/test-order-data-store.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,19 @@ public function test_get_order_data_from_table_when_order_is_still_in_post_meta(
7676
$this->assertEmpty( $order->get_data_store()->get_order_data_from_table( $order ) );
7777
}
7878

79+
/**
80+
* @ticket https://github.com/liquidweb/woocommerce-custom-orders-table/issues/68
81+
*/
82+
public function test_get_order_data_from_table_populates_customer_notes() {
83+
$order = WC_Helper_Order::create_order();
84+
$order->set_customer_note( 'This is a new post excerpt.' );
85+
$order->save();
86+
87+
$order = wc_get_order( $order );
88+
89+
$this->assertEquals( 'This is a new post excerpt.', $order->get_customer_note() );
90+
}
91+
7992
public function test_update_post_meta_for_new_order() {
8093
$order = new WC_Order( wp_insert_post( array(
8194
'post_type' => 'product',
@@ -269,6 +282,20 @@ public function test_populate_from_meta_handles_errors() {
269282
$this->assertInstanceOf( 'WP_Error', $order1->get_data_store()->populate_from_meta( $order2 ) );
270283
}
271284

285+
public function test_populate_from_meta_handles_wc_data_exceptions() {
286+
$this->toggle_use_custom_table( false );
287+
$order = WC_Helper_Order::create_order();
288+
$this->toggle_use_custom_table( true );
289+
290+
// Give an invalid billing email.
291+
update_post_meta( $order->get_id(), '_billing_email', 'this is not an email address' );
292+
293+
// Refresh the instance.
294+
$order = wc_get_order( $order->get_id() );
295+
296+
$this->assertInstanceOf( 'WP_Error', $order->get_data_store()->populate_from_meta( $order ) );
297+
}
298+
272299
/**
273300
* Shortcut for setting up reflection methods + properties for update_post_meta().
274301
*

0 commit comments

Comments
 (0)