Require a Reason Before Issuing a Refund
Stops staff from issuing a refund in the order screen until they have typed a reason, so every refund in your records is explained.
Opens WPIDE on
Code
add_action( 'admin_footer', function () {
$screen = get_current_screen();
if ( ! $screen || ! function_exists( 'wc_get_page_screen_id' ) || wc_get_page_screen_id( 'shop-order' ) !== $screen->id ) {
return;
}
?>
<script>
document.addEventListener( 'click', function ( event ) {
var button = event.target.closest( '.do-api-refund, .do-manual-refund' );
var reason = document.getElementById( 'refund_reason' );
if ( ! button || ! reason || reason.value.trim() !== '' ) { return; }
event.preventDefault();
event.stopImmediatePropagation();
window.alert( <?php echo wp_json_encode( 'Please enter a reason for this refund first.' ); ?> );
reason.focus();
}, true );
</script>
<?php
} );
Notes
Works with both manual and gateway (API) refunds on HPOS and legacy order screens.
How to add this snippet
- Install WPIDE on your WordPress site (it is free) and open Code Snippets.
- Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
- It is added inactive: review the code and settings, then switch it on.