0

So I am trying to update a user meta data array that I have previously stored with this function:

function oe_charge_customer( $customer ) { 

    // load the stripe libraries
    require_once(STRIPE_BASE_DIR . '/lib/Stripe.php');

    // check if in test mode or not
    check_stripe_api();

    // counter
    $i = $customer['i'];

    // get user meta, if any
    $payment_args = get_user_meta( $customer['user_id'], 'oe_customer_details' );

    // update paid value to true
    $payment_args[0]['payments'][$i]['paid'] = 1;

    // attempt to charge the customer's card
    try {

        $charge = Stripe_Charge::create(array(
                'amount'    => $customer['amount'] * 100,
                'currency'  => 'usd',
                'customer'  => $customer['cust_id']
            )
        );

        // update user profile to show paid
        update_user_meta( $customer['user_id'], 'oe_customer_details', $payment_args );

    } catch (Exception $e) {

        wp_die($e);

    }

}

I am getting somewhat expected results, except it keeps throwing it in a new array every time it gets updated. Any idea why this is happening? I can't seem to wrap my head around it . I get the following after it gets updated:

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [0] => Array
                        (
                            [0] => Array
                                (
                                    [0] => Array
                                        (
                                            [customer] => Array
                                                (
                                                    [name] => 
                                                    [id] => cus_2Oo1hyvYyZsaj0
                                                    [user_id] => 18
                                                )

                                            [payments] => Array
                                                (
                                                    [0] => Array
                                                        (
                                                            [amount] => 16
                                                            [date] => 1376710188
                                                            [paid] => 0
                                                        )

                                                    [1] => Array
                                                        (
                                                            [amount] => 4
                                                            [date] => 1376710238
                                                            [paid] => 0
                                                        )

                                                )

                                        )

                                    [payments] => Array
                                        (
                                            [0] => Array
                                                (
                                                    [paid] => 0
                                                )

                                        )

                                )

                        )

                    [payments] => Array
                        (
                            [1] => Array
                                (
                                    [paid] => 1
                                )

                        )

                )

        )

)
0

It looks like get_user_meta() returns an array like such.

array(2) {
   [0]=>
       array(1) {
           [0]=>
       }

   [1]=>
       array(1) {
           [0]=>
       }
}

Then when update_user_meta() stores the array it sticks it inside another array like this.

Edit: As it turns out get_user_meta() is encapsulating the result in an array when it grabs it.

array(1) {
    [0]=>
        array(2) {
           [0]=>
               array(1) {
                   [0]=>
               }
           [1]=>
               array(1) {
                   [0]=>
               }
        }
}

Then later it's retrieved and saved again adding a new level each time.

There's a simple, although counter-intuitive, solution to this problem. Set the last parameter of get_user_meta() to true.

get_user_meta($id,$key,true);`

If it is false it returns an array and if it is true it returns a single value which could be an array itself.

More info is available here. https://stackoverflow.com/questions/7746887/storing-php-arrays-in-wordpress-user-meta-database

  • Any idea how I can avoid this? – souporserious Aug 17 '13 at 5:28
  • @ftntravis I've updated my answer with the solution. Just set the last parameter of get_user_meta() to true. – epilektric Aug 17 '13 at 8:11
  • Ahhhh this was it! Thanks for that, I tried last night with no luck, but then remembered when I was calling get_user_meta() again, I needed that third parameter true as well. Appreciate your help. Just needed a fresh pair of eyes to look at it. – souporserious Aug 17 '13 at 16:05

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.