Woocommerce通过送货方式下拉过滤器获取所有订单

时间:2017-11-19 20:09:27

标签: php wordpress woocommerce shipping orders

我正在构建一个自定义函数,列出按发货方式过滤的所有订单。

我第一次构建循环如下:

<?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和价格。

因此,当我选择“基本运输”时,该表格将加载此运费的所有订单。

我可以为动作构建功能。我只是坚持检索所有方法。

由于

1 个答案:

答案 0 :(得分:1)

更新 (基于您的修改)

要通过装运方法获取订单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>' ;
}

这将允许您显示每个选定的装运方法,使用此装运方法的相应订单IDS ...在此示例中,我以逗号分隔的字符串输出订单ID。

但是您可以使用Orders 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
    }
}