• Viktor123b

    (@viktorbengtsson8)


    Trying to get this function to work. The first one works fine, it gets the total number of items ordered by a user (all time) and the second one is supposed to change user role based on the total items.

    function get_user_total_purchased_items( $user_id = 0 ){
        global $wpdb;
    
        $customer_id = $user_id === 0 ? get_current_user_id() : (int) $user_id;
    
        return (int) $wpdb->get_var( "
            SELECT SUM(woim.meta_value)
            FROM {$wpdb->prefix}woocommerce_order_items AS woi
            INNER JOIN {$wpdb->prefix}woocommerce_order_itemmeta AS woim ON woi.order_item_id = woim.order_item_id
            INNER JOIN {$wpdb->prefix}posts as p ON woi.order_id = p.ID
            INNER JOIN {$wpdb->prefix}postmeta as pm ON woi.order_id = pm.post_id
            WHERE woi.order_item_type = 'line_item'
            AND p.post_type LIKE 'shop_order'
            AND p.post_status IN ('wc-completed')
            AND pm.meta_key LIKE '_customer_user'
            AND pm.meta_value LIKE '$customer_id'
            AND woim.meta_key LIKE '_qty'
        " );
    }
    
    add_action( 'woocommerce_order_status_completed', 'change_role_on_purchase', 20, 2 );
    function change_role_on_purchase( $order_id, $order ) {
    
        $user_id   = $order->get_customer_id();
        $user      = new WP_User( $customer_id );
        $purchases = get_user_total_purchased_items( $customer_id );
    
        if( $purchases >= 50 &&  $purchases < 100 && ! in_array( 'bronze_user', $user->roles ) ) {
            // Remove role
            $user->remove_role( 'subscriber' );
    
            // Add role
            $user->add_role( 'bronze_user' );
        }
        elseif( $purchases >= 100 &&  $purchases < 200 && ! in_array( 'silver_user', $user->roles ) ) {
            // Remove role
            $user->remove_role( 'bronze_user' );
    
            // Add role
            $user->add_role( 'silver_user' );
        }
        elseif( $purchases >= 200 && ! in_array( 'gold_user', $user->roles ) ) {
            // Remove role
            $user->remove_role( 'silver_user' );
    
            // Add role
            $user->add_role( 'gold_user' );
        }
    
    }

    Anyone have any ideas why its not working?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    If you add some debugging statements, what’s in $purchases in the 2nd function?

    If you add some debbuging statements, do things proceed through your ifs properly?

    Hey there,
    I think you should replace this part of code:

    if( $purchases >= 50 && ! in_array( 'bronze_user', $user->roles ) ) {
            // Remove role
            $user->remove_role( 'subscriber' );
    
            // Add role
            $user->add_role( 'bronze_user' );
        }
     if( $purchases >= 100 && ! in_array( 'silver_user', $user->roles ) ) {
            // Remove role
            $user->remove_role( 'bronze_user' );
    
            // Add role
            $user->add_role( 'silver_user' );
        }
     if( $purchases >= 200 && ! in_array( 'gold_user', $user->roles ) ) {
            // Remove role
            $user->remove_role( 'silver_user' );
    
            // Add role
            $user->add_role( 'gold_user' );
        }

    Hope it does helps.

    • This reply was modified 6 years, 3 months ago by Sunny.
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Change user role based on total number of items ordered.’ is closed to new replies.