How to get WooCommerce order details
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
andWC_Abstract_Order
getter and setter methods are now required on theWC_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 usingget_data()
,get_meta_data()
andget_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) …
Get Woocommerce order details in a page from a custom form
Updated - Try the following instead:
// Function that output order details
function display_order_details() {
// Exit if Order number not submitted
if ( ! isset( $_POST['order_number'] ) )
return; // Exit
if( $_POST['order_number'] > 0 )
$order_id = sanitize_text_field( $_POST['order_number'] );
else
return; // Exit
## $order = wc_get_order( $order_id ); // Not really needed in this case
echo '<h3>THE ORDER META DATA (Using the array syntax notation):</h3>
<p>';
$billing_first_name = get_post_meta( $order_id, '_billing_first_name', true );
if( ! empty( $billing_first_name ) )
echo 'Billing first name: ' . $billing_first_name . '<br>';
$billing_last_name = get_post_meta( $order_id, '_billing_last_name', true );
if( ! empty( $billing_last_name ) )
echo 'Billing last name: ' . $billing_last_name . '<br>';
$billing_address_index = get_post_meta( $order_id, '_billing_address_index', true );
if( ! empty( $billing_address_index ) )
echo 'Billing details: ' . $billing_address_index . '<br>';
$shipping_address_index = get_post_meta( $order_id, '_shipping_address_index', true );
if( ! empty( $shipping_address_index ) )
echo 'Shipping details: ' . $shipping_address_index;
echo '</p><br>
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - <br><br>';
}
// Shotcode that display the form and output order details once submitted
add_shortcode( 'order_details', 'form_get_order_details' );
function form_get_order_details(){
ob_start(); // Buffering data
?>
<form action="" method="post">
<label for="order_number">Order number</label><br>
<input type="text" name="order_number" size="30"><br><br>
<input type="submit" id="submit" value="Find and output"><br>
</form>
<?php
display_order_details();
return ob_get_clean(); // Output data from buffer
}
Code goes in function.php file of your active child theme (or active theme). Tested and works.
POSSIBLE USAGE:
- In any page (or post) you can use the shortcode
[order_details]
in the WordPress text editor. - In a php file you will use:
echo do_shortcode("[order_details]");
- In a php file in the html, you will use:
<?php echo do_shortcode("[order_details]"); ?>
It should work and display order details once the order number will be submitted
Related thread: How to get WooCommerce order details
Get woocommerce order details from database
All you need is well documented here - https://businessbloomer.com/woocommerce-easily-get-order-info-total-items-etc-from-order-object/ - once you have the order ID, you can easily get all the info you need. Cheers.
How to get last order details of a customer in Woocommerce
As you mention this wont work correctly if a user has no orders yet. The get_last_order() returns false if this is the case. So you would need to check if get_last_order is false or not an object, and if so display some alternate html eg
//Start Woocommerce Add Info Order on Dashboard
<?php
// For logged in users only
if ( is_user_logged_in() ) :
$user_id = get_current_user_id(); // The current user ID
// Get the WC_Customer instance Object for the current user
$customer = new WC_Customer( $user_id );
// Get the last WC_Order Object instance from current customer
$last_order = $customer->get_last_order();
if ( ! $last_order ) { // or check if ( ! is_object( $last_order ) )
?><div class="last_order_items">You currently have no orders</div><?php
} else {
$order_id = $last_order->get_id(); // Get the order id
$order_data = $last_order->get_data(); // Get the order unprotected data in an array
$order_status = $last_order->get_status(); // Get the order status
$currency = $last_order->get_currency(); //Get Currency
?>
<?php foreach ( $last_order->get_items() as $item ) : ?>
<div class="last_order_items"><?php echo $item->get_name(); ?></div>
<div class="last_order_items"><?php echo $order_total = $order_data['total']; ?>€</div>
<div class="last_order_items"><?php echo $order_date_created = $order_data['date_created']->date('Y-m-d H:i:s'); ?></div>
<div class="last_order_items"><?php echo esc_html( wc_get_order_status_name( $order_status ) ); ?></div>
<?php endforeach; ?>
<?php }
endif; ?>
I haven't tested, but I am confident this should work
Get order items data values in Woocommerce
Instead using a SQL query try this (where $order_id
is the post ID (or the order number)):
$order = wc_get_order( $order_id );
echo '<p>'. __('Order total: ') . $order->get_total() . '</p>';
foreach ( $order->get_items() as $item ){
echo '<p>';
echo __('Product name: ') . $item->get_name() . '<br>';
echo __('Quantity: ') . $item->get_quantity() . '<br>';
echo __('Line total: ') . wc_price($item->get_total()) . '</p>';
}
Related:
- How to get WooCommerce order details
- Get Order items and WC_Order_Item_Product in Woocommerce 3
WooCommerce get order id and order key at order-received endpoint
add_action( 'woocommerce_thankyou', 'woocommerce_thankyou_redirect', 4 );
function woocommerce_thankyou_redirect( $order_id ) {
//$order_id. // This contains the specific ID of the order
$order = wc_get_order( $order_id );
$order_key = $order->get_order_key();
wp_redirect( 'redirection here with parameters' );
exit;
}
Try this code snippet.
Access WooCommerce Order Details in Custom Plugin
Not sure why you are using the woocommerce_after_register_post_type
action hook. but if you want just to test whether you can get order details by id or not you can use the init
hook. try the below code.
function middleground_function(){
$order = wc_get_order( 37340 );
echo "<pre>"; print_r( $order ); echo "</pre>";
}
add_action( 'init ', 'middleground_function' );
Related Topics
How to Store .Pdf Files into MySQL as Blobs Using PHP
Clear the Form Field After Successful Submission of PHP Form
Get Woocommerce Carts Total Amount
How to Do Multilevel Menu the Correct Way With MySQL PHP
How to Get Values of Multiple Selected (Dynamic) Checkbox in PHP
How to Not Make Phpmailer Send an Email With Multiple 'To' Addresses
Block Direct Access to a File Over Http But Allow PHP Script Access
How to Update Stock Quantity Using MySQL in PHP
How to Get Comma Separated Values from a Table in Laravel
Auto Update Cart on Click in Woocommerce
Object of Class Mysqli_Result Could Not Be Converted to String
Redirect That Same Page from Where Id Is Coming
How to Pass Something With Apostrophe to Json
How to Check If MySQL Results Returned Empty in PHP
In Laravel,How to Use Foreach Loop
How to Get Toggle Button Working With PHP