Created
June 21, 2023 07:47
-
-
Save hamidrezayazdani/fb85b842a4dcea914143324601653a61 to your computer and use it in GitHub Desktop.
Filter WooCommerce orders by payment methods in admin panel
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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