WooCommerce在foreach中获取所有订单

时间:2019-04-07 17:25:06

标签: php wordpress woocommerce

您好,我有此功能,它仅显示1个订单详细信息

    add_filter( 'woocommerce_admin_reports', 'add_report_tab' );

function add_report_tab( $reports ) {
    $orders = wc_get_orders( array('numberposts' => -1) );

    // Loop through each WC_Order object
    $items=array();

    foreach( $orders as $order ){
        if ( sizeof($order->get_used_coupons()) > 0 ) {
        $order_data = $order->get_data(); // The Order data
        $data = 'Order Number: #' . $order->id . '<br>' .'Order Status: '. $order->status . '<br>' . 'Order Creation Date: ' . $order_date_created = $order_data['date_created']->date('Y-m-d H:i:s') . '<br>' . 'Order Total: '. $order->total . '<br>' . 'Customer Username: ' . $order_billing_first_name = $order_data['billing']['first_name'] . '<br>' . 'Customer E-Mail: '. $order_billing_email = $order_data['billing']['email'] . '<br>' . 'Customer Phone: ' . $order_billing_phone = $order_data['billing']['phone'] . '<br>'; 
        }

    }
    //return array($items);

$reports['reports'] = array(
                'title'  => __( 'Discounted Orders Reports', 'woocommerce' ),
                'reports' => array(
                    "sales_by_code" => array(
                        'title'       => __( 'Sales by Discount', 'woocommerce' ),
                        'description' => $data, 
                        'hide_title'  => false,
                        'callback'    => '',
                    ),
                ),
            );

return $reports;
}  

此代码显示enter image description here

当我回显它显示大约7个订单详细信息时,如何使其显示所有订单而不是仅显示一个订单

编辑1:'description' => $data, 标签下显示数据的是什么

1 个答案:

答案 0 :(得分:1)

在我看来,您在每次循环迭代中都覆盖了$ data。

在foreach循环之前使用$ data =“”初始化数据,然后使用“。=”或$ data = $ data +“您的代码”

$data .= 'Order Number: #' . $order->id . '<br>' .'Order Status: '. $order->status . '<br>' . 'Order Creation Date: ' . $order_date_created = $order_data['date_created']->date('Y-m-d H:i:s') . '<br>' . 'Order Total: '. $order->total . '<br>' . 'Customer Username: ' . $order_billing_first_name = $order_data['billing']['first_name'] . '<br>' . 'Customer E-Mail: '. $order_billing_email = $order_data['billing']['email'] . '<br>' . 'Customer Phone: ' . $order_billing_phone = $order_data['billing']['phone'] . '<br>'; 

代替

$data = 'Order Number: #' . $order->id . '<br>' .'Order Status: '. $order->status . '<br>' . 'Order Creation Date: ' . $order_date_created = $order_data['date_created']->date('Y-m-d H:i:s') . '<br>' . 'Order Total: '. $order->total . '<br>' . 'Customer Username: ' . $order_billing_first_name = $order_data['billing']['first_name'] . '<br>' . 'Customer E-Mail: '. $order_billing_email = $order_data['billing']['email'] . '<br>' . 'Customer Phone: ' . $order_billing_phone = $order_data['billing']['phone'] . '<br>';