Get orders shipping items details in WooCommerce 3
If you want to get the Order Items Shipping data, you need first to get them in a foreach loop (for 'shipping'
item type) and to use WC_Order_Item_Shipping methods to access data
$order_id = 528; // For example
// An instance of
$order = wc_get_order($order_id);
// Iterating through order shipping items
foreach( $order->get_items( 'shipping' ) as $item_id => $item ){
$order_item_name = $item->get_name();
$order_item_type = $item->get_type();
$shipping_method_title = $item->get_method_title();
$shipping_method_id = $item->get_method_id(); // The method ID
$shipping_method_instance_id = $item->get_instance_id(); // The instance ID
$shipping_method_total = $item->get_total();
$shipping_method_total_tax = $item->get_total_tax();
$shipping_method_taxes = $item->get_taxes();
}
You can also get an array of this (unprotected and accessible) data using the WC_Data
method get_data()
inside this foreach loop:
$order_id = 528; // For example
// An instance of
$order = wc_get_order($order_id);
// Iterating through order shipping items
foreach( $order->get_items( 'shipping' ) as $item_id => $item ){
// Get the data in an unprotected array
$item_data = $item->get_data();
$shipping_data_id = $item_data['id'];
$shipping_data_order_id = $item_data['order_id'];
$shipping_data_name = $item_data['name'];
$shipping_data_method_title = $item_data['method_title'];
$shipping_data_method_id = $item_data['method_id'];
$shipping_data_instance_id = $item_data['instance_id'];
$shipping_data_total = $item_data['total'];
$shipping_data_total_tax = $item_data['total_tax'];
$shipping_data_taxes = $item_data['taxes'];
}
To finish you can use the following WC_Abstract_Order
methods related to "Shipping data", like in this examples:
// Get an instance of the WC_Order object
$order = wc_get_order(522);
// Return an array of shipping costs within this order.
$order->get_shipping_methods(); // same thing than $order->get_items('shipping')
// Conditional function based on the Order shipping method
if( $order->has_shipping_method('flat_rate') ) {
// Output formatted shipping method title.
echo '<p>Shipping method name: '. $order->get_shipping_method()) .'</p>';
How to retrieve Shipping Method of an Order in WooCommerce?
Since WooCommerce 3, if you want to get the shipping formatted method title(s), you can better use WC_Order
method get_shipping_method()
like:
// Get the WC_Order object from the Order ID
$order = wc_get_order( $order_id );
// Output the shipping method(s) formatted method title(s)
echo $order->get_shipping_method();
For other shipping item details see the following threads:
- Get orders shipping method details in WooCommerce 3
- Show Shipping Method data on the Order edit pages in WooCommerce
Get the order total shipping in Woocommerce 3
Since Woocommerce 3 get_total_shipping()
method is replaced by get_shipping_total()
.
So there is actually 2 available CRUD getters methods for shipping totals in WC_Abstract_Order
Class that can be used on the WC_Order
instance object:
get_shipping_total()
that is the shipping total excluding taxesget_shipping_tax()
that is the shipping taxes total
So you will use them with the $order
variable object simply this way:
$shipping_total = $order->get_shipping_total();
$shipping_tax = $order->get_shipping_tax();
There is also get_shipping_to_display()
method that will output the formatted shipping total.
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 order items stock status when order was created in WooCommerce
You can use the following that will save the product stock status as custom order item meta data when customer place an order (so you will always get the stock status when the order was placed):
add_action('woocommerce_checkout_create_order_line_item', 'save_stock_status_order_item_meta', 10, 4 );
function save_stock_status_order_item_meta( $item, $cart_item_key, $values, $order ) {
$item->update_meta_data( '_stock_status', $values['data']->get_stock_status() );
}
Code goes in functions.php file of the active child theme (or active theme).
Then you will replace your "code hooked onto your emails" by this one:
$stock_status = $item->get_meta('_stock_status');
if ( 'instock' === $stock_status ) {
echo __('Available for immediate shipping');
} elseif ( 'onbackorder' === $stock_status ) {
echo __('On Preorder - slow shipping');
}
It should work.
Woocommerce get all orders with shipping method dropdown filter
UPDATE (Based on your edit)
To get the Orders Ids by Shipping methods with a select field of existing shipping methods, the more lighter and accurate way should be to use an SQL query first…
Here is the commented code example (that you can change and embed in a function:
global $wpdb;
// The SQL query
$results = $wpdb->get_results( "
SELECT woim.meta_value as rate_id, woi.order_item_name as rate_name, woi.order_id
FROM {$wpdb->prefix}woocommerce_order_itemmeta as woim
INNER JOIN {$wpdb->prefix}woocommerce_order_items as woi ON woim.order_item_id = woi.order_item_id
WHERE woi.order_item_type LIKE 'shipping'
AND woim.meta_key LIKE 'method_id'
ORDER BY woi.order_id
" );
// Get the selected value to set it in the select field options as "selected"
if( ! empty( $_POST['submit_shipping_method'] ) && $_POST['orders_by_shipping'] != '' ){
$selected_option_value = $_POST['orders_by_shipping'];
echo $selected_option_value.'<br>';
} else $selected_option_value = '';
// First select field option row
$options = array('' => 'Choose a Shipping Method');
// Iterating through the data query
foreach( $results as $result ){
if( empty( $data[$result->rate_id] ) ){
// Preparing the shipping method master array structure
$data[$result->rate_id] = array(
'rate_id' => $result->rate_id,
'rate_name' => $result->rate_name,
'orders_ids' => array()
);
// Preparing the select field option rows
$options[$result->rate_id] = $result->rate_name;
}
// Grouping orders by shipping methods
$data[$result->rate_id]['orders_ids'][] = $result->order_id;
}
// The form and all needed html tags
$select_field_html = '<div class="orders-ids-by-shipping">
<form class="cart" method="post" action="">
<label for="orders_by_shipping">'. __("Get Orders IDs by shipping method:", "woocommerce").'</label><br>
<select name="orders_by_shipping" id="select-orders-shipping">';
// The select field
foreach( $options as $option_value => $option_name ){
if( $selected_option_value == $option_value ){
$selected = ' selected="selected"';
} else $selected = '';
$select_field_html .= '<option value="'.$option_value.'"'.$selected.'>'.$option_name.'</option>';
}
// The Submit button
$select_field_html .= '</select><br><br>
<input type="submit" class="button" name="submit_shipping_method" value="Submit" />
</form>
</div>';
// Display the select field
echo $select_field_html;
// Displaying the orders IDs from the submited shipping method choice
if( ! empty( $_POST['submit_shipping_method'] ) && $_POST['orders_by_shipping'] != '' ){
$rate_id = $_POST['orders_by_shipping'];
$orders_ids = $data[$rate_id]['orders_ids'];
$rate_name = $data[$rate_id]['rate_name'];
$orders_ids_str = implode( ', ', $orders_ids );
echo '<p>Orders IDs for this shipping Method "'.$rate_name.'" are:<br>'. $orders_ids_str .'</p>' ;
}
This will allow you to display for each selected Shipping Method, the corresponding Orders IDS that are using this Shipping Method… Here in this example I output the orders IDs in a coma separated string.
But you can use the array of Orders IDs to make something else more convenient, like embed them in a paginated WP_Query, to avoid overcharging your server when there is many Orders…
Initial answer:
To list all the shipping methods used in your orders you should better use this code instead:
// Get all orders
$orders = wc_get_orders( array(
'numberposts' => -1,
'orderby' => 'id',
'order' => 'DESC'
) );
// Loop though your orders
foreach($orders as $order){
// Get the shipping method title for the current order
$shipping_method_title = $order->get_shipping_method();
// Get the WC_Order_Item_Shipping object for the current order (with all details)
$shipping_methods = $order->get_shipping_methods();
// Get the data from the WC_Order_Item_Shipping object for the current order
foreach( $shipping_methods as $item_id => $shipping_method){
$shipping_method_name = $shipping_data->get_name();
$shipping_method_title = $shipping_data->get_method_title();
$shipping_method_id = $shipping_data->get_method_id();
$shipping_method_total = $shipping_data->get_total();
$shipping_method_total_tax = $shipping_data->get_total_tax();
$shipping_method_taxes = $shipping_data->get_taxes(); // array
}
}
Get Order items and WC_Order_Item_Product in WooCommerce 3
If you use the
get_id()
method, you get your item ID which is15
in your code.
Get the product ID:
The correct WC_Order_Item_Product
method to get the Product Id is: get_product_id()
Get the variation ID:
The correct WC_Order_Item_Product
method to get the variation Id is: get_variation_id()
Get the order ID
The correct WC_Order_Item_Product
method to get the Order Id is: get_order_id()
Get the WC_Product object
The correct WC_Order_Item_Product
method to get WC_Product
object is:get_product()
Get the WC_Order object
The correct WC_Order_Item_Product
method to get WC_order
object is:get_order()
Get and unprotecting the data and meta data using WC_Data
methods:
get_data()
get_meta_data()
Get The WC_Product
object from the order item ID:
$order_item_id = 15;
$item = new WC_Order_Item_Product($order_item_id);
// The product ID
$product_id = $item->get_product_id();
// The variation ID
$variation_id = $item->get_variation_id();
// The WC_Product object
$product = $item->get_product();
// The quantity
$quantity = $item->get_quantity();
// The order ID
$order_id = $item->get_order_id();
// The WC_Order object
$order = $item->get_order();
// The item ID
$item_id = $item->get_id(); // which is your $order_item_id
// The product name
$product_name = $item->get_name(); // … OR: $product->get_name();
// Get the product SKU (using WC_Product method)
$sku = $product->get_sku();
// Get line item totals (non discounted)
$total = $item->get_subtotal(); // Total without tax (non discounted)
$total_tax = $item->get_subtotal_tax(); // Total tax (non discounted)
// Get line item totals (discounted when a coupon is applied)
$total = $item->get_total(); // Total without tax (discounted)
$total_tax = $item->get_total_tax(); // Total tax (discounted)
Get the order items from the WC_Order
object (and use the WC_product
Object):
$order_id = 156; // The order_id
// get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// The loop to get the order items which are WC_Order_Item_Product objects since WC 3+
foreach( $order->get_items() as $item_id => $item ){
//Get the product ID
$product_id = $item->get_product_id();
//Get the variation ID
$variation_id = $item->get_variation_id();
//Get the WC_Product object
$product = $item->get_product();
// The quantity
$quantity = $item->get_quantity();
// The product name
$product_name = $item->get_name(); // … OR: $product->get_name();
//Get the product SKU (using WC_Product method)
$sku = $product->get_sku();
// Get line item totals (non discounted)
$total = $item->get_subtotal(); // Total without tax (non discounted)
$total_tax = $item->get_subtotal_tax(); // Total tax (non discounted)
// Get line item totals (discounted when a coupon is applied)
$total = $item->get_total(); // Total without tax (discounted)
$total_tax = $item->get_total_tax(); // Total tax (discounted)
}
###Accessing data and custom meta data:
1). Unprotecting WC_Order_Item_Product
data and custom meta data:
You can use all WC_Order_Item_Product data
methods or you can unprotect the data using WC_Data
following methods:
$order_id = 156; // The order_id
// get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// The loop to get the order items which are WC_Order_Item_Product objects since WC 3+
foreach( $order->get_items() as $item_id => $item ){
// Get the common data in an array:
$item_product_data_array = $item->get_data();
// Get the special meta data in an array:
$item_product_meta_data_array = $item->get_meta_data();
// Get the specific meta data from a meta_key:
$meta_value = $item->get_meta( 'custom_meta_key', true );
// Get all additional meta data (formatted in an unprotected array)
$formatted_meta_data = $item->get_formatted_meta_data( ' ', true );
// Get line item totals (non discounted)
$total = $item->get_subtotal(); // Total without tax (non discounted)
$total_tax = $item->get_subtotal_tax(); // Total tax (non discounted)
// Get line item totals (discounted when a coupon is applied)
$total = $item->get_total(); // Total without tax (discounted)
$total_tax = $item->get_total_tax(); // Total tax (discounted)
}
2). The Array Access is still possible (for backwards compatibility with legacy arrays) to get the common data directly:
$order_id = 156; // The order_id
// get an instance of the WC_Order object
$order = wc_get_order( $order_id );
// The loop to get the order items which are WC_Order_Item_Product objects since WC 3+
foreach( $order->get_items() as $item_id => $item ){
$product_id = $item['product_id']; // Get the product ID
$variation_id = $item['variation_id']; // Get the variation ID
$product_name = $item['name']; // The product name
$item_qty = $item['quantity']; // The quantity
// Get line item totals (non discounted)
$line_total = $item['subtotal']; // or $item['line_subtotal'] -- The line item non discounted total
$line_total_tax = $item['subtotal_tax']; // or $item['line_subtotal_tax'] -- The line item non discounted tax total
// Get line item totals (discounted)
$line_total2 = $item['total']; // or $item['line_total'] -- The line item non discounted total
$line_total_tax2 = $item['total_tax']; // The line item non discounted tax total
// And so on ……
}
As reference:
- Get the metadata of an order item in woocommerce 3
- How to get WooCommerce order details
Related Topics
How to Create Codeigniter Batch Insert Array
How to Count Days Between Two Dates in PHP
Create a Dynamic MySQL Query Using PHP Variables
Preg_Match(); - Unknown Modifier '+'
Setting Value of a HTML Form Textarea
Print Time in 15-Minute Increments Between Two Times in the Same Day
Retrieve All Hashtags from a Tweet in a PHP Function
Regex Pattern to Get the Youtube Video Id from Any Youtube Url
Get Youtube Video Id from HTML Code with PHP
The Advantage/Disadvantage Between Global Variables and Function Parameters in PHP
What Causes: "Notice: Uninitialized String Offset" to Appear
Manipulate an Archive in Memory with PHP (Without Creating a Temporary File on Disk)
Replacing MySQL_* Functions with Pdo and Prepared Statements
What Does a Successful MySQL Delete Return? How to Check If Delete Was Successful