WooCommerce custom report: Get orders with completed status

梦想的初衷 提交于 2021-02-17 02:50:08

问题


im making a custom reports for woocommerce im trying to add a report for all delivered orders here is what im doing

        $orders = wc_get_orders( array('numberposts' => -1) );
    foreach( $orders as $order ){
    if ( $order->get_status() === completed){
        $order_data = $order->get_data(); // The Order data
        $items = $order->get_items();
        foreach ( $items as $item ) {
            $product_name = $item->get_name();
            $product_id = $item->get_product_id();
        }
        $orders_completed .=  '<tr><td>' . $order->get_order_number() . '</td>' .
        '<td>' . $order->get_date_created()->date('Y-m-d H:i:s') . '</td>' .
        '<td>' . $order->get_status() . '</td>' .
        '<td>' . $order->get_total() . '</td>' .
        '<td>' . $product_id . '</td>' .
        '<td>' . $product_name . '</td>' .
        '<td>' . $order->get_item_count() . '</td>' .
        '<td>' . $order->get_billing_first_name() . '</td>' .
        '<td>' . $order->get_billing_email() . '</td>' .
        '<td>' . $order->get_billing_phone() . '</td>' .
        '<td>' . $order_payment_method = $order_data['payment_method_title'] . '</td></tr>';
    }
}

i get

Call to undefined method WC_Admin_Order_Refund::get_order_number()

i don't know what im doing wrong


回答1:


You need to target only "shop_order" post type, without "shop_order_refund" post type in your WC_Order_Query, because some WC_Order methods doesn't exist for WC_Order_Refund.

So you can replace instead the first line of your code by:

$orders = wc_get_orders( array('limit' => -1, 'type' => 'shop_order') );

This should solve this issue.

See the official documentation about wc_get_orders and WC_Order_Query



来源:https://stackoverflow.com/questions/57134533/woocommerce-custom-report-get-orders-with-completed-status

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!