How to get woocommerce order details?

by edmond_brakus , in category: PHP CMS , 19 days ago

How to get woocommerce order details?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by haylee.mertz , 18 days ago

@edmond_brakus 

To get WooCommerce order details, you can use the following code snippet in your WordPress theme or plugin:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
$order_id = 123; // Replace 123 with the actual order ID

$order = new WC_Order($order_id);

$order_data = $order->get_data();

// Output order details
echo 'Order ID: ' . $order->get_id();
echo 'Order total: ' . wc_price($order->get_total());
echo 'Order status: ' . ucfirst($order->get_status());
echo 'Order date: ' . $order->get_date_created()->date('Y-m-d H:i:s');
echo 'Customer ID: ' . $order->get_customer_id();

// Get billing address
$billing_address = $order_data['billing'];
echo 'Billing first name: ' . $billing_address['first_name'];
echo 'Billing last name: ' . $billing_address['last_name'];
echo 'Billing email: ' . $billing_address['email'];

// Get shipping address
$shipping_address = $order_data['shipping'];
echo 'Shipping first name: ' . $shipping_address['first_name'];
echo 'Shipping last name: ' . $shipping_address['last_name'];
echo 'Shipping email: ' . $shipping_address['email'];

// Get line items
foreach ($order->get_items() as $item_id => $item) {
    echo 'Product ID: ' . $item->get_product_id();
    echo 'Product name: ' . $item->get_name();
    echo 'Product quantity: ' . $item->get_quantity();
    echo 'Line total: ' . wc_price($item->get_total());
}

// Get order notes
$notes = $order->get_customer_order_notes();
foreach ($notes as $note) {
    echo 'Note: ' . $note->comment_content;
    echo 'Note date: ' . $note->comment_date;
}


Replace 123 with the actual order ID you want to retrieve details for. This code snippet will output various details about the specified order such as order total, status, date, customer details, billing and shipping address, line items, and order notes. You can customize the output or use this data in any way you need.