How7o
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Reading: How to Get the Customer ID from an Order ID in WooCommerce
Share
How7oHow7o
Font ResizerAa
  • OS
Search
  • Home
  • Tools
  • Prank Screens
  • Learn
  • Blog
  • Contact
Follow US
© 2024–2026 How7o. All rights reserved.
How7o > Free Laravel, PHP, WordPress & Server Tutorials > Web Development > How to Get the Customer ID from an Order ID in WooCommerce
Web Development

How to Get the Customer ID from an Order ID in WooCommerce

how7o
By how7o
Last updated: May 10, 2026
7 Min Read
WooCommerce get customer ID from order — WC_Order::get_user_id
SHARE

For woocommerce get customer id from order, call $order->get_user_id() (or its alias get_customer_id()) on any WC_Order object. Returns the customer’s WP_User ID, or 0 for guest checkouts. On older installs without HPOS, the legacy meta read (_customer_user) still works but has been superseded — this guide shows the modern method and why the older pattern breaks on current WooCommerce.

Contents
  • TL;DR
  • In a $order-aware hook
  • Why not get_post_meta($order_id, '_customer_user')?
  • Loading an order by ID
  • Order snapshot vs live user data
  • Frequently asked questions
  • Related guides
  • References

Last verified: 2026-04-23 on WooCommerce 9.x (HPOS enabled) with WordPress 6.5. Originally published 2022-11-08, rewritten and updated 2026-04-23.

TL;DR

// From a $order object already in scope
$user_id = $order->get_user_id();   // int, 0 for guests

// If you only have the order ID
$order   = wc_get_order( $order_id );
$user_id = $order ? $order->get_user_id() : 0;

In a $order-aware hook

add_action( 'woocommerce_admin_order_data_after_billing_address', 'how7o_customer_details', 10, 1 );

function how7o_customer_details( $order ) {
    $user_id = $order->get_user_id();

    if ( ! $user_id ) {
        echo '<p><em>Guest checkout</em></p>';
        return;
    }

    $custom_details = get_user_meta( $user_id, '_custom_details', true );
    if ( $custom_details ) {
        printf(
            '<p><strong>Custom Details:</strong> %s</p>',
            esc_html( $custom_details )
        );
    }
}

The woocommerce_admin_order_data_after_billing_address hook passes the $order object so you can grab get_user_id() directly. Guard against 0 (guest checkout) before handing the value to get_user_meta — meta lookups against user ID 0 would return a WordPress-wide record, which is not what you want.

woocommerce get customer id from order — WC_Order method on HPOS vs legacy meta read

Why not get_post_meta($order_id, '_customer_user')?

// Legacy — only works on pre-HPOS installs
$user_id = get_post_meta( $order_id, '_customer_user', true );

This pattern reads the _customer_user post meta that WooCommerce used to write to wp_postmeta when orders were stored as custom posts. WooCommerce 8.2 made HPOS (High-Performance Order Storage) the default: orders move to a dedicated wc_orders table and wp_postmeta no longer holds the link. get_post_meta returns an empty string on HPOS installs — silently, no error.

Always use the WC_Order method (get_user_id() / get_customer_id()). It abstracts the storage mode — works the same whether the order lives in posts (legacy) or the custom table (HPOS).

Loading an order by ID

$order = wc_get_order( $order_id );

if ( ! $order ) {
    // Order doesn't exist or the ID is bogus
    return;
}

$user_id       = $order->get_user_id();
$billing_email = $order->get_billing_email();
$status        = $order->get_status();

wc_get_order() is the factory that handles every order type (standard, subscription, refund). It accepts an ID, a post object, or an existing order object, and returns false when the input doesn’t resolve. The return-value check catches deleted orders and typos without fatal errors.

Order snapshot vs live user data

// From the order — the values as they were when the order was placed
$order->get_billing_first_name();
$order->get_billing_email();
$order->get_shipping_city();

// From the current user record — may have been updated since
$user         = $order->get_user();     // WP_User or false
$current_name = $user ? $user->first_name : '';

WooCommerce snapshots the billing and shipping addresses on the order at checkout time. The $order->get_billing_*() methods give you that frozen copy. $order->get_user() gives you the live WP_User, which may have been updated since the order.

For analytics and invoicing, use the order snapshot (the customer’s name at the time of purchase). For sending a post-purchase email or running a loyalty check, use the live user data.

Frequently asked questions

What’s the shortest woocommerce get customer id from order?

$order->get_user_id() (or the alias get_customer_id()) — both return the WP_User ID of the customer who placed the order, or 0 if it was a guest checkout. Works anywhere you have a WC_Order object.

