How can I get the order shipping method id.?
For example 'flate_rate'.
Since WooCommerce 3 it is now complicated as everything has changed.
I have tried it with $order->get_data()
in a foreach loop but the data is protected.
There under the customers name you will see a “Users Insights Profile →” link. This links to the WooCommerce customer profile page of the customer who have purchased the order and from there you can get all the customer details that you might need.
Configure WooCommerce settings Set up shipping zones to define different rates based on customers' locations. To assign shipping methods to zones, click Add Shipping Method. Choose Flat Rate, Free Shipping, or Local Pickup, and select the options and pricing you'd like for each one.
You'll need to use the get_formatted_order_total() method or the get_total() method.
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>';
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With