"how to get woocommerce order details" Code Answer


woocommerce orders in version 3.0+

since woocommerce mega major update 3.0+ things have changed quite a lot:

  • for wc_order object, properties can't be accessed directly anymore as before and will throw some errors.
  • new wc_order and wc_abstract_order getter and setter methods are now required on the wc_order object instance.
  • also, there are some new classes for order items:
    • wc_order_item class,
    • wc_order_item_product class,
    • wc_order_item_tax class,
    • wc_order_item_shipping class,
    • wc_order_item_coupon class,
    • wc_order_item_fee class.
  • additionally, wc_data abstract class allow to access order and order items data using get_data(), get_meta_data() and get_meta() methods.

• how to get customer details from order in woocommerce?
• get order items and wc_order_item_product in woocommerce 3

so the order items properties will not be accessible as before in a foreach loop and you will have to use these specific getter and setter methods instead.

using some wc_order and wc_abstract_order methods (example):

// get an instance of the wc_order object (same as before)
$order = wc_get_order( $order_id );

$order_id  = $order->get_id(); // get the order id
$parent_id = $order->get_parent_id(); // get the parent order id (for subscriptions…)

$user_id   = $order->get_user_id(); // get the costumer id
$user      = $order->get_user(); // get the wp_user object

$order_status  = $order->get_status(); // get the order status (see the conditional method has_status() below)
$currency      = $order->get_currency(); // get the currency used  
$payment_method = $order->get_payment_method(); // get the payment method id
$payment_title = $order->get_payment_method_title(); // get the payment method title
$date_created  = $order->get_date_created(); // get date created (wc_datetime object)
$date_modified = $order->get_date_modified(); // get date modified (wc_datetime object)

$billing_country = $order->get_billing_country(); // customer billing country

// ... and so on ...

for order status as a conditional method (where "the_targeted_status" need to be defined and replaced by an order status to target a specific order status):