What if I only have an order ID, not the object?

Load it: $order = wc_get_order($order_id);. wc_get_order is the proper factory — works with HPOS (High-Performance Order Storage, now the default on new WooCommerce installs), handles subscription order types, and returns false if the ID doesn’t exist. Don’t reach for new WC_Order($id) directly; the factory is what handles storage differences.

Is get_post_meta($order_id, '_customer_user', true) still valid?

Only on legacy installs that haven’t migrated to HPOS. With HPOS enabled (default since WooCommerce 8.2), orders live in a custom table wc_orders instead of wp_posts, and get_post_meta returns empty. Always prefer the $order->get_user_id() method — it works in both storage modes.

How do I handle guest orders?

get_user_id() returns 0 for guest checkouts. Use $order->get_billing_email() to identify the guest, or match against user_email in wp_users to see if they later created an account with the same address. Don’t assume every order has a logged-in customer — guest checkout is common.

Can I look up the customer’s meta from the order?

$user_id = $order->get_user_id(); $meta = get_user_meta($user_id, 'some_key', true); — two calls. Or the shortcut: WooCommerce stores the snapshot of customer info on the order itself (billing address, shipping address), accessible via $order->get_billing_first_name(), $order->get_shipping_city(), etc. Reach for the user_meta lookup when you need data that isn’t in the order snapshot; reach for the order getters when you want the address as it was when the order was placed.

Related guides

  • How to Display Orders Instead of Dashboard on the WooCommerce My Account Page — another order-surface customization.
  • How to Search Users by Multiple Fields in WordPress — finding customers when you don’t have an order ID.
  • How to Login a User Programmatically in WordPress — post-purchase account linking.
  • How to Add a Custom Fee in WooCommerce — order-context hook patterns.

References

WooCommerce WC_Order reference: woocommerce.github.io/code-reference/classes/WC-Order.

TAGGED:phpWooCommercewordpress

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
[mc4wp_form]
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Share This Article
Facebook Copy Link Print
Previous Article WooCommerce dynamic currency switcher — cookie-stored currency applied via woocommerce_currency filter How to Dynamically Change Currency in WooCommerce
Next Article WooCommerce SKU search — posts_search filter injecting SKU-matched product IDs How to Include SKU in WooCommerce Search
Leave a Comment

Leave a Reply Cancel reply

You must be logged in to post a comment.

FacebookLike
XFollow
PinterestPin
InstagramFollow
Most Popular
Laravel Eloquent ORM — a model class mapping to a database table with query methods
Laravel Eloquent ORM: The Complete Guide to Querying Your Database
June 16, 2026
Set vi as the default editor in Ubuntu — a terminal opening the vim editor
How to Set vi (Vim) as the Default Editor in Ubuntu
June 8, 2026
rsync says ALL DONE but files are missing — a terminal showing ALL DONE next to an empty folder
rsync Says “ALL DONE” but Files Are Missing: How to Verify
June 8, 2026
Migrate a website to a new server with rsync — files copying from an old server to a new one over SSH
How to Migrate a Website to a New Server With rsync
June 8, 2026
Bun runtime — faster JS toolkit replacing npm in Laravel projects
How to Install Bun Runtime on Ubuntu (And Use It in a Laravel Project)
May 24, 2026

You Might Also Like

Laravel Eloquent count rows — Post::count query snippet with aggregate bar chart icon
Web Development

How to Count Rows in Laravel Eloquent Efficiently

6 Min Read
Remove unwanted characters from a PHP string with regex
Web Development

How to Remove Unwanted Characters from a String in PHP

5 Min Read
Laravel Blade Time Format (HH:MM)
Web Development

How to Show Only Hours and Minutes in Laravel Blade (HH:MM)

3 Min Read
Keep only the digits in a JavaScript string with regex
Web Development

How to Keep Only Numbers in a String with JavaScript

4 Min Read
How7o

We provide tips, tricks, and advice for improving websites and doing better search.

Tools

  • Age Calculator
  • Word Counter
  • Image Upscaler
  • Password Generator
  • QR Code Generator
  • See all tools→

Pranks

  • Fake Blue Screen Prank
  • Hacker Typer
  • Fake iMessage Generator
  • Windows XP Crash Prank
  • Windows 11 Update Prank
  • See all prank screens →

Company

  • About Us
  • Blog
  • Contact
  • Privacy Policy
  • Terms of Service
  • Sitemap
© 2024–2026 How7o. All rights reserved.
Welcome Back!

Sign in to your account

Username or Email Address
Password

Lost your password?