Backfill guests into customer report lookup table using order data and billing email.

This commit is contained in:
Jeff Stieler 2018-12-21 13:12:58 -07:00
parent d000d3e42a
commit c337944cf6
2 changed files with 163 additions and 1 deletions

View File

@ -386,7 +386,48 @@ class WC_Admin_Api_Init {
}
}
// TODO: Backfill customer lookup table with guests.
// Backfill customer lookup table with guests.
$guest_order_ids = get_transient( 'wc_update_350_all_guest_orders' );
if ( false === $guest_order_ids ) {
$guest_order_ids = wc_get_orders(
// TODO: restrict to certain order status?
array(
'fields' => 'ids',
'customer_id' => 0,
'order' => 'asc',
'orderby' => 'date',
'posts_per_page' => -1,
)
);
set_transient( 'wc_update_350_all_guest_orders', $guest_order_ids, DAY_IN_SECONDS );
}
$customers_report_data_store = new WC_Admin_Reports_Customers_Data_Store();
foreach ( $guest_order_ids as $idx => $order_id ) {
$order = wc_get_order( $order_id );
if ( ! $order ) {
unset( $guest_order_ids[ $idx ] );
} elseif ( ! $order->get_billing_email( 'edit' ) ) {
unset( $guest_order_ids[ $idx ] );
} else {
$result = $customers_report_data_store->update_guest_customer_by_order( $order );
if ( $result ) {
unset( $guest_order_ids[ $idx ] );
}
}
if ( $updater instanceof WC_Background_Updater && $updater->is_memory_exceeded() ) {
// Update the transient for the next run to avoid processing the same orders again.
set_transient( 'wc_update_350_all_guest_orders', $guest_order_ids, DAY_IN_SECONDS );
return true;
}
}
return true;
}

View File

@ -336,6 +336,127 @@ class WC_Admin_Reports_Customers_Data_Store extends WC_Admin_Reports_Data_Store
self::update_registered_customer( $customer_id );
}
/**
* Updates a guest (no user_id) customer data with new order data.
*
* @param WC_Order $order Order to update guest customer data with.
* @return int|false The number of rows affected, or false on error.
*/
public function update_guest_customer_by_order( $order ) {
global $wpdb;
$email = $order->get_billing_email( 'edit' );
if ( empty( $email ) ) {
return true;
}
$existing_guest = $this->get_guest_by_email( $email );
if ( $existing_guest ) {
$order_timestamp = date( 'Y-m-d H:i:s', $order->get_date_created( 'edit' )->getTimestamp() );
$new_orders_count = $existing_guest['orders_count'] + 1;
$new_total_spend = $existing_guest['total_spend'] + (float) $order->get_total( 'edit' );
return $wpdb->update(
$wpdb->prefix . self::TABLE_NAME,
array(
'orders_count' => $new_orders_count,
'total_spend' => $new_total_spend,
'avg_order_value' => $new_total_spend / $new_orders_count,
'date_last_order' => $order_timestamp,
'date_last_active' => $order_timestamp,
),
array(
'customer_id' => $existing_guest['customer_id'],
),
array( '%d', '%f', '%f', '%s', '%s' ),
array( '%d' )
);
}
return $this->insert_guest_customer(
array(
'first_name' => $order->get_billing_first_name( 'edit' ),
'last_name' => $order->get_billing_last_name( 'edit' ),
'email' => $email,
'city' => $order->get_billing_city( 'edit' ),
'postcode' => $order->get_billing_postcode( 'edit' ),
'country' => $order->get_billing_country( 'edit' ),
'orders_count' => 1,
'total_spend' => (float) $order->get_total( 'edit' ),
'avg_order_value' => (float) $order->get_total( 'edit' ),
'date_last_order' => date( 'Y-m-d H:i:s', $order->get_date_created( 'edit' )->getTimestamp() ),
'date_last_active' => date( 'Y-m-d H:i:s', $order->get_date_created( 'edit' )->getTimestamp() ),
)
);
}
/**
* Insert a guest (no user_id) customer into lookup table.
*
* @param array $customer_data Array of guest customer data to insert.
* @return int|false The number of rows affected, or false on error.
*/
public static function insert_guest_customer( $customer_data ) {
global $wpdb;
return $wpdb->insert(
$wpdb->prefix . self::TABLE_NAME,
array(
'first_name' => $customer_data['first_name'],
'last_name' => $customer_data['last_name'],
'email' => $customer_data['email'],
'city' => $customer_data['city'],
'postcode' => $customer_data['postcode'],
'country' => $customer_data['country'],
'orders_count' => $customer_data['orders_count'],
'total_spend' => $customer_data['total_spend'],
'avg_order_value' => $customer_data['avg_order_value'],
'date_last_order' => $customer_data['date_last_order'],
'date_last_active' => $customer_data['date_last_active'],
),
array(
'%s',
'%s',
'%s',
'%s',
'%s',
'%s',
'%d',
'%f',
'%f',
'%s',
'%s',
)
);
}
/**
* Retrieve a guest (no user_id) customer row by email.
*
* @param string $email Email address.
* @returns false|array Customer array if found, boolean false if not.
*/
public function get_guest_by_email( $email ) {
global $wpdb;
$table_name = $wpdb->prefix . self::TABLE_NAME;
$guest_row = $wpdb->get_row(
$wpdb->prepare(
"SELECT * FROM {$table_name} WHERE email = %s AND user_id IS NULL LIMIT 1",
$email
),
ARRAY_A
); // WPCS: unprepared SQL ok.
if ( $guest_row ) {
return $this->cast_numbers( $guest_row );
}
return false;
}
/**
* Update the database with customer data.
*