简体   繁体   中英

Change user role depending on purchased products in WooCommerce

In woocommerce I am trying to change the user role depending on customer purchased products. I saw the various code and tried them all without being able to make them work.

I have several products to check for different user roles change.

This is my code attempt:

add_action('woocommerce_order_status_completed', 'change_role_on_purchase' );

$products_to_check = array( '5345, 5344,5342' );

foreach ( $items as $item ) {
    if ( $order->user_id > 0 && in_array( $item['product_id'], $products_to_check ) ) {
        $user = new WP_User( $order->user_id );

        // Change role
        $user->remove_role( 'Subscriber' );
        $user->add_role( 'Student-Group' );

        // Exit the loop
        break;
    }
}

$products_to_check = array( '5353, 5352,5351,12119' );

foreach ( $items as $item ) {
    if ( $order->user_id > 0 && in_array( $item['product_id'], $products_to_check ) ) {
        $user = new WP_User( $order->user_id );

        // Change role
        $user->remove_role( 'Subscriber' );
        $user->add_role( 'Student-11free2' );

        // Exit the loop
        break;
    }
}

$products_to_check = array( '5360, 5359,5358' );

foreach ( $items as $item ) {
    if ( $order->user_id > 0 && in_array( $item['product_id'], $products_to_check ) ) {
        $user = new WP_User( $order->user_id );

        // Change role
        $user->remove_role( 'Subscriber' );
        $user->add_role( 'Student-11free3' );

        // Exit the loop
        break;
    }
}

$products_to_check = array( '5363, 5362,5361' );

foreach ( $items as $item ) {
    if ( $order->user_id > 0 && in_array( $item['product_id'], $products_to_check ) ) {
        $user = new WP_User( $order->user_id );

        // Change role
        $user->remove_role( 'Subscriber' );
        $user->add_role( 'Student-11regular2' );

        // Exit the loop
        break;
    }
}

But it doesn't work. Any help will be appreciated.

Since Woocommerce 3 your code is outdated and full of mistakes… Instead use the following:

add_action('woocommerce_order_status_completed', 'change_user_role_on_order_status_completed', 10, 2 );
function change_user_role_on_order_status_completed( $order_id, $order ){
    if ( $order->get_user_id() > 0 ) {
        // Here your settings in this multi dimensional array
        $user_roles_for_products = array(
            'Student-Group'      => array( 5345, 5344, 5342 ),
            'Student-11free3'    => array( 5353, 5352, 5351, 12119 ),
            'Student-11free3'    => array( 5360, 5359, 5358 ),
            'Student-11regular2' => array( 5363, 5362, 5361 ),
        );

        $user = $order->get_user();

        // Loop through order items
        foreach ( $order->get_items() as $item ) {
            $product_ids = array( $item->get_product_id(), $item->get_variation_id() );

            // Loop through all products to check
            foreach ( $user_roles_for_products as $role => $products_to_check ) {
                if ( array_intersect( $product_ids, $products_to_check ) && in_array( 'Subscriber', $user->roles ) ) {
                    $user->remove_role( 'Subscriber' );
                    $user->add_role( $role );
                    $break = true;
                    break; // Stop the loop
                }
            }
            if( isset($break) && $break )
                break; // Stop the loop
        }
    }
}

Code goes in function.php file of your active child theme (or active theme). It should better works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM