Get Refunded Orders and Refunded Order Items Details in Woocommerce 3

Get refunded orders and refunded order items details in Woocommerce 3

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:

  • For each refund, you can use the WC_Order_Refund methods.
  • For each refund, you can access items using WC_Abstract_Order method get_items() that will give you the refunded items for the current Order refund.
  • Each refund "line" item is a WC_Order_Item_Product (with negative values in general) see this related answer: Get Order items and WC_Order_Item_Product in WooCommerce 3

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:

  • How to get WooCommerce order details
  • Get Order items and WC_Order_Item_Product in WooCommerce 3
  • Woocommerce - Getting the order item price and quantity.

Check if an order contains any refund in Woocommerce 3

You can use WC_Order the method get_refunds() like:

if( sizeof( $order->get_refunds() ) > 0 ) {
printf( 'Order id %s has some refund', $order->get_id() );
}

Or as a custom conditional function:

function has_refunds( $order ) {
return sizeof( $order->get_refunds() ) > 0 ? true : false;
}

Usage:

if( has_refunds( $order ) ) {
// Do something
}

Related answer thread: Get refunded orders and refunded order items details in Woocommerce 3

Get the refund reason in Woocommerce customer refunded order notification

Updated

First you don't need __( '%s', 'woocommerce' ) as there is nothing to translate, so you don't need printf() function either.

Since WooCommerce 3, the WC_Order_Refund method get_refund_reason() is deprecated and replaced by get_reason() method.

It doesn't work because you need to get the refunded orders (they can be many for an order) from the WC_Order Object, using the method get_refunds()

Try the following instead:

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

// Loop through refunded orders for the current WC_Order object
foreach( $order_refunds as $order_refund ){
// To be sure we check if that method exist and that is not empty
if( method_exists( $order_refund, 'get_reason' ) && $order_refund->get_reason() ) {
echo '<p>' . esc_html( $order_refund->get_reason() ) . '</p>';
}
}
?>

It should work.

WooCommerce: Get date when order was refunded

Indeed there is a way. $refunds is an array as there can be many refunds.

// Get order object (eg from id)
$order = wc_get_order( $order_id );

// Get all refunds
$refunds = $order->get_refunds();

// Loop over refunds
foreach ($refunds as $refund) {

// Old way:
echo $refund->date;

// New way (see @Yoda’s comment)
echo $refund->get_date_created()->format( 'd/m/Y' );
}

Remove refund row(s) from WooCommerce order details table

When we look at /includes/class-wc-order.php in detail we see the following function is used in WooCommerce for adding the total refunds row(s).

/**
* Add total row for refunds.
*
* @param array $total_rows Total rows.
* @param string $tax_display Tax to display.
*/
protected function add_order_item_totals_refund_rows( &$total_rows, $tax_display ) {
$refunds = $this->get_refunds();
if ( $refunds ) {
foreach ( $refunds as $id => $refund ) {
$total_rows[ 'refund_' . $id ] = array(
'label' => $refund->get_reason() ? $refund->get_reason() : __( 'Refund', 'woocommerce' ) . ':',
'value' => wc_price( '-' . $refund->get_amount(), array( 'currency' => $this->get_currency() ) ),
);
}
}
}

Since an order can consist of several refunds, 'refund_' . $id is used opposite 'refund'


So to remove it, you have to use a loop. So you get:

function filter_woocommerce_get_order_item_totals( $total_rows, $order, $tax_display ) {
// Get the Order refunds (array of refunds)
$order_refunds = $order->get_refunds();

// NOT empty
if ( ! empty( $order_refunds) ) {
// Unset
foreach ( $order_refunds as $id => $refund ) {
unset( $total_rows[ 'refund_' . $id ] );
}
}

return $total_rows;
}
add_filter( 'woocommerce_get_order_item_totals', 'filter_woocommerce_get_order_item_totals', 10, 3 );


Related Topics



Leave a reply



Submit