Make customer name extendable (https://github.com/woocommerce/woocommerce-admin/pull/1976)
* Make customer name extendable * Update get_customer_name to favor user profile information * Update customer name on profile update * Add tests around customer name getting and setting * Prefix customer name filter with woocommerce_reports_
This commit is contained in:
parent
f682fe76be
commit
e31be343e2
|
@ -461,16 +461,17 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
|
|||
return $returning_customer_id;
|
||||
}
|
||||
|
||||
$data = array(
|
||||
'first_name' => $order->get_billing_first_name( 'edit' ),
|
||||
'last_name' => $order->get_billing_last_name( 'edit' ),
|
||||
$customer_name = self::get_customer_name( $order->get_user_id(), $order );
|
||||
$data = array(
|
||||
'first_name' => $customer_name[0],
|
||||
'last_name' => $customer_name[1],
|
||||
'email' => $order->get_billing_email( 'edit' ),
|
||||
'city' => $order->get_billing_city( 'edit' ),
|
||||
'postcode' => $order->get_billing_postcode( 'edit' ),
|
||||
'country' => $order->get_billing_country( 'edit' ),
|
||||
'date_last_active' => date( 'Y-m-d H:i:s', $order->get_date_created( 'edit' )->getTimestamp() ),
|
||||
);
|
||||
$format = array(
|
||||
$format = array(
|
||||
'%s',
|
||||
'%s',
|
||||
'%s',
|
||||
|
@ -505,6 +506,43 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
|
|||
return $result ? $customer_id : false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Try to get a customer name from user profile or order information.
|
||||
*
|
||||
* @param int $user_id User ID.
|
||||
* @param WC_Order $order Order made by customer.
|
||||
* @return array
|
||||
*/
|
||||
public static function get_customer_name( $user_id = 0, $order = null ) {
|
||||
$first_name = null;
|
||||
$last_name = null;
|
||||
|
||||
if (
|
||||
$user_id &&
|
||||
get_user_meta( $user_id, 'first_name', true ) ||
|
||||
get_user_meta( $user_id, 'last_name', true )
|
||||
) {
|
||||
$first_name = get_user_meta( $user_id, 'first_name', true );
|
||||
$last_name = get_user_meta( $user_id, 'last_name', true );
|
||||
} elseif (
|
||||
$order &&
|
||||
$order->get_billing_first_name( 'edit' ) ||
|
||||
$order->get_billing_last_name( 'edit' )
|
||||
) {
|
||||
$first_name = $order->get_billing_first_name( 'edit' );
|
||||
$last_name = $order->get_billing_last_name( 'edit' );
|
||||
} elseif (
|
||||
$order &&
|
||||
$order->get_shipping_first_name( 'edit' ) ||
|
||||
$order->get_shipping_last_name( 'edit' )
|
||||
) {
|
||||
$first_name = $order->get_shipping_first_name( 'edit' );
|
||||
$last_name = $order->get_shipping_last_name( 'edit' );
|
||||
}
|
||||
|
||||
return apply_filters( 'woocommerce_reports_customer_name', array( $first_name, $last_name ), $order );
|
||||
}
|
||||
|
||||
/**
|
||||
* Retrieve a guest ID (when user_id is null) by email.
|
||||
*
|
||||
|
@ -584,12 +622,13 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
|
|||
return false;
|
||||
}
|
||||
|
||||
$last_active = $customer->get_meta( 'wc_last_active', true, 'edit' );
|
||||
$data = array(
|
||||
$customer_name = self::get_customer_name( $user_id, $customer->get_last_order() );
|
||||
$last_active = $customer->get_meta( 'wc_last_active', true, 'edit' );
|
||||
$data = array(
|
||||
'user_id' => $user_id,
|
||||
'username' => $customer->get_username( 'edit' ),
|
||||
'first_name' => $customer->get_first_name( 'edit' ),
|
||||
'last_name' => $customer->get_last_name( 'edit' ),
|
||||
'first_name' => $customer_name[0],
|
||||
'last_name' => $customer_name[1],
|
||||
'email' => $customer->get_email( 'edit' ),
|
||||
'city' => $customer->get_billing_city( 'edit' ),
|
||||
'postcode' => $customer->get_billing_postcode( 'edit' ),
|
||||
|
@ -597,7 +636,7 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
|
|||
'date_registered' => $customer->get_date_created( 'edit' )->date( WC_Admin_Reports_Interval::$sql_datetime_format ),
|
||||
'date_last_active' => $last_active ? date( 'Y-m-d H:i:s', $last_active ) : null,
|
||||
);
|
||||
$format = array(
|
||||
$format = array(
|
||||
'%d',
|
||||
'%s',
|
||||
'%s',
|
||||
|
|
|
@ -326,4 +326,80 @@ class WC_Tests_API_Reports_Customers extends WC_REST_Unit_Test_Case {
|
|||
$this->assertCount( 1, $reports );
|
||||
$this->assertEquals( $test_customers[0]->get_id(), $reports[0]['user_id'] );
|
||||
}
|
||||
|
||||
/**
|
||||
* Test customer first and last name.
|
||||
*/
|
||||
public function test_customer_name() {
|
||||
wp_set_current_user( $this->user );
|
||||
|
||||
$customer = wp_insert_user(
|
||||
array(
|
||||
'user_login' => 'daenerys',
|
||||
'user_pass' => null,
|
||||
'role' => 'customer',
|
||||
)
|
||||
);
|
||||
|
||||
// Test shipping name and empty billing name.
|
||||
$order = WC_Helper_Order::create_order( $customer );
|
||||
$order->set_billing_first_name( '' );
|
||||
$order->set_billing_last_name( '' );
|
||||
$order->set_shipping_first_name( 'Daenerys' );
|
||||
$order->set_shipping_last_name( 'Targaryen' );
|
||||
$order->set_status( 'completed' );
|
||||
$order->set_total( 100 );
|
||||
$order->save();
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
$headers = $response->get_headers();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 1, $reports );
|
||||
$this->assertEquals( 'Daenerys Targaryen', $reports[0]['name'] );
|
||||
|
||||
// Test billing name.
|
||||
$order->set_billing_first_name( 'Jon' );
|
||||
$order->set_billing_last_name( 'Snow' );
|
||||
$order->save();
|
||||
do_action( 'woocommerce_update_customer', $customer );
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params( array( 'orderby' => 'username' ) ); // Cache busting.
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
$headers = $response->get_headers();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 1, $reports );
|
||||
$this->assertEquals( 'Jon Snow', $reports[0]['name'] );
|
||||
|
||||
// Test user profile name.
|
||||
wp_update_user(
|
||||
array(
|
||||
'ID' => $customer,
|
||||
'first_name' => 'Tyrion',
|
||||
'last_name' => 'Lanister',
|
||||
)
|
||||
);
|
||||
do_action( 'woocommerce_update_customer', $customer );
|
||||
|
||||
WC_Helper_Queue::run_all_pending();
|
||||
|
||||
$request = new WP_REST_Request( 'GET', $this->endpoint );
|
||||
$request->set_query_params( array( 'orderby' => 'name' ) ); // Cache busting.
|
||||
$response = $this->server->dispatch( $request );
|
||||
$reports = $response->get_data();
|
||||
$headers = $response->get_headers();
|
||||
|
||||
$this->assertEquals( 200, $response->get_status() );
|
||||
$this->assertCount( 1, $reports );
|
||||
$this->assertEquals( 'Tyrion Lanister', $reports[0]['name'] );
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue