Automattic\WooCommerce\Admin\API\Reports\Customers

DataStore::sync_order_customer()public staticWC 1.0

Sync customers data after an order was updated.

Only updates customer if it is the customers last order.

Method of the class: DataStore{}

Return

true|-1.

Usage

$result = DataStore::sync_order_customer( $post_id );
$post_id(int) (required)
of order.

DataStore::sync_order_customer() code WC 8.7.0

public static function sync_order_customer( $post_id ) {
	global $wpdb;

	if ( ! OrderUtil::is_order( $post_id, array( 'shop_order', 'shop_order_refund' ) ) ) {
		return -1;
	}

	$order       = wc_get_order( $post_id );
	$customer_id = self::get_existing_customer_id_from_order( $order );
	if ( false === $customer_id ) {
		return -1;
	}
	$last_order = self::get_last_order( $customer_id );

	if ( ! $last_order || $order->get_id() !== $last_order->get_id() ) {
		return -1;
	}

	list($data, $format) = self::get_customer_order_data_and_format( $order );

	$result = $wpdb->update( self::get_db_table_name(), $data, array( 'customer_id' => $customer_id ), $format );

	/**
	 * Fires when a customer is updated.
	 *
	 * @param int $customer_id Customer ID.
	 * @since 4.0.0
	 */
	do_action( 'woocommerce_analytics_update_customer', $customer_id );

	return 1 === $result;
}