Home > Enterprise >  Add user role(s) based on product IDs and quantity when WooCommerce order status is completed
Add user role(s) based on product IDs and quantity when WooCommerce order status is completed

Time:03-12

I'm trying to assign user roles based on a few different criteria:

  • If a user purchases Item-A, user-role-a is assigned.
  • If a user purchases Item-B, user-role-b is assigned.
  • If a user purchases 2 or more of Item-B, user-role-c is also assigned.

So multiple user roles can be assigned. Based on Change User Role After Purchasing Product, this is my attempt:

add_action( 'woocommerce_order_status_completed', 'wpglorify_change_role_on_purchase' );

function wpglorify_change_role_on_purchase( $order_id ) {

    $order = new WC_Order( $order_id );
    $items = $order->get_items();

// CHILD PRODUCT MAKES PRIMARY ADULT ROLE
    
    $product_id = 50; // that's the child product ID

    foreach ( $items as $item ) {

        if( $product_id == $item['product_id'] && $order->user_id ) {
            $user = new WP_User( $order->user_id );

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

// ADULT PRODUCT MAKES ADULT ROLE
    
    $product_id = 43; // that's the adult product ID
    
    foreach ( $items as $item ) {

        if( $product_id == $item['product_id'] && $order->user_id ) {
            $user = new WP_User( $order->user_id );

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

With my code I was able to get a bit of a solution (assign roles based on purchased product IDs), but I still need to be able to assign a role based on the quantity of a purchased item. Any advice?

CodePudding user response:

In addition to answering your question, I also modified and optimized your code a bit:

  • It is not necessary to go through the order items again per condition, instead put the conditions in the loop
  • $item->get_quantity() can be used to know the quantity
  • Multiple roles can be assigned to the same user, based on the product IDs

So you get:

function action_woocommerce_order_status_completed( $order_id, $order ) {
    // Product IDs
    $item_a = 30;
    $item_b = 813;

    // Is a order
    if ( is_a( $order, 'WC_Order' ) ) {
        // Get user
        $user = $order->get_user();

        // User is NOT empty
        if ( ! empty ( $user ) ) {
            // Loop through order items
            foreach ( $order->get_items() as $key => $item ) {
                // Item A
                if ( $item->get_product_id() == $item_a ) {
                    // Add user role
                    $user->add_role( 'user-role-a' );
                // Item B
                } elseif ( $item->get_product_id() == $item_b ) {
                    // Add user role
                    $user->add_role( 'user-role-b' );

                    // Quantity equal to or greater than
                    if ( $item->get_quantity() >= 2 ) {
                        // Add user role
                        $user->add_role( 'user-role-c' );                       
                    }
                }
            }
        }
    }
}
add_action( 'woocommerce_order_status_completed', 'action_woocommerce_order_status_completed', 10, 2 );
  • Related