0

So I am attempting to create a series of functions that calculate a price based on a client level I have set for each customer. Some customers may have a special deal.

To do this, I created a custom meta field in the user meta that allows me to assign either High, medium, or low to that client.

I then created a function meant to determine what the client level is and use that for the price calculations.

If a client is not logged in, or a field is empty, the default is set to High

I tried two versions of this function and put it in a plugin to test with a simple echo statement, but neither seem to be changing the result to anything besides high, despite the meta field being set to low.

if you can help me figure out what I am missing, that would be great. First Attempt:

function get_client_lvl () {
    $user_id = get_current_user_id();
    If ($user_id == 0) {
        return 'high';
    } Else {
        $client_lvl= get_user_meta($user_id, wpcf-client-lvl, true); 
        if (empty($client_lvl)) {
            return 'high';
        } Else {
            return $client_lvl;
        }
    }
}

$client_lvl=get_client_lvl();

echo $client_lvl;

Second Attempt

function get_client_lvl () {
    $user_id = get_current_user_id();
    $client_lvl= get_user_meta($user_id, wpcf-client-lvl, true);

    If ($user_id == 0 || empty($client_lvl)) {
        return 'high';
    } Else {
        return $client_lvl;
    }
}

$client_lvl=get_client_lvl();

echo $client_lvl;
1

The problem is that when you pass the $key argument on get_user_meta() function, you don't pass a valid argument. The $key must be a string according to the documentation page. It represents the value of meta_key in the wp_usermeta table.

If with wpcf-client-lvl is actually a variable that contains the name of the field ( lets say $wpcf-client-lvl = 'client_level' ) replace 'wpcf-client-lvl' with $wpcf-client-lvl. My guess was that this was the actual meta_key name.

I also change some of the syntax code if you don't mind :)

So I believe you copy / paste the following code it will work :

function get_client_lvl(){

    $global $current_user;
    get_currentuserinfo();

    $user_id = $current_user->ID;

    if ($user_id == 0)
        return 'high';

    else {

        $client_lvl= get_user_meta($user_id, 'wpcf-client-lvl', true); 

        if (empty($client_lvl))
            return 'high';

        else
            return $client_lvl;

    }

}

$client_lvl = get_client_lvl();

echo $client_lvl;

UPDATE

In order for the get_current_user_id() to return the ID you must either include pluggable.php ( which I don't think is a good approach ) or use global variables to get id. I changed the code using the global $current_user.

  • Yes wpcf-client-lvl is supposed to be the field name created by the wp-types plugin. I tried adding your code with no success. – Jon Cody Aug 31 '14 at 0:11
  • Ok, I added an echo code in the function to determine what user_id it is generating, and for some reason it is generating an ID of 0 even though I am listed in the database as user ID 1 as I should be. Any ideas? – Jon Cody Aug 31 '14 at 5:51
  • took some work but I figured it out. it needed a require pluggable.php for it to know what the function was. – Jon Cody Aug 31 '14 at 6:31
  • I updated the answer using a different approach which I find to be more efficient. I hope you find it useful. – negletios Aug 31 '14 at 7:27
  • Ok, well I tried your method, but if current user is not defined or pluggable is not initiated, then it breaks the site. The only other think I can think of to get rid of the pluggable requirement is to tell the function to load after all other files are initiated in wordpress. But I'm not sure how to do that. Although, since the actual call of the function will not be in the function itself, maybe it will work just fine. The actual use of this function is called within another function that is called on page templates. What do you think? – Jon Cody Aug 31 '14 at 22:00
0

You can read this: get_userdata and section about capatibilities.

You need to use the user global. Your function don't know the user id. You can declare argument ($user_id) or use global $wp_user.

Hope this helps you.

  • Well the very first line of the function determines userid if I'm not mistaken. – Jon Cody Aug 31 '14 at 0:07
  • Yes it determine user id in the loop, but your function need global to really be a part of your loop. See arguments of a function. You can change the call and the function like this : get_clt_lvl($user_id)... – Benoti Aug 31 '14 at 8:00

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.