Reputation: 544
I'm building a custom function who list all my orders filtered by shipping method.
In the first time I already build my loop as below :
<?php
global $woocommerce;
$filters_orders = array(
'post_status' => 'processing',
'post_type' => 'shop_order',
'posts_per_page' => 10,
'paged' => $paged,
'orderby' => 'modified',
'order' => 'ASC'
);
$temp = $loop;
$loop= null;
$loop = new WP_Query($filters_orders);
while ($loop->have_posts()) {
$loop->the_post();
$order = new WC_Order($loop->post->ID);
$items = $order->get_items();
}
?>
Second time, I need a content (dropdown for my needs) who lists all methods taken by the orders I build this :
public function display_shipping_dropdown(){
global $typenow;
$shipping_methods = WC()->shipping->get_shipping_methods();
// print_r($shipping_methods);
foreach ($shipping_methods as $shipping_method){
echo $shipping_method->get_method_title();
}
}
But the function doesn't do what I want.
I want to retrieve all the method title, id and price.
So, when I will select for example "Basic Shipping", the table will load all the orders with this shipping.
I can build the function for the action. I'm just stuck to retrieve all method.
Thanks
Upvotes: 1
Views: 3993
Reputation: 253814
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
}
}
Upvotes: 1