WooCommerce Get Order Details By Order Id

Hi Guys,

If you want to get the order details by order id, you can use the following code snippet:

$order_id =10;

$order = wc_get_order($order_id);

$order_data = $order->get_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_total_final = $order_data[‘total’];

$order_date_created = $order_data[‘date_created’]->date(‘M d, Y’);
$order_date_modified = $order_data[‘date_modified’]->date(‘Y-m-d H:i:s’);

$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 = wc_price($order_data[‘shipping_total’]);
$order_shipping_tax = $order_data[‘shipping_tax’];
$order_total = $order_data[‘cart_tax’];
$order_total_tax = $order_data[‘total_tax’];
$order_customer_id = $order_data[‘customer_id’];
/**************** 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’];

$states = WC()->countries->get_states($order_shipping_country);
$order_shipping_state = !empty($states[$order_shipping_state]) ? $states[$order_shipping_state] : ”;

$order_shipping_country = WC()->countries->countries[$order_shipping_country];
if (!empty($order_shipping_address_2)) {
$fullShipAddress = $order_shipping_address_1 . ‘, ‘ . $order_shipping_address_2 . ‘, ‘ . $order_shipping_city . ‘, ‘ . $order_shipping_state . ‘, ‘ . $order_shipping_postcode . ‘, ‘ . $order_shipping_country;
} else {
$fullShipAddress = $order_shipping_address_1 . ‘, ‘ . $order_shipping_city . ‘, ‘ . $order_shipping_state . ‘, ‘ . $order_shipping_postcode . ‘, ‘ . $order_shipping_country;
}

To get the order item details, use the following code snippets:

$mainSubtotal = 0;
foreach ($order->get_items() as $item_key => $item_values){
$item_id = $item_values->get_id();

$item_name = $item_values->get_name(); // Name of the product
$item_type = $item_values->get_type(); // Type of the order item (“line_item”)

$product_id = $item_values->get_product_id(); // the Product id
$image = wp_get_attachment_image_src(get_post_thumbnail_id($product_id), ‘single-post-thumbnail’);
$imageSrcUrl = isset($image[0]) ? $image[0] : ”;

$product = $item_values->get_product(); // the WC_Product object
## Access Order Items data properties (in an array of values)
$item_data = $item_values->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 = wc_price($item_data[‘subtotal’]);
$mainSubtotal += $item_data[‘subtotal’];
$line_subtotal_tax = $item_data[‘subtotal_tax’];
$line_total = wc_price($item_data[‘total’]);
$line_total_tax = $item_data[‘total_tax’];

// Get data from The WC_product object using methods (examples)
$product_type = $product->get_type();
$product_sku = $product->get_sku();
$product_price = wc_price($product->get_price());
$stock_quantity = $product->get_stock_quantity();

}

That’s it! You are done.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s