wc_get_order() Function: Load Orders in WooCommerce [Guide]

wc_get_order() function in WooCommerce: document card with a search lens

Almost every WooCommerce task that touches an order starts with the same question: which order is this? wc_get_order() answers it. Pass an order ID, and you get back a WC_Order object you can read, change, and save.

This guide covers the return values, how to read order data, four practical use-cases, and the mistakes that cause fatal errors.

What Does wc_get_order() Do?

wc_get_order() is a global WooCommerce function that loads an order from its ID. It builds the object through WC_Order_Factory, so you get the right class for the record. Regular orders return a WC_Order, and refunds return a WC_Order_Refund. Both extend WC_Abstract_Order, so the shared getter methods work on either.

Function Signature and Return Values

The signature is wc_get_order( $the_order = false ). The parameter accepts an order ID or a post object. In practice, you almost always pass an ID. The return value depends on what the ID points to:

InputReturns
ID of a regular orderWC_Order
ID of a refundWC_Order_Refund
Missing or invalid IDfalse
Post ID that is not an orderfalse

Load an Order by ID

Pass the ID, then check the return value before you call any method on it. This function prints the order number, or does nothing when the order is missing.

<?php
function my_show_order_number( $order_id ) {
    $order = wc_get_order( $order_id );

    if ( ! $order ) {
        return;
    }

    echo esc_html( $order->get_order_number() );
}

Check the Return Value

Skipping the check is the most common mistake. With a missing order, the next line fails with Call to a member function get_order_number() on false. Also call the function no earlier than the init hook. Before that point, WooCommerce has not registered its order post types, so the function returns false and logs a “doing it wrong” notice.

Read Order Data

Getter methods read one field at a time, such as get_total() or get_billing_email(). When you need several fields, get_data() returns the whole order as a nested array. Use it for exports and debugging. In regular code, prefer the getters, because they make it clear what you read.

Use get_data() for a Full Array

<?php
function my_order_email_and_city( $order_id ) {
    $order = wc_get_order( $order_id );

    if ( ! $order ) {
        return array();
    }

    $data = $order->get_data();

    return array(
        'email' => $data['billing']['email'],
        'city'  => $data['shipping']['city'],
        'total' => $data['total'],
    );
}

Some values in the array, such as dates, are WC_DateTime objects rather than strings. Convert them before you encode the array as JSON, as the export use-case below shows.

wc_get_order() Use-Cases

Each example loads the order with wc_get_order() and then does one job. Put the code in a site-specific plugin or a child theme’s functions.php.

1. Order Status Lookup for a Support Tool

A support agent types an order number into an internal tool and gets the status back. wc_get_order() loads the order, and wc_get_order_status_name() turns the internal key into a readable label.

<?php
function my_support_order_status( $order_id ) {
    $order = wc_get_order( $order_id );

    if ( ! $order ) {
        return 'Order not found';
    }

    return wc_get_order_status_name( $order->get_status() );
}

Keep this lookup behind a capability check, such as current_user_can( 'manage_woocommerce' ). Otherwise anyone can use it to find out whether an order exists.

Emails and thank-you links carry an order key, which proves the visitor received the link. Compare it with key_is_valid() before you show any order details. Then changing the ID in the URL reveals nothing.

<?php
function my_can_view_order( $order_id, $order_key ) {
    $order = wc_get_order( $order_id );

    if ( ! $order ) {
        return false;
    }

    return $order->key_is_valid( $order_key );
}

3. Handle a Payment Provider Webhook

Payment providers send the order ID with the webhook. The handler loads the order, skips it if it is already paid, and otherwise marks it paid. Write my_verify_webhook_signature to check your provider’s signature. Never trust the payload without that check.

<?php
add_action( 'rest_api_init', 'my_register_payment_webhook' );

function my_register_payment_webhook() {
    register_rest_route( 'my-shop/v1', '/payment-webhook', array(
        'methods'             => 'POST',
        'callback'            => 'my_handle_payment_webhook',
        'permission_callback' => 'my_verify_webhook_signature',
    ) );
}

function my_handle_payment_webhook( WP_REST_Request $request ) {
    $order = wc_get_order( absint( $request->get_param( 'order_id' ) ) );

    if ( ! $order ) {
        return new WP_Error( 'order_not_found', 'Order not found.', array( 'status' => 404 ) );
    }

    if ( $order->is_paid() ) {
        return array( 'status' => 'already_paid' );
    }

    $order->payment_complete( sanitize_text_field( $request->get_param( 'transaction_id' ) ) );

    return array( 'status' => 'ok' );
}

4. Export an Order as JSON

A shipping partner or accounting tool may want the full order as JSON. get_data() supplies the array, but a WC_DateTime encodes as an object with date, timezone_type, and timezone keys. Convert those values to ISO 8601 strings first.

<?php
function my_order_as_json( $order_id ) {
    $order = wc_get_order( $order_id );

    if ( ! $order ) {
        return false;
    }

    $data = $order->get_data();

    foreach ( $data as $key => $value ) {
        if ( $value instanceof WC_DateTime ) {
            $data[ $key ] = $value->date( 'c' );
        }
    }

    return wp_json_encode( $data );
}

Tip: In long-running jobs such as cron events, store the order ID and reload the order inside the job. An object holds the data from the moment you loaded it.

Common Mistakes

  • Skipping the false check. A missing order returns false, and calling a method on it causes a fatal error.
  • Calling too early. Use the function no earlier than init. Earlier calls return false.
  • Assuming a refund is an order. Check instanceof WC_Order before you call order-only methods such as payment_complete().
  • Using post functions. get_post() and get_post_meta() miss orders stored in HPOS tables. Use the order object’s methods instead.

FAQs About wc_get_order()

Does wc_get_order() Work With Refunds?

Yes. Pass a refund ID, and you get a WC_Order_Refund back. Check the class with instanceof before you call methods that exist only on regular orders.

Is wc_get_order() the Same as wc_get_orders()?

No. wc_get_order() loads one order by ID. wc_get_orders() runs a query and returns a list of orders.

Can I Pass a WP_Post Object?

Yes. The function accepts a post object as well as an ID. Passing the ID is clearer, and it works the same way under both storage modes.

Does wc_get_order() Work With HPOS?

Yes. The function loads orders from both the legacy post tables and the HPOS tables, so the same call works on either storage. For background, read the WooCommerce HPOS guide.

Conclusion

wc_get_order() is the entry point for most order-related code. Check the return value, read data through getters or get_data(), and you can build lookups, webhooks, and exports on top of it safely.

Need a custom order workflow or integration built for your store? Hire a WooCommerce developer, or reach out to me directly.

About The Author

Picture of Andriy Haydash

Andriy Haydash

Andriy Haydash is a WordPress Expert who helps people build and launch successful WordPress membership and e-learning websites.

Note: Not all of the articles are written directly by me.
Affiliate Disclaimer: Some links in the post may be my affiliate links

Kinsta - Managed Hosting for WordPress

Before YOU Leave...
Join My Newsletter

Get practical tips & tricks on how to start, grow and market your course/membership site.