#50577·woocommerce

[Enhancement]: Adding fees in refunds

Author: nekemUCreated Aug 11, 2024Updated Sep 29, 2026
LabelsEnhancementneeds: designOrder Managementtype: community contributionRubikneeds: engineering

Describe the solution you'd like

It is quite common these days that when an item is returned and refunded, some additional fees may be deducted to the refunded amount. For example, if shipping was free but the item is returned, then the shipping fee is deducted from the refund. Thus, in the admin view, Edit Item, when "Refund" button is clicked, it would be good to have some fields to add fees and their reasons. These fees would be deducted from the amount to be refunded. Having this information is also crucial when a refund email is sent to the customer, so it is well specified the fee amount and reason for it.

Describe alternatives you've considered

I have considering doing something like:

add_action( 'admin_enqueue_scripts', 'enqueue_custom_refund_script' );

function enqueue_custom_refund_script() { // Get the current screen object $screen = get_current_screen();

// Check if we're on the WooCommerce order edit page
if ( $screen && 'woocommerce_page_wc-orders' === $screen->base && 'shop_order' === $screen->post_type ) {
    error_log('We are on the WooCommerce order edit page.');
    wp_enqueue_script( 'custom-refund-fields', get_stylesheet_directory_uri() . '/js/custom-refund-fields.js', array( 'jquery' ), '1.0', true );
    error_log('Refund script triggered.');
}

}

The jquery would be something like it shows below. The idea is that when the refund button is clicked, it shows a few new fields like fee 1: (amount to enter), reason fee 1: (text), fee 2: (amount to enter), reason fee 2: (text_")

jQuery(document).ready(function($) { // Monitor when the Refund button is clicked $(document).on('click', '.refund-items', function() { ...

Then, there would be another function like:

//Handling the Custom Fields on Refund Creation add_action( 'woocommerce_create_refund', 'process_custom_refund_fields', 10, 2 );

function process_custom_refund_fields( $refund, $args ) { // Retrieve custom fields from the $_POST data $refund_fee_reason_1 = isset( $_POST['refund_fee_reason_1'] ) ? sanitize_text_field( $_POST['refund_fee_reason_1'] ) : ''; $refund_fee_1 = isset( $_POST['refund_fee_1'] ) ? floatval( $_POST['refund_fee_1'] ) : 0;

$refund_fee_reason_2 = isset( $_POST['refund_fee_reason_2'] ) ? sanitize_text_field( $_POST['refund_fee_reason_2'] ) : '';
$refund_fee_2 = isset( $_POST['refund_fee_2'] ) ? floatval( $_POST['refund_fee_2'] ) : 0;

// Adjust the refund amount
$total_deductions = 0;

error_log( 'Processing refund custom fields.' );

if ( $refund_fee_1 > 0 ) {
    $total_deductions += $refund_fee_1;
    $refund->add_meta_data( 'refund_fee_1', $refund_fee_1, true );
    $refund->add_meta_data( 'refund_fee_reason_1', $refund_fee_reason_1, true );
}

if ( $refund_fee_2 > 0 ) {
    $total_deductions += $refund_fee_2;
    $refund->add_meta_data( 'refund_fee_2', $refund_fee_2, true );
    $refund->add_meta_data( 'refund_fee_reason_2', $refund_fee_reason_2, true );
}

if ( $total_deductions > 0 ) {
    $new_total = $refund->get_amount() - $total_deductions;
    $refund->set_amount( $new_total );
    
    error_log( 'Refund amount adjusted. New total: ' . $new_total );
}

// No need to manually save, WooCommerce will save the refund after this hook

}

In these lines - However, the jquery did not get executed and I could not really find the reason, so I do not know if the rest works either. In general, I did not feel there are many/good hooks to easily change the refund function.

Additional context

No response