Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save hamidrezayazdani/fb85b842a4dcea914143324601653a61 to your computer and use it in GitHub Desktop.
Save hamidrezayazdani/fb85b842a4dcea914143324601653a61 to your computer and use it in GitHub Desktop.
Filter WooCommerce orders by payment methods in admin panel
<?php
/**
* Create a list of all available payment methods, even inactive ones
*
* @return void
*/
function ywp_filter_orders_by_payment_method() {
global $typenow;
if ( 'shop_order' !== $typenow || ! current_user_can( 'edit_shop_orders' ) ) {
return;
}
$gateways = WC()->payment_gateways->payment_gateways();
$gateways_list = sprintf(
'<select name="ywp-payment-method"><option value="">%s</option>',
esc_html__( 'All Payment Methods' ),
);
foreach ( $gateways as $id => $gateway ) {
$gateways_list .= sprintf(
'<option value="%s" %s>%s</option>',
esc_attr( $id ),
! empty( $_GET['ywp-payment-method'] ) ? selected( $id, $_GET['ywp-payment-method'], false ) : '',
esc_html( $gateway->get_method_title() ),
);
}
echo $gateways_list . '</select>';
}
add_action( 'restrict_manage_posts', 'ywp_filter_orders_by_payment_method', 20 );
/**
* Change request query to do filter orders by selected payment method
*
* @param $query
*
* @return mixed
*/
function ywp_filter_orders_by_payment_method_query( $query ) {
global $typenow;
if ( 'shop_order' !== $typenow || ! current_user_can( 'edit_shop_orders' ) || empty( $_GET['ywp-payment-method'] ) ) {
return $query;
}
$query['meta_key'] = '_payment_method';
$query['meta_value'] = wc_clean( $_GET['ywp-payment-method'] );
return $query;
}
add_filter( 'request', 'ywp_filter_orders_by_payment_method_query' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment