0

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?

0

You are using a wrong Parameter '$customer_id' that is not defined in your code. Check below is updated code. It should be work. replace below code with your function.

function change_role_on_purchase( $order_id, $order ) {

    $user_id   = $order->get_customer_id();
    $user      = new WP_User( $user_id );
    $purchases = get_user_total_purchased_items( $user_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' );
    }

}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.