我正在构建一个自定义函数,该函数列出了按运输方式过滤的所有订单。
在第一次我已经构建了我的循环,如下所示:
<?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();
}
?>
第二次,我需要一个内容(满足我需求的下拉列表(,其中列出了我构建的订单所采用的所有方法:
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();
}
}
但是该函数没有做我想要的。
我想检索所有方法标题,ID和价格。
因此,当我选择例如"基本运输"时,该表将加载与此运输的所有订单。
我可以为动作构建函数。我只是坚持检索所有方法。
谢谢
更新 (基于您的编辑(
要使用现有运输方式的选择字段按运输方式获取订单 ID,更轻松、更准确的方法应该是首先使用 SQL 查询......
下面是注释的代码示例(您可以更改并嵌入到函数中:
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>' ;
}
这将允许您为每个选定的运输方式显示使用此运输方式的相应订单编号...在此示例中,我以逗号分隔的字符串输出订单 ID。
但是您可以使用订单 ID 数组来使其他内容更方便,例如将它们嵌入分页WP_Query中,以避免在有许多订单时向服务器过度收费......
初始答案:
要列出订单中使用的所有运输方式,您最好使用此代码:
// 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
}
}