Get the customer ID from an order ID in WooCommerce

旧时模样 提交于 2019-12-03 07:45:14

You could use the order number to get the corresponding customer id using a database query, google wordpress meta query

To get the User ID from the Order ID, you can use many ways, Here are 2 of them:

In WooCommerce 2.5 to 3.0+ you can use get_post_meta() function this way:

function get_customerorderid(){
    global $post;
    $order_id = $post->ID;

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

    return $user_id;
}

In WooCommerce 3.0+ you can use the class WC_Order methods this way:

function get_customerorderid(){
    global $post;
    $order_id = $post->ID;

    // Get an instance of the WC_Order object
    $order = wc_get_order($order_id);

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

    return $user_id;
}

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!