I am trying to create a new post inside my custom post type 'vipmembers' once the user buys a WooCommerce membership subscription (user role 'subscriber'). The code below only allows me to add a new post once the user is a customer(i.e when user register) but I want the post to only be added once the user buys a WooCommerce membership subscription product (i.e when a user role is Subscriber).
Any help would be appreciated.
add_action( 'user_register', 'membership_import', 10, 1 );
function membership_import( $user_id ) {
if($user_id){
$args = array(
'post_type' => 'vipmembers',
'meta_query' => array(
array(
'key' => 'user_id',
'value' => $user_id,
'compare' => '='
)
)
);
$users_exists = get_posts( $args );
$author_obj = get_user_by('id', $user_id);
$roles = $author_obj->roles;
$post = array(
'post_title' => wp_strip_all_tags($author_obj->user_login),
'post_status' => 'publish',
'post_type' => 'vipmembers'
);
if(empty($users_exists)){
$post_ID = wp_insert_post($post);
update_post_meta($post_ID, 'user_id',$user_id);
update_post_meta($post_ID, 'user_email',$author_obj->user_email);
}
}
}