I'm struggling with Woocommerce data structure regarding order information for a specific time range, especially refunds.
My goal:
I need to sum up order information for a specific time range based on grouped products, payment methods for these groups and refunds for these groups.
This works fine, if there would be no refund, because I cannot find out detailed refund information for an order (item).
Following order example:
Order date: 28.04.2022
- 1x Drink A 1€
- 4x Drink B 2€
- 1x Meal A 10€
Part of the order gets refunded on 01.05.2022
- 1x Drink A -> -1x refunded
- 4x Drink B -> -2x refunded
- 1x Meal A
Now I need to find out the exact amount for each product which has be refunded. And in the end I need to calculate the value for these, in the example: 1x1€ and 2x2€.
So what I have tried so far was
- Get the detailed information with order refunds object -> at some point there is no more detailed information
- Get the detailed information when looping through the order / items and then get refunded information there
Steps 1:
Accessing information with refund objects
$order_refunds = $order->get_refunds();
if (count($order_refunds) > 0) {
//Successfully has value when there was a refund in the order
echo 'refunds found'; //this will be shown
foreach( $order_refunds as $ref ){
echo $ref; //this will be shown as well
$refund = new WC_Order_Refund( $ref->get_id() );
$refunded_items = $refund->get_items();
foreach( $refund_items as $item ){
echo $item . '<br>'; //this will NOT be shown, although there should be something. So no information about qty or refunded amount
}
}
}
Steps 2: Accessing refunds within order item objects
$order = wc_get_order( $orderId);
foreach($order->get_items() as $item_id => $orderItem ){
$orderItemname = $orderItem->get_name();
$orderItemProductId = $orderItem->get_product_id();
$orderItemData = $orderItem->get_data(); // The Order data
$item_qty_refunded = $order->get_qty_refunded_for_item( $item_id ); // Get the refunded amount for a line item.
//$item_total_refunded = $order->get_total_refunded_for_item( $orderItemId ); // Get the refunded amount for a line item.
echo 'refunded:' . $item_qty_refunded . '<br>'; // this is always 0
}
I found these ways here https://wpdavies.dev/how-to-get-woocommerce-order-details-beginners-guide/ but I cannot figure out what I'm doing wrong here.
thank you very much