Halloween Sale 15% off Ends in See the plans
PHP

Filter Orders by Payment Method

Adds a "Payment method" dropdown above the admin orders list, so you can list only bank transfer, PayPal or cash on delivery orders.

Don’t have WPIDE yet? Install it on your site

Code

$wpide_pm_filter_dropdown = function () {
    if ( ! function_exists( 'WC' ) ) {
        return;
    }

    $current = isset( $_GET['wpide_payment'] ) ? sanitize_key( wp_unslash( $_GET['wpide_payment'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification

    echo '<label for="wpide-payment-filter" class="screen-reader-text">' . esc_html( 'Filter by payment method' ) . '</label>';
    echo '<select name="wpide_payment" id="wpide-payment-filter"><option value="">' . esc_html( 'All payment methods' ) . '</option>';

    foreach ( WC()->payment_gateways()->payment_gateways() as $id => $gateway ) {
        if ( 'yes' === $gateway->enabled ) {
            printf( '<option value="%s" %s>%s</option>', esc_attr( $id ), selected( $current, $id, false ), esc_html( $gateway->get_title() ) );
        }
    }

    echo '</select>';
};

add_action( 'woocommerce_order_list_table_restrict_manage_orders', function ( $order_type = 'shop_order' ) use ( $wpide_pm_filter_dropdown ) {
    if ( 'shop_order' === $order_type ) {
        $wpide_pm_filter_dropdown();
    }
} );

add_action( 'restrict_manage_posts', function ( $post_type ) use ( $wpide_pm_filter_dropdown ) {
    if ( 'shop_order' === $post_type ) {
        $wpide_pm_filter_dropdown();
    }
} );

add_filter( 'woocommerce_order_list_table_prepare_items_query_args', function ( $args ) {
    if ( ! empty( $_GET['wpide_payment'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
        $args['payment_method'] = sanitize_key( wp_unslash( $_GET['wpide_payment'] ) ); // phpcs:ignore WordPress.Security.NonceVerification
    }

    return $args;
} );

add_action( 'pre_get_posts', function ( $query ) {
    if ( ! is_admin() || ! $query->is_main_query() || 'shop_order' !== $query->get( 'post_type' ) || empty( $_GET['wpide_payment'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification
        return;
    }

    $meta_query   = (array) $query->get( 'meta_query' );
    $meta_query[] = array( 'key' => '_payment_method', 'value' => sanitize_key( wp_unslash( $_GET['wpide_payment'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification
    $query->set( 'meta_query', $meta_query );
} );

Notes

Lists the payment methods enabled in your store and works on both the HPOS and legacy orders screens.

How to add this snippet

  1. Install WPIDE on your WordPress site (it is free) and open Code Snippets.
  2. Use snippet above adds it from the library in one click. Or copy the code into a new snippet with the same code type.
  3. It is added inactive: review the code and settings, then switch it on.

Have a snippet worth sharing?

Publish it to the library from WPIDE or right here. Sign in with any WordPress site that runs WPIDE, free or Pro. Public snippets are reviewed by the WPIDE team before they appear.

Which site should get this snippet?

Enter the address of your WordPress site. WPIDE opens there in a new tab with the snippet ready to review and save.

Remembered in this browser only, so you are asked once.

Report this snippet

Tell the WPIDE team what is wrong: broken code, something harmful, spam or copied work.