限制订单类型以避免在WooCommerce中完成订单时出错

我想显示基于订单状态的报告(如果已完成),然后显示一些数据


但是它适用于所有订单状态,例如,如果我将其保留,它将获取数据,但是当我完成时,会出现此错误


致命错误:调用未定义的方法WC_Order_Refund :: get_order_number()


这是我的代码


    //Get Report for Orders that have status of delivered

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>';

    }

}

如果我将“已完成”更改为“保留”或任何其他状态,它将起作用并获取数据


但是如何使它与已完成的订单一起使用?


慕姐4208626
浏览 289回答 2
2回答

拉丁的传说

该问题与应为“商店订单”的订单类型有关。有两种方法:1)检查订单类型,该类型必须为“ shop_order” (而不是“ shop_order_refund”)。因此,您可以使用方法get_type(),该方法也适用于WC_Order_Refund类似以下类的类:foreach( $orders as $order ){&nbsp; &nbsp; if ( $order->get_type() === 'shop_order' && $order->get_status() === 'completed'){&nbsp; &nbsp; &nbsp; &nbsp; $order_data = $order->get_data(); // The Order data&nbsp; &nbsp; &nbsp; &nbsp; $items = $order->get_items();&nbsp; &nbsp; &nbsp; &nbsp; foreach ( $items as $item ) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $product_name = $item->get_name();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $product_id = $item->get_product_id();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; $orders_completed .=&nbsp; '<tr><td>' . $order->get_order_number() . '</td>' .&nbsp; &nbsp; &nbsp; &nbsp; '<td>' . $order->get_date_created()->date('Y-m-d H:i:s') . '</td>' .&nbsp; &nbsp; &nbsp; &nbsp; '<td>' . $order->get_status() . '</td>' .&nbsp; &nbsp; &nbsp; &nbsp; '<td>' . $order->get_total() . '</td>' .&nbsp; &nbsp; &nbsp; &nbsp; '<td>' . $product_id . '</td>' .&nbsp; &nbsp; &nbsp; &nbsp; '<td>' . $product_name . '</td>' .&nbsp; &nbsp; &nbsp; &nbsp; '<td>' . $order->get_item_count() . '</td>' .&nbsp; &nbsp; &nbsp; &nbsp; '<td>' . $order->get_billing_first_name() . '</td>' .&nbsp; &nbsp; &nbsp; &nbsp; '<td>' . $order->get_billing_email() . '</td>' .&nbsp; &nbsp; &nbsp; &nbsp; '<td>' . $order->get_billing_phone() . '</td>' .&nbsp; &nbsp; &nbsp; &nbsp; '<td>' . $order_payment_method = $order_data['payment_method_title'] . '</td></tr>';&nbsp; &nbsp; }};2)更改WC_Order_Query,仅定位“ shop_order”帖子类型:$orders = wc_get_orders( array('limit' => -1, 'type' => 'shop_order') );两种方式都将避免得到此致命错误

慕后森

要获取完整的订单清单,您可以使用此功能if ($order->payment_complete()){}
打开App,查看更多内容
随时随地看视频慕课网APP