if ( $order->has_status('completed') ) {
    // do something

get and access to the order data properties (in an array of values):

// get an instance of the wc_order object
$order = wc_get_order( $order_id );

$order_data = $order->get_data(); // the order data

$order_id = $order_data['id'];
$order_parent_id = $order_data['parent_id'];
$order_status = $order_data['status'];
$order_currency = $order_data['currency'];
$order_version = $order_data['version'];
$order_payment_method = $order_data['payment_method'];
$order_payment_method_title = $order_data['payment_method_title'];
$order_payment_method = $order_data['payment_method'];
$order_payment_method = $order_data['payment_method'];

## creation and modified wc_datetime object date string ##

// using a formated date ( with php date() function as method)
$order_date_created = $order_data['date_created']->date('y-m-d h:i:s');
$order_date_modified = $order_data['date_modified']->date('y-m-d h:i:s');

// using a timestamp ( with php gettimestamp() function as method)
$order_timestamp_created = $order_data['date_created']->gettimestamp();
$order_timestamp_modified = $order_data['date_modified']->gettimestamp();

$order_discount_total = $order_data['discount_total'];
$order_discount_tax = $order_data['discount_tax'];
$order_shipping_total = $order_data['shipping_total'];
$order_shipping_tax = $order_data['shipping_tax'];
$order_total = $order_data['total'];
$order_total_tax = $order_data['total_tax'];
$order_customer_id = $order_data['customer_id']; // ... and so on

## billing information:

$order_billing_first_name = $order_data['billing']['first_name'];
$order_billing_last_name = $order_data['billing']['last_name'];
$order_billing_company = $order_data['billing']['company'];
$order_billing_address_1 = $order_data['billing']['address_1'];
$order_billing_address_2 = $order_data['billing']['address_2'];
$order_billing_city = $order_data['billing']['city'];
$order_billing_state = $order_data['billing']['state'];
$order_billing_postcode = $order_data['billing']['postcode'];
$order_billing_country = $order_data['billing']['country'];
$order_billing_email = $order_data['billing']['email'];
$order_billing_phone = $order_data['billing']['phone'];

## shipping information:

$order_shipping_first_name = $order_data['shipping']['first_name'];
$order_shipping_last_name = $order_data['shipping']['last_name'];
$order_shipping_company = $order_data['shipping']['company'];
$order_shipping_address_1 = $order_data['shipping']['address_1'];
$order_shipping_address_2 = $order_data['shipping']['address_2'];
$order_shipping_city = $order_data['shipping']['city'];
$order_shipping_state = $order_data['shipping']['state'];
$order_shipping_postcode = $order_data['shipping']['postcode'];
$order_shipping_country = $order_data['shipping']['country'];

get the order items and access the data with wc_order_item_product and wc_order_item methods:

// get an instance of the wc_order object
$order = wc_get_order($order_id);

// iterating through each wc_order_item_product objects
foreach ($order->get_items() as $item_key => $item ):

    ## using wc_order_item methods ##

    // item id is directly accessible from the $item_key in the foreach loop or
    $item_id = $item->get_id();

    ## using wc_order_item_product methods ##

    $product      = $item->get_product(); // get the wc_product object

    $product_id   = $item->get_product_id(); // the product id
    $variation_id = $item->get_variation_id(); // the variation id

    $item_type    = $item->get_type(); // type of the order item ("line_item")

    $item_name    = $item->get_name(); // name of the product
    $quantity     = $item->get_quantity();  
    $tax_class    = $item->get_tax_class();
    $line_subtotal     = $item->get_subtotal(); // line subtotal (non discounted)
    $line_subtotal_tax = $item->get_subtotal_tax(); // line subtotal tax (non discounted)
    $line_total        = $item->get_total(); // line total (discounted)
    $line_total_tax    = $item->get_total_tax(); // line total tax (discounted)

    ## access order items data properties (in an array of values) ##
    $item_data    = $item->get_data();

    $product_name = $item_data['name'];
    $product_id   = $item_data['product_id'];
    $variation_id = $item_data['variation_id'];
    $quantity     = $item_data['quantity'];
    $tax_class    = $item_data['tax_class'];
    $line_subtotal     = $item_data['subtotal'];
    $line_subtotal_tax = $item_data['subtotal_tax'];
    $line_total        = $item_data['total'];
    $line_total_tax    = $item_data['total_tax'];

    // get data from the wc_product object using methods (examples)
    $product        = $item->get_product(); // get the wc_product object

    $product_type   = $product->get_type();
    $product_sku    = $product->get_sku();
    $product_price  = $product->get_price();
    $stock_quantity = $product->get_stock_quantity();


so using get_data() method allow us to access to the protected data (associative array mode) …

By fhonics on May 2 2022

1. You have access to $order variable

Hooks (do_action and apply_filters) use additional arguments which are passed on to the function. If they allow you to use the “$order” object you’re in business. Here’s how to get all the order information:

// Get Order ID and Key
// Get Order Totals $0.00
// Get and Loop Over Order Items
foreach ( $order->get_items() as $item_id => $item ) {
   $product_id = $item->get_product_id();
   $variation_id = $item->get_variation_id();
   $product = $item->get_product(); // see link above to get $product info
   $product_name = $item->get_name();
   $quantity = $item->get_quantity();
   $subtotal = $item->get_subtotal();
   $total = $item->get_total();
   $tax = $item->get_subtotal_tax();
   $tax_class = $item->get_tax_class();
   $tax_status = $item->get_tax_status();
   $allmeta = $item->get_meta_data();
   $somemeta = $item->get_meta( '_whatever', true );
   $item_type = $item->get_type(); // e.g. "line_item"
// Other Secondary Items Stuff
// Get Order Lines
// Get Order Shipping
// Get Order Dates
// Get Order User, Billing & Shipping Addresses
// Get Order Payment Details
// Get Order URLs
// Get Order Status
// Get Thank You Page URL

2. You have access to $order_id variable

If you have access to the order ID (once again, usually the do_action or apply_filters might give you this), you have to get the order object first. Then do the exact same things as above.

// Get $order object from order ID
$order = wc_get_order( $order_id );
// Now you have access to (see above)...
if ( $order ) {
   $order->get_formatted_order_total( );
   // etc.
   // etc.

3. You have access to $email variable

If you are working with WooCommerce emails, often you will have the $email object available as parameter. In order to get the object from that, you need an additional step. Then do the exact same things as above.

// Get $order object from $email
$order = $email->object;
// Now you have access to (see above)...
if ( $order ) {
   $order->get_formatted_order_total( );
   // etc.
   // etc.
By mp2 on October 26 2022
 I try to parse order object like var_dump() and get vars, it’s nightmare believe me.
By mp2 on October 26 2022

Answers related to “how to get woocommerce order details”

Only authorized users can answer the Search term. Please sign in first, or register a free account.