• Resolved Ash

    (@ash222)


    Hello,
    I have a code in which particular user’s total balance is got.I am repeating this code several times. So I added the same code inside a function so that i can call that function when needed. The function takes user id as parameter. Can u please guide me on how to call the function by passing parameter and return the result. If it is possible by using add_filter then how to do that?

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

    (@sterndata)

    Volunteer Forum Moderator

    Moderator bcworkz

    (@bcworkz)

    There’s no reason to use add_filter() unless you are modifying someone else’s code or you want someone else to modify your code. If your function needs the user ID as a parameter, you can call your function (called get_total_balance() here) like so:

    $id = get_current_user_id();
    $balance = get_total_balance( $id );
    echo 'Amount owed: ' . money_format('%i', $balance ) . "\n";

    Your function declaration, minus the functional code within, would look something like this:

    function get_total_balance( $user_id ) {
      // take user ID and get their total balance
      // assign the balance to variable $bal
      return $bal;
    }
    Thread Starter Ash

    (@ash222)

    Hello,
    Thanks for the reply. I followed the same structure. I called the function
    $balance= wallet_balance($user_id); //here user_id is not of current logged in user. This function is used within the hook woocommerce_order_refunded.

    function wallet_balance($user_id){
    //my code goes here
    }

    But in this case i get error. user_id got is empty

    Moderator Steven Stern (sterndata)

    (@sterndata)

    Volunteer Forum Moderator

    Show the code where you get the value you’re passing to the function.

    Thread Starter Ash

    (@ash222)

    hello,
    Sorry for disturbing. Found the answer. You are correct. Mistake I made was didn’t declare wpdb as global variable.
    global $wpdb;

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘How to do function call in wordpress’ is closed to new replies.