Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the customer ID from an order ID in WooCommerce

I want to get the "mycred" balance of a customer through the order while using WP ALL Export to export the customer balance based on orders to a spreadsheet. It's actually probably quite simple. I'm able to get the Order ID, but not the Customer ID

Here is what I'm doing to test if I can get the customer ID:

function get_customeruserid($value)
{
  global $woocommerce, $post;

  $order = new WC_Order($post->ID);
  $order_id = $order->get_order_number();

  $customer = new WC_Customer($post->ID);
  $user_id = $customer->get_ID();

  $value = $user_id;
  return $value;
}

This returns a 0.

However, I can get the order number easily enough by doing this:

function get_customerorderid($value)
{
  global $woocommerce, $post;

  $order = new WC_Order($post->ID);
  $order_id = $order->get_order_number();

  $value = $order_id;
  return $value;
}

This returns the customer's order number which is great, but only half the battle. I now want the Customer ID so I call call the mycred balance function to show their balance.

Any ideas? I'm a newbie at php and probably very bad.

like image 695
Fluke Fox Avatar asked May 05 '17 21:05

Fluke Fox


People also ask

How do I find my WordPress customer ID?

Find User ID in WordPress Dashboard First, you'll need to login to your WordPress admin dashboard. From here you can select the user you want the ID of by clicking edit. This will lead you to the user edit page. Here you'll easily be able to see the user's ID in the address bar of your browser.

How do I find the last order ID in WooCommerce?

php $latest_order_id = get_last_order_id(); // Last order ID $order = wc_get_order( $latest_order_id ); // Get an instance of the WC_Order object $order_details = $order->get_data(); // Get the order data in an array $order_status = esc_html( wc_get_order_status_name( $order->get_status() ) ); $order_items = $ ...


2 Answers

To get the User ID from the Order ID, you can use many ways, Here are 2 of them: In WooCommerce 3.0+ you can use WC_Order Class methods this way:

function get_customerorderid(){
    global $order, $post;

    if( ! is_a($order, 'WC_Order') ) {
        $order_id = $post->ID;

        // Get an instance of the WC_Order object
        $order = wc_get_order($order_id);
    } else {
        $order_id = $order->id;
    }

    // Get the user ID from WC_Order methods
    $user_id = $order->get_user_id(); // or $order->get_customer_id();

    return $user_id;
}

Before WooCommerce 3.0 version, you can use get_post_meta() function this way:

function get_customerorderid(){
    global $order, $post;

    if( ! is_a($order, 'WC_Order') ) {
        $order_id = $post->ID;
    } else {
        $order_id = $order->id;
    }

    // Get the user ID
    $user_id = get_post_meta($order_id, '_customer_user', true);

    return $user_id;
}
like image 181
LoicTheAztec Avatar answered Sep 22 '22 03:09

LoicTheAztec


For those who want to specifically add the customer mycred balance from an ORDER into the CSV sheet within WP All Export here is the bit of code I used. Thank you for your help getting it solved.

While editing an ORDER export in WP ALL EXPORT, add a new data object and click on it and "Export the value returned by a PHP function" then add the following function in the code editor:

function all_export_mycred($balance)
{
    global $woocommerce, $post;

    $order = new WC_Order($post->ID);
    $user_id = $order->get_user_id( );

    $balance = mycred_get_users_balance( $user_id );

            return $balance;

    }

Then make sure to add the "all_export_mycred" to the php return field.

like image 43
Fluke Fox Avatar answered Sep 24 '22 03:09

Fluke Fox