9

I see when I look at an order it will show the specific item that was refunded if the whole order wasn't.

Is there a way to find using the WC_Order_Item_Product if an item was refunded? Also, is there a way to get the discount amount that shows up below the item in the order view?

I'm currently doing it manually but if there is a function for it already, I would rather use it.

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
twg_
  • 995
  • 2
  • 7
  • 15
  • Updated the answer with that important detail: you can use `$item->get_meta('_refunded_item_id');` to get the original item ID that has been refunded – LoicTheAztec Sep 26 '18 at 07:01

2 Answers2

19

To get refunded orders you could use some dedicated WC_Order methods like the following ones that have Item Id as argument:

$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( $item_id ); // Get the refunded amount for a line item.

You can access an array WC_Order_Refund Objects for this order using get_refunds() method:

So you can use the following example code:

// Get the WC_Order Object instance (from the order ID)
$order = wc_get_order( $order_id );

// Get the Order refunds (array of refunds)
$order_refunds = $order->get_refunds();

// Loop through the order refunds array
foreach( $order_refunds as $refund ){
    // Loop through the order refund line items
    foreach( $refund->get_items() as $item_id => $item ){

        ## --- Using WC_Order_Item_Product methods --- ##

        $refunded_quantity      = $item->get_quantity(); // Quantity: zero or negative integer
        $refunded_line_subtotal = $item->get_subtotal(); // line subtotal: zero or negative number
        // ... And so on ...

        // Get the original refunded item ID
        $refunded_item_id       = $item->get_meta('_refunded_item_id'); // line subtotal: zero or negative number
    }
}

To get the order items discounted values that appear in admin order edit pages you will use the following code:

// Get the WC_Order Object instance (from the order ID)
$order = wc_get_order($order_id);

// Loop through the order refund line items
foreach( $order->get_items() as $item_id => $item ){
    $line_subtotal     = $item->get_subtotal();
    $line_total        = $item->get_total();
    $line_subtotal_tax = $item->get_subtotal_tax();
    $line_total_tax    = $item->get_total_tax();
    
    // Get the negative discount values
    $line_discount     = $line_total - $line_subtotal; // (Negative number)
    $line_discount_tax = $line_total_tax - $line_subtotal_tax; // (Negative number)
}

Related Answers:

LoicTheAztec
  • 229,944
  • 23
  • 356
  • 399
  • How to get refunded tax amount for line item ? – moneeb Feb 09 '22 at 16:31
  • what could be the issue, when these ways do not work? I get a refund but the refund itself has no items? and the other way mentioned with $order->get_qty_refunded_for_item( $item_id ); always returns 0 for order items which are refunded??? – Vario Apr 28 '22 at 08:48
0

If you are using get_qty_refunded_for_item( $item_id ) or get_total_refunded_for_item( $item_id ) returns 0 use absint().

$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( $item_id ); // Get the refunded amount for a line item.