Customers: Update Autoship Customer in WP-Usermeta on Login

In this example, a developer adds a new meta_key (or updates an existing one) to the wp_usermeta table in their Wordpress database when a customer who has "Active" status Scheduled Orders logs in to their account.

Remember: Always test new code on a staging site before adding it to your live site.

Code Example:

/**
 * Checks if the current user logging in has an Active Scheduled Order
 * @see autoship_search_all_scheduled_orders()
 *
 * @param string $user_login The user's login
 * @param WP_User $user the user object
 */
function xx_check_is_active_autoship_customer_logging_in( $user_login, $user ){

	// Check if user has a scheduled order and update a metadata value to be used later
	$orders = autoship_search_all_scheduled_orders( $user->ID, 1, array( 'statusNames' => array('Active') ) );
  
	// Update the custom user meta
	update_user_meta( $user->ID, 'xx_has_active_autoship_order', !empty( $orders ) );
  
  }
  add_action('wp_login', 'xx_check_is_active_autoship_customer_logging_in', 10, 2 );

Result:

User Id's 82 & 83 have logged on and have active Scheduled Orders, so we see the new meta_key populate in wp_usermeta table