- What Is the WC_Order Class?
- How to Get a WC_Order Object
- Reading Order Data
- Changing Orders and Saving Data
- Working With Refunds
- WC_Order Use-Cases
- 1. Send Paid Orders to an External CRM
- 2. Notify the Warehouse on Status Change
- 3. Personalize the Thank-You Page
- 4. Store Custom Checkout Data
- 5. Calculate Sales Over the Last 30 Days
- Common Mistakes
- FAQs About WC_Order
- What Is the Difference Between WC_Order and WC_Order_Refund?
- Is WC_Order the Same as WP_Post?
- Do I Need to Call save() After update_status()?
- What Does wc_get_order() Return for an Invalid ID?
- Conclusion
Every purchase on a WooCommerce store ends up as an order, and in code that order is a WC_Order object. Payment gateways, emails, admin screens, and reports all read from it, so once you know how to work with this class, you can hook into almost any part of the purchase flow.
This guide explains what the WC_Order class does, how to load and read an order, how to change its status and save custom data, and five practical use-cases you can adapt to your store.
What Is the WC_Order Class?
WC_Order is the class that represents a regular WooCommerce order. It extends WC_Abstract_Order, which holds the logic shared by orders and refunds, and that class extends WC_Data, the base class WooCommerce uses for its data objects. An order keeps its data in internal properties such as the billing address, totals, status, and line items. You read or change them through getter and setter methods.
Refunds use a sibling class, WC_Order_Refund, which also extends WC_Abstract_Order. Code that handles orders often checks the object type first, because some methods exist only on regular orders.
High-Performance Order Storage (HPOS) keeps orders in custom tables instead of wp_posts. Using class methods rather than get_post() or get_post_meta() keeps your code working with either storage. For the storage side, read the WooCommerce HPOS guide.
How to Get a WC_Order Object
When you only need to read an order, load it with wc_get_order() instead of creating a new WC_Order. The function accepts an order ID, returns the matching object, or returns false if the order does not exist. For the product side of the same pattern, see the WC_Product_Query object guide.
Get an Order With wc_get_order()
This helper returns a short summary of an order. It returns an empty string when the order is missing, so the caller never touches a value that is not an object.
<?php
function my_get_order_summary( $order_id ) {
$order = wc_get_order( $order_id );
if ( ! $order ) {
return '';
}
return sprintf(
'Order #%s: %s, total %s %s',
$order->get_order_number(),
$order->get_status(),
$order->get_total(),
$order->get_currency()
);
}Check the Object Type
An ID can also belong to a refund. Check the class before you call order-only methods such as payment_complete():
<?php
$object = wc_get_order( $order_id );
if ( $object instanceof WC_Order ) {
// Regular order: payment and status methods are available.
}Reading Order Data
Getter methods return stored values. Totals come back as strings, so cast them with (float) before you do arithmetic that needs a float.
Common Getter Methods
| Method | Returns |
|---|---|
$order->get_id() | Order ID as an integer |
$order->get_status() | Status key without the wc- prefix, such as processing |
$order->get_total() | Grand total |
$order->get_currency() | Currency code, such as USD |
$order->get_billing_email() | Billing email address |
$order->get_payment_method() | Gateway ID, such as stripe |
$order->get_transaction_id() | Transaction ID returned by the gateway |
$order->get_date_created() | WC_DateTime object for the creation date |
$order->get_customer_id() | Customer user ID, or 0 for guests |
$order->get_order_key() | Key that verifies access to the order page |
Looping Through Line Items
get_items( 'line_item' ) returns only product rows. Without the argument, the loop also includes shipping, fee, and tax rows.
<?php
function my_get_order_lines( $order_id ) {
$order = wc_get_order( $order_id );
if ( ! $order ) {
return array();
}
$lines = array();
foreach ( $order->get_items( 'line_item' ) as $item ) {
$lines[] = array(
'product_id' => $item->get_product_id(),
'name' => $item->get_name(),
'quantity' => $item->get_quantity(),
'total' => $item->get_total(),
);
}
return $lines;
}Changing Orders and Saving Data
Setters change the object in memory. Nothing reaches the database until you call save(). The status methods are the exception: update_status() and payment_complete() save the order for you.
Change the Status
update_status() changes the status, adds a note, and saves the order in one call. Its third argument marks the change as manual.
<?php
$order = wc_get_order( $order_id );
// Changes the status, adds the note, and saves the order.
$order->update_status( 'on-hold', 'Waiting for the bank transfer.' );For a successful gateway payment, call payment_complete() with the gateway transaction ID. It marks the order as paid, moves it to Processing or Completed, and fires woocommerce_payment_complete. Check is_paid() first so the payment hooks run only once.
Store Custom Data With Meta
Store your own data with update_meta_data() and read it with get_meta(). Do not use update_post_meta() for orders, because HPOS keeps order meta in its own table. Keys that start with an underscore stay hidden from the admin screen.
<?php
$order->update_meta_data( '_my_crm_id', 'C-5521' );
$order->save();
$crm_id = $order->get_meta( '_my_crm_id', true );
Tip: Call
save()once after several setters. Each save is a database write, so batching changes saves time.
Working With Refunds
Create refunds with wc_create_refund(). It returns a WC_Order_Refund object, or a WP_Error when the amount is invalid. Set refund_payment to true to ask the gateway to return the money, if it supports refunds. Read the totals on the parent order with get_total_refunded() and get_remaining_refund_amount().
<?php
$refund = wc_create_refund( array(
'order_id' => $order_id,
'amount' => 10.00,
'reason' => 'Damaged item',
'refund_payment' => true,
) );
if ( is_wp_error( $refund ) ) {
// Show $refund->get_error_message() to the admin.
}WC_Order Use-Cases
Each example below hooks into an action WooCommerce already fires, then reads the order through WC_Order. Put the code in a site-specific plugin or a child theme, and replace the example addresses and URLs with your own.
1. Send Paid Orders to an External CRM
When a payment succeeds, send the order to your CRM, ERP, or accounting tool. woocommerce_payment_complete passes the order ID, and the callback loads the order with wc_get_order().
<?php
add_action( 'woocommerce_payment_complete', 'my_send_order_to_crm' );
function my_send_order_to_crm( $order_id ) {
$order = wc_get_order( $order_id );
if ( ! $order ) {
return;
}
$payload = array(
'order_id' => $order->get_id(),
'email' => $order->get_billing_email(),
'total' => $order->get_total(),
'currency' => $order->get_currency(),
'items' => array(),
);
foreach ( $order->get_items( 'line_item' ) as $item ) {
$payload['items'][] = array(
'name' => $item->get_name(),
'quantity' => $item->get_quantity(),
);
}
wp_remote_post( 'https://crm.example.com/api/orders', array(
'headers' => array( 'Content-Type' => 'application/json' ),
'body' => wp_json_encode( $payload ),
'timeout' => 10,
) );
}wp_remote_post() waits for the response, so keep the timeout short. For slow APIs, queue the request with as_enqueue_async_action() from Action Scheduler, which ships with WooCommerce.
2. Notify the Warehouse on Status Change
woocommerce_order_status_changed fires on every status change and passes the order ID, the old status, the new status, and the order object. Filter on the transition you need, so the warehouse hears about only paid orders.
<?php
add_action( 'woocommerce_order_status_changed', 'my_notify_warehouse', 10, 4 );
function my_notify_warehouse( $order_id, $from_status, $to_status, $order ) {
if ( 'processing' !== $to_status ) {
return;
}
wp_mail(
'[email protected]',
'Ship order #' . $order->get_order_number(),
'Ship to: ' . $order->get_shipping_address_1() . ', ' . $order->get_shipping_city()
);
}3. Personalize the Thank-You Page
woocommerce_thankyou runs on the order received page. WooCommerce validates the order key before it renders that page, so the order you receive is one the visitor is allowed to see. Unpaid orders, such as bank transfers, return early, so the message appears only after payment.
<?php
add_action( 'woocommerce_thankyou', 'my_thankyou_message' );
function my_thankyou_message( $order_id ) {
$order = wc_get_order( $order_id );
if ( ! $order || ! $order->is_paid() ) {
return;
}
echo '<p>Thanks, ' . esc_html( $order->get_billing_first_name() ) . '! Your order #' . esc_html( $order->get_order_number() ) . ' is confirmed.</p>';
}4. Store Custom Checkout Data
If you add a field to checkout using the woocommerce_form_field() guide technique, save its value to the order in woocommerce_checkout_create_order. The hook passes the order before WooCommerce stores it, so the value is saved with the rest of the order data.
<?php
add_action( 'woocommerce_checkout_create_order', 'my_save_gift_message' );
function my_save_gift_message( $order ) {
if ( ! empty( $_POST['my_gift_message'] ) ) {
$order->update_meta_data( '_my_gift_message', sanitize_textarea_field( wp_unslash( $_POST['my_gift_message'] ) ) );
}
}5. Calculate Sales Over the Last 30 Days
wc_get_orders() returns WC_Order objects that match your filters. Subtract refunds so the total reflects the money you kept. This snippet reads up to 500 orders. For larger stores, paginate with paginate or move the calculation to a cron job. To query products the same way, see the wc_get_products guide.
<?php
function my_sales_last_30_days() {
$orders = wc_get_orders( array(
'status' => array( 'completed', 'processing' ),
'date_created' => '>' . ( time() - 30 * DAY_IN_SECONDS ),
'limit' => 500,
'return' => 'objects',
) );
$total = 0;
foreach ( $orders as $order ) {
$total += $order->get_total() - $order->get_total_refunded();
}
return $total;
}Common Mistakes
- Forgetting save(). Setters change memory only. Call
save()unless you usedupdate_status()orpayment_complete(). - Using post functions.
get_post_meta()andupdate_post_meta()do not read HPOS orders. Useget_meta()andupdate_meta_data()on the order. - Skipping the false check.
wc_get_order()returnsfalsefor a missing ID. Calling a method on it causes a fatal error. - Calling payment_complete() twice. Check
is_paid()first, so the payment hooks do not run again.
FAQs About WC_Order
What Is the Difference Between WC_Order and WC_Order_Refund?
WC_Order represents a customer purchase. WC_Order_Refund represents a refund and belongs to a parent order. Both extend the same base class, so many methods are shared. Payment methods such as payment_complete() exist only on regular orders.
Is WC_Order the Same as WP_Post?
No. WC_Order is not a WP_Post. Read and write order data through its methods instead of get_post() or get_post_meta().
Do I Need to Call save() After update_status()?
No. update_status() and payment_complete() save the order. Calling save() afterward is harmless, but it adds a database write.
What Does wc_get_order() Return for an Invalid ID?
It returns false. Check the return value before you call any method on it.
Conclusion
WC_Order is the object behind every WooCommerce purchase. Load it with wc_get_order(), read data through getters, change it through setters and status methods, and save. Once that is in place, connecting orders to other systems takes only a few lines of code.
Need a custom order workflow or integration built for your store? Hire a WooCommerce developer, or reach out to me directly.
