如何从现有的 foreach 循环中获取订单费用并将其用作 WooCommerce 中的参数
How do I get order fee from existing foreach loop and use it as argument in WooCommerce
由于我已经可以访问订单 ID 和订单列表,我如何才能访问每个订单的每个订单费用?
我有以下内容,但是我在哪里以及如何将它插入我现有的代码中,从而允许我将它添加为 $orderList
索引?
foreach( $orderID->get_items('fee') as $item_id => $item_fee ){
// fee name
$fee_name = $item_fee->get_name();
// fee total amount
$fee_total = $item_fee->get_total();
// fee total tax amount
$fee_total_tax = $item_fee->get_total_tax();
}
我需要能够将上面的代码与下面的代码一起使用。
function GetOrderList() {
$query = new WC_Order_Query( array(
'limit' => 100,
'orderby' => 'date',
'order' => 'DESC',
'return' => 'ids',
) );
$orders = $query->get_orders();
$orderList = array();
$index = 0;
foreach ( $orders as $orderID ) {
$order = new WC_Order($orderID);
$order_data = $order->get_data();
$orderList[$index]['order_id'] = $orderID;
$orderList[$index]['name'] = $order->get_billing_first_name().' '.$order->get_billing_last_name();
$orderList[$index]['email'] = $order->get_billing_email();
$orderList[$index]['phone'] = $order->get_billing_phone();
$orderList[$index]['note'] = $order->get_customer_note();
$orderList[$index]['order_status'] = $order->get_status();
$orderList[$index]['total_price'] = $order->get_total();
// how do I get this to work?
$orderList[$index]['fee'] = $order->$fee_total;
$index++;
}
return $orderList;
}
我稍微修改了你的代码。
要将费用数据添加到从您的自定义函数返回的数组中,您可以使用:
function get_order_list() {
// Get 100 most recent order objects in date descending order.
$orders = wc_get_orders( array(
'limit' => 100,
'orderby' => 'date',
'order' => 'DESC',
'return' => 'objects',
));
$order_list = array();
$index = 0;
// NOT empty
if ( sizeof( $orders ) > 0 ) {
// Iterating through each order
foreach ( $orders as $order ) {
// Get order details
$order_list[$index]['order_id'] = $order->get_id();
$order_list[$index]['name'] = $order->get_billing_first_name() . ' ' . $order->get_billing_last_name();
$order_list[$index]['email'] = $order->get_billing_email();
$order_list[$index]['phone'] = $order->get_billing_phone();
$order_list[$index]['note'] = $order->get_customer_note();
$order_list[$index]['order_status'] = $order->get_status();
$order_list[$index]['total_price'] = $order->get_total();
$indexx = 0;
// Iterating through order fee items
foreach( $order->get_items('fee') as $item_id => $item_fee ) {
// Fee name
$order_list[$index][$indexx]['item_fee_name'] = $item_fee->get_name();
// Fee total amount
$order_list[$index][$indexx]['item_fee_amount'] = $item_fee->get_total();
// Fee total tax amount
$order_list[$index][$indexx]['item_fee_total_tax'] = $item_fee->get_total_tax();
$indexx++;
}
$index++;
}
}
return $order_list;
}
结果:
$result = get_order_list();
echo '<pre>', print_r( $result, 1 ), '</pre>';
由于我已经可以访问订单 ID 和订单列表,我如何才能访问每个订单的每个订单费用?
我有以下内容,但是我在哪里以及如何将它插入我现有的代码中,从而允许我将它添加为 $orderList
索引?
foreach( $orderID->get_items('fee') as $item_id => $item_fee ){
// fee name
$fee_name = $item_fee->get_name();
// fee total amount
$fee_total = $item_fee->get_total();
// fee total tax amount
$fee_total_tax = $item_fee->get_total_tax();
}
我需要能够将上面的代码与下面的代码一起使用。
function GetOrderList() {
$query = new WC_Order_Query( array(
'limit' => 100,
'orderby' => 'date',
'order' => 'DESC',
'return' => 'ids',
) );
$orders = $query->get_orders();
$orderList = array();
$index = 0;
foreach ( $orders as $orderID ) {
$order = new WC_Order($orderID);
$order_data = $order->get_data();
$orderList[$index]['order_id'] = $orderID;
$orderList[$index]['name'] = $order->get_billing_first_name().' '.$order->get_billing_last_name();
$orderList[$index]['email'] = $order->get_billing_email();
$orderList[$index]['phone'] = $order->get_billing_phone();
$orderList[$index]['note'] = $order->get_customer_note();
$orderList[$index]['order_status'] = $order->get_status();
$orderList[$index]['total_price'] = $order->get_total();
// how do I get this to work?
$orderList[$index]['fee'] = $order->$fee_total;
$index++;
}
return $orderList;
}
我稍微修改了你的代码。
要将费用数据添加到从您的自定义函数返回的数组中,您可以使用:
function get_order_list() {
// Get 100 most recent order objects in date descending order.
$orders = wc_get_orders( array(
'limit' => 100,
'orderby' => 'date',
'order' => 'DESC',
'return' => 'objects',
));
$order_list = array();
$index = 0;
// NOT empty
if ( sizeof( $orders ) > 0 ) {
// Iterating through each order
foreach ( $orders as $order ) {
// Get order details
$order_list[$index]['order_id'] = $order->get_id();
$order_list[$index]['name'] = $order->get_billing_first_name() . ' ' . $order->get_billing_last_name();
$order_list[$index]['email'] = $order->get_billing_email();
$order_list[$index]['phone'] = $order->get_billing_phone();
$order_list[$index]['note'] = $order->get_customer_note();
$order_list[$index]['order_status'] = $order->get_status();
$order_list[$index]['total_price'] = $order->get_total();
$indexx = 0;
// Iterating through order fee items
foreach( $order->get_items('fee') as $item_id => $item_fee ) {
// Fee name
$order_list[$index][$indexx]['item_fee_name'] = $item_fee->get_name();
// Fee total amount
$order_list[$index][$indexx]['item_fee_amount'] = $item_fee->get_total();
// Fee total tax amount
$order_list[$index][$indexx]['item_fee_total_tax'] = $item_fee->get_total_tax();
$indexx++;
}
$index++;
}
}
return $order_list;
}
结果:
$result = get_order_list();
echo '<pre>', print_r( $result, 1 ), '</pre>';