Skip to content

Instantly share code, notes, and snippets.

@Spreeuw
Last active February 20, 2021 18:29
Show Gist options
  • Save Spreeuw/102930ad74437d03dd872d4075b37a44 to your computer and use it in GitHub Desktop.
Save Spreeuw/102930ad74437d03dd872d4075b37a44 to your computer and use it in GitHub Desktop.
WooCommerce view/filter all refunded orders (including partially refunded)
<?php
add_filter( 'views_edit-shop_order', 'wpo_wc_show_all_refunded_orders', 10, 1 );
function wpo_wc_show_all_refunded_orders ($views) {
$title = __('All refunded');
$views['all-refunded'] = sprintf( '<a href="edit.php?post_status=wc-refunded&post_type=shop_order&all-refunded=true">%s</a>', $title );
return $views;
}
add_filter( 'request', 'wpo_wc_show_all_refunded_orders_request' );
function wpo_wc_show_all_refunded_orders_request( $query_vars ) {
if ( is_admin() && !empty( $_GET[ 'all-refunded' ] ) && ! empty( $query_vars['post_type'] ) && $query_vars['post_type'] == 'shop_order' ) {
$refund_ids = wpo_get_wc_orders_with_refunds();
$query_vars = array_merge( $query_vars, array(
'post_status' => ['wc-completed','wc-refunded'],
'post__in' => $refund_ids,
) );
}
return $query_vars;
}
function wpo_get_wc_orders_with_refunds() {
$query_args = array(
'fields' => 'id=>parent',
'post_type' => 'shop_order_refund',
'post_status' => 'any',
'posts_per_page' => -1,
);
$refunds = get_posts( $query_args );
return array_values( array_unique( $refunds ) );
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment