WooCommerce – When a new order is created, add the customer ID as post author

This simple snippet is useful when logged in users create orders (buy things) on your store and you want to associate the user as post_author for advanced purposes.

Why? you can have employees (users with shop_managers) make purchases in the frontend of the store on behalf of customers, and having the orders associated to their user as post_author will let you integrate easily with other frontend dashboard plugins.

For example. Our WP Frontend Admin plugin allows you to display “wp-admin” pages in the frontend. In this case, you can display the “wp-admin > orders” page to allow users/employees to manage orders on the frontend.

If you combine our WP Frontend Admin plugin with this snippet, you can add a restriction so the current user will see orders created by him only.

This can be helpful. Employees can buy products on behalf of customers using the frontend store and manage the orders created by them.

add_filter('woocommerce_new_order_data', function($post_data){
	if( is_user_logged_in() ){
		$post_data['post_author'] = get_current_user_id();
	}
	return $post_data;
});