0

I need to add a new role to my recently registered user (upon buying any of my four specific subscriptions products). Until now, every recently registered user (those who buy a subscription product) get a Subscriber role. While I want them to be Subscriber + Advertiser if they buy any of my 4 target subscription products.

I have tried woocommerce_order_status_completed, woocommerce_order_status_processing and woocommerce_order_status_changed hooks but none of them are calling. I have modified the function and code inside these hooks several times but I got nothing special.

If the same hook or anything else can solve my problem, I will appreciate your help.

Until now, I have used this code.

add_action( 'woocommerce_order_status_completed', 'so_29647785_convert_customer_role' );

function so_29647785_convert_customer_role( $order_id ) {

$order = new WC_Order( $order_id );
if ( $order->user_id > 0 ) {
    foreach ( $order->get_items() as $order_item ) {
        if( 4008 == $order_item[ 'product_id' ] ) {
            $user = new WP_User( $order->user_id );

            // Add new role
            $user->add_role( 'advertiser' );
        }
    }
}
}

I have also tried this code and it is helpful in creating a user with both Subscriber + Advertiser roles but I can't do so in my case. Because I need users to be registered with both Subscriber + Advertiser roles only if they will buy four of my target subscription products. While this code is adding both Subscriber + Advertiser to every new user regardless of the product they choose.

add_filter('woocommerce_new_customer_data', 'bbloomer_assign_custom_role', 10, 1);

function bbloomer_assign_custom_role($args) {
  $args['role'] = 'advertiser';
  return $args;
}

Any help will highly be appreciated!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.