"how to get woocommerce order details" Code Answer

4

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.

related:
• 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();

endforeach;

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

By fhonics on May 2 2022
0

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
$order->get_id();
$order->get_order_key();
 
// Get Order Totals $0.00
$order->get_formatted_order_total();
$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_shipping_tax();
$order->get_shipping_total();
$order->get_subtotal();
$order->get_subtotal_to_display();
$order->get_tax_location();
$order->get_tax_totals();
$order->get_taxes();
$order->get_total();
$order->get_total_discount();
$order->get_total_tax();
$order->get_total_refunded();
$order->get_total_tax_refunded();
$order->get_total_shipping_refunded();
$order->get_item_count_refunded();
$order->get_total_qty_refunded();
$order->get_qty_refunded_for_item();
$order->get_total_refunded_for_item();
$order->get_tax_refunded_for_item();
$order->get_total_tax_refunded_by_rate_id();
$order->get_remaining_refund_amount();
  
// 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
$order->get_items_key();
$order->get_items_tax_classes();
$order->get_item_count();
$order->get_item_total();
$order->get_downloadable_items();
$order->get_coupon_codes();
  
// Get Order Lines
$order->get_line_subtotal();
$order->get_line_tax();
$order->get_line_total();
  
// Get Order Shipping
$order->get_shipping_method();
$order->get_shipping_methods();
$order->get_shipping_to_display();
  
// Get Order Dates
$order->get_date_created();
$order->get_date_modified();
$order->get_date_completed();
$order->get_date_paid();
  
// Get Order User, Billing & Shipping Addresses
$order->get_customer_id();
$order->get_user_id();
$order->get_user();
$order->get_customer_ip_address();
$order->get_customer_user_agent();
$order->get_created_via();
$order->get_customer_note();
$order->get_address_prop();
$order->get_billing_first_name();
$order->get_billing_last_name();
$order->get_billing_company();
$order->get_billing_address_1();
$order->get_billing_address_2();
$order->get_billing_city();
$order->get_billing_state();
$order->get_billing_postcode();
$order->get_billing_country();
$order->get_billing_email();
$order->get_billing_phone();
$order->get_shipping_first_name();
$order->get_shipping_last_name();
$order->get_shipping_company();
$order->get_shipping_address_1();
$order->get_shipping_address_2();
$order->get_shipping_city();
$order->get_shipping_state();
$order->get_shipping_postcode();
$order->get_shipping_country();
$order->get_address();
$order->get_shipping_address_map_url();
$order->get_formatted_billing_full_name();
$order->get_formatted_shipping_full_name();
$order->get_formatted_billing_address();
$order->get_formatted_shipping_address();
  
// Get Order Payment Details
$order->get_payment_method();
$order->get_payment_method_title();
$order->get_transaction_id();
  
// Get Order URLs
$order->get_checkout_payment_url();
$order->get_checkout_order_received_url();
$order->get_cancel_order_url();
$order->get_cancel_order_url_raw();
$order->get_cancel_endpoint();
$order->get_view_order_url();
$order->get_edit_order_url();
  
// Get Order Status
$order->get_status();
 
// Get Thank You Page URL
$order->get_checkout_order_received_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_id();
   $order->get_formatted_order_total( );
   // etc.
   // etc.
}
By mp2 on October 26 2022
0
 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.