Guide: WooCommerce Order Data

WooCommerce stores order records in the posts table and its associated data in the postmeta table by default. To access and update order data, WooCommerce offers a number of helper methods.

Order Object

To use WooCommerce methods, you’ll need an instance of the WC_Order object.

Use `wc_get_order` to get an instance of the order. Example:

// Get order ID #123
$order = wc_get_order( 123 );

Basic Order Properties

// Get Order ID
$order->get_id();

// Get Order Key
$order->get_order_key();

Get custom meta data

WooCommerce has built-in methods for accessing most data it sets (which I’ll list below), but if need to access custom meta data you’ve added or another WooCommerce extension has added, get_meta is the method for you.

$order->get_meta( $key );

Let’s say you want to add gift note data to an order:

$order->update_meta_data( 'gift_note', 'Hi Mom!' );
$order->save();

After saving the meta data, you would access it with:

$order->get_meta( 'gift_note' );

Order Status

// Get Order Status
$order->get_status();

You can get a list of all valid order statuses using `wc_get_order_statuses()`:

wc_get_order_statuses();

// Output
["wc-pending"] => "Pending payment"
["wc-processing"] => "Processing"
["wc-on-hold"]=> "On hold"
["wc-completed"]=> "Completed"
["wc-cancelled"]=> "Cancelled"
["wc-refunded"]=> "Refunded"
["wc-failed"]=> "Failed"

To set an order status:

$order->set_status( 'completed' );

The “wc-” prefix is detected automatically when setting a status. So `$order->set_status( ‘completed’ );` is exactly the same as `$order->set_status( ‘wc-completed’ );`.

Order Pricing and Totals

Methods for getting prices and totals from the order.

$order->get_cart_tax();
$order->get_currency();
$order->get_discount_tax();
$order->get_discount_to_display();
$order->get_discount_total();
$order->get_fees();
$order->get_formatted_line_subtotal();
$order->get_formatted_order_total();
$order->get_item_count_refunded();
$order->get_qty_refunded_for_item();
$order->get_remaining_refund_amount();
$order->get_shipping_tax();
$order->get_shipping_total();
$order->get_subtotal_to_display();
$order->get_subtotal();
$order->get_tax_location();
$order->get_tax_refunded_for_item();
$order->get_tax_totals();
$order->get_taxes();
$order->get_total_discount();
$order->get_total_qty_refunded();
$order->get_total_refunded_for_item();
$order->get_total_refunded();
$order->get_total_shipping_refunded();
$order->get_total_tax_refunded_by_rate_id();
$order->get_total_tax_refunded();
$order->get_total_tax();
$order->get_total();
$order->get_prices_include_tax();

Order Dates

$order->get_date_created();
$order->get_date_modified();
$order->get_date_completed();
$order->get_date_paid();

If you have third-party services that pull from your WooCommerce API, it’s often useful to update the modified date of an order after modifing any data. Here’s how to do that:

$order->set_date_modified( time() );
$order->save();

Other order date setters:

// Date should be mysql format.
$timestamp = time();

$order->set_date_paid( $timestamp );
$order->set_date_completed( $timestamp );

Order Address and User Information

$order->get_address();
$order->get_billing_address_1();
$order->get_billing_address_2();
$order->get_billing_city();
$order->get_billing_company();
$order->get_billing_country();
$order->get_billing_email();
$order->get_billing_first_name();
$order->get_billing_last_name();
$order->get_billing_phone();
$order->get_billing_postcode();
$order->get_billing_state();
$order->get_created_via();
$order->get_customer_id();
$order->get_customer_ip_address();
$order->get_customer_note();
$order->get_customer_user_agent();
$order->get_shipping_address_1();
$order->get_shipping_address_2();
$order->get_shipping_city();
$order->get_shipping_company();
$order->get_shipping_country();
$order->get_shipping_first_name();
$order->get_shipping_last_name();
$order->get_shipping_postcode();
$order->get_shipping_state();
$order->get_user_id();
$order->get_user();

To update address and user data:

// Billing Address
set_billing_first_name( $string )
set_billing_last_name( $string )
set_billing_company( $string )
set_billing_address_1( $string );
set_billing_address_2( $string );
set_billing_city( $string );
set_billing_state( $string );
set_billing_postcode( $string );
set_billing_country( $string );
set_billing_phone( $string );

// Shipping Address
set_shipping_first_name( $string );
set_shipping_last_name( $string );
set_shipping_company( $string );
set_shipping_address_2( $string );
set_shipping_city( $string );
set_shipping_state( $string );
set_shipping_postcode( $string );
set_shipping_country( $string );
set_shipping_phone( $string );

// Order customer
set_customer_id( $id )
set_billing_email( $email );

// If the order has user (customer_id), update billing email
// to use the email for that customer (user).
maybe_set_user_billing_email();

Shipping Methods

$order->get_shipping_method();
$order->get_shipping_methods();
$order->get_shipping_to_display();

Payment Methods

$order->get_payment_method();
$order->get_payment_method_title();
$order->get_transaction_id();

Additional Documentation

If you need more information about how any of these methods work, the WooCommerce source code is one of the best places to look. You can find all the methods listed here and many more in the WC_Abstract_Order class.

Leave a Reply