How do I get order fee from existing foreach loop and use it as argument in WooCommerce

316 Views Asked by At

Since I already have access to the order ID and the list of orders, how do I get access to each order fee from each each order?

I have the following, but where and how do I insert that inside my existing code, allowing me to add it as an $orderList index?

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();
}

I need to be able to use the above with the code below.

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;
}
1

There are 1 best solutions below

0
On BEST ANSWER

I modified your code a bit.

To add the fee data to the array, returned from your custom function, you can use:

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:

$result = get_order_list();

echo '<pre>', print_r( $result, 1 ), '</pre>';