Black Friday & Cyber Monday SUPER SALE ALL WEEK:
Grab 40% OFF on plugins
Days
Hours
Minutes
Seconds

How to Add a Payment Gateway Filter Field in WooCommerce >Admin Orders Page?

By default, the WooCommerce orders page includes the most basic columns and filter options only. But you can customize the orders page by additional parameters such as Payment method, Order status, Delivery dates, etc. It allows the admin to quickly filter and sort orders easily. This can save time when managing orders and analyzing sales data.

This post helps you to Add a Payment Gateway Filter Field in WooCommerce >Admin Orders Page. 

Where to Add Custom Code in WooCommerce?

It is advisable to add the code snippets to the functions.php file of your child theme. Access the file directly from Appearance->Theme File Editor->Locating the child theme’s functions.php from the right sidebar. You can also access it from your theme’s directory file. Insert the following code snippet in functions.php. The alternative & easy option is to install & activate the Code Snippets plugin. You can then add the code as a new snippet via the plugin.

Solution: Add a Payment Gateway Filter Field in WooCommerce >Admin Orders Page

Imagine an online clothing store offering multiple payment methods, including credit cards, PayPal, and Apple Pay. If the store owner wants to monitor the performance of these payment gateways, manually sorting and organizing orders by payment method can be a time-consuming task.

The following code snippet simplifies this process by adding a dropdown field labeled as ‘Filter by Payment Method’ and a new column as ‘Payment Gateway’ in the admin Orders table, enabling users to filter orders by specific payment gateways.

// Add the Payment Method column to the Orders page in WooCommerce admin
function ts_add_payment_method_column($columns) {
    $columns['payment_method'] = __('Payment Method', 'woocommerce');
    return $columns;
}
add_filter('manage_edit-shop_order_columns', 'ts_add_payment_method_column');

// Display the payment method value in the Payment Method column
function ts_display_payment_method_column($column, $post_id) {
    if ($column == 'payment_method') {
        $order = wc_get_order($post_id);
        echo $order->get_payment_method_title();
    }
}
add_action('manage_shop_order_posts_custom_column', 'ts_display_payment_method_column', 10, 2);

// Add a custom dropdown for filtering by payment method
function ts_add_payment_method_filter() {
    global $typenow;
    
    if ('shop_order' === $typenow) {
        $payment_methods = array(
            'bacs' => __('BACS', 'woocommerce'),
            'cod'  => __('Cash on Delivery', 'woocommerce'),
			'cheque'  => __('Cheque', 'woocommerce'),
                        
            // Add more payment methods as needed
        );
        
        echo '<select name="payment_method_filter">';
        echo '<option value="">Filter by Payment Method</option>';
        
        foreach ($payment_methods as $method => $label) {
            echo '<option value="' . $method . '">' . $label . '</option>';
        }
        
        echo '</select>';
    }
}
add_action('restrict_manage_posts', 'ts_add_payment_method_filter');

// Filter orders based on the selected payment method
function ts_filter_orders_by_payment_method($query) {
    global $pagenow;
    
    if ('edit.php' === $pagenow && isset($_GET['payment_method_filter']) && !empty($_GET['payment_method_filter'])) {
        $query->query_vars['meta_key'] = '_payment_method';
        $query->query_vars['meta_value'] = sanitize_text_field($_GET['payment_method_filter']);
    }
}
add_filter('parse_query', 'ts_filter_orders_by_payment_method');

Output

The image below indicates that the code has added a field named “Filter by Payment Method” that allows you to select a particular payment method via a dropdown filter. A new column titled “Payment Gateway” will now be visible in the admin Orders table. This column will showcase the payment method used for each order based on the user’s selection.

How to Add a Payment Gateway Filter Field in WooCommerce >Admin Orders Page? - Tyche Softwares

The image below shows that when you choose and filter a specific payment method such as ‘Cash on Delivery’, only that method is filtered, and it will be displayed in the payment method column.

How to Add a Payment Gateway Filter Field in WooCommerce >Admin Orders Page? - Tyche Softwares

If you choose and filter a specific payment method such as ‘BACS’, only that method is filtered, and it will be displayed in the payment method column.

How to Add a Payment Gateway Filter Field in WooCommerce >Admin Orders Page? - Tyche Softwares

Code Explanation

Step 1: Adding the Payment Method Column

This step is focused on customizing the display of the Orders page in the WooCommerce admin panel.

  • add_payment_method_column is a custom function that takes an array of column names as its parameter. In this function, it adds a new column named ‘payment_method’ to the existing columns. The text ‘Payment Method’ is the label for this new column.
  • The add_filter function is used to apply this customization. It tells WordPress to execute the add_payment_method_column function when generating the columns for the ‘shop_order’ type (i.e., WooCommerce orders).

Step 2: Displaying the Payment Method Value in the Column

In this step, the code specifies how to display the payment method information in the new ‘Payment Method’ column.

  • The display_payment_method_column function is defined to display the content of the ‘payment_method’ column. It checks if the current column being processed is ‘payment_method’ and if so, it retrieves the order associated with the current post ID using wc_get_order.
  • Then, it outputs the title of the payment method associated with that order using $order->get_payment_method_title().
  • The add_action function is used to execute the display_payment_method_column function when generating custom columns for shop orders. 

Step 3: Adding a Custom Dropdown for Filtering by Payment Method

This part of the code adds a custom dropdown filter to the WooCommerce Orders page, enabling users to filter orders by payment method.

  • The add_payment_method_filter function checks if the current post type is ‘shop_order’ by inspecting the global variable $typenow.
  • If it’s indeed a ‘shop_order’ page, it defines an array called $payment_methods. This array contains various payment methods, each associated with a label. You can add more payment methods as needed.
  • The code then generates HTML for a <select> dropdown with options for each payment method. It also includes a default option for filtering (“Filter by Payment Method”).
  • This function is executed when generating the WordPress admin page and custom post types using the restrict_manage_posts action hook.

Step 4: Filtering Orders Based on the Selected Payment Method

This step focuses on filtering orders based on the selected payment method using the custom dropdown created earlier.

  • The filter_orders_by_payment_method function checks if the current page being displayed is ‘edit.php’ (the edit page for posts) and if the ‘payment_method_filter’ parameter is set in the URL, indicating that a payment method has been selected.
  • If these conditions are met, it modifies the query to filter orders by adding a meta key (meta_key) of ‘_payment_method’ and a meta value (meta_value) based on the selected payment method. It uses sanitize_text_field to ensure that the input is safe and clean.
  • This function is hooked to the parse_query action, which allows you to modify the query parameters before it’s executed.

Filtering Orders by Non-Default Payment Gateways (Like PayPal, Stripe) in WooCommerce Admin

Upgraded to new WooCommerce version 8.5.1 and did you find the above code not working in it? Here is the modified code that works perfectly well, which is compatible with HPOS table storage and in the updated WooCommerce version 8.5.1:

Many WooCommerce stores integrate third party payment gateways like Stripe, Paypal alongside the default payment options. Even though admin has a mix of both default and non default payment gateway, this code snippet will easily filter any specific gateway as per their choice. This enhancement streamlines order processing and to manage orders more easily than ever before.


Note: The specific meta values associated with each gateway can vary depending on the gateway itself and make sure to use the right meta value in this specific line of the code:
‘<your_custom_gateway_meta_key>’ => __(‘Your Custom Gateway Name’, ‘woocommerce’), // Add more payment methods as needed );

// Add the Payment Method column to the Orders page in WooCommerce admin
function ts_add_payment_method_column($columns) {
    $columns['payment_method'] = __('Payment Method', 'woocommerce');
    return $columns;
}
add_filter('manage_woocommerce_page_wc-orders_columns', 'ts_add_payment_method_column');

// Display the payment method value in the Payment Method column
function ts_display_payment_method_column($column, $post_id) {
    if ($column == 'payment_method') {
        $order = wc_get_order($post_id);
        echo $order->get_payment_method_title();
    }
}
add_action('manage_woocommerce_page_wc-orders_custom_column', 'ts_display_payment_method_column', 10, 2);

// Add a custom dropdown for filtering by payment method
function ts_add_payment_method_filter($post_type) {
    // global $typenow;

    if ('shop_order' === $post_type) {
        $payment_methods = array(
            'bacs'   => __('BACS', 'woocommerce'),
            'cod'    => __('Cash on Delivery', 'woocommerce'),
            'cheque' => __('Cheque', 'woocommerce'),
             'ppcp-gateway' => __('PayPal', 'woocommerce'),
            // Add more payment methods as needed
        );

        echo '<select name="payment_method_filter">';
        echo '<option value="">Filter by Payment Method</option>';

        foreach ($payment_methods as $method => $label) {
            echo '<option value="' . $method . '">' . $label . '</option>';
        }

        echo '</select>';
    }
}
add_action('woocommerce_order_list_table_restrict_manage_orders', 'ts_add_payment_method_filter');

// Filter orders based on the selected payment method
function ts_filter_orders_by_payment_method($vars) {
    global $pagenow, $post_type;
    $meta_queries = array('relation' => 'AND');

    if (isset($_GET['payment_method_filter']) && !empty($_GET['payment_method_filter'])) {
        $selected_payment_method = sanitize_text_field($_GET['payment_method_filter']);

        // Modify the query to filter orders by the selected payment method
        $vars['payment_method'] =  $selected_payment_method;
    }
    return $vars;
}

add_filter('woocommerce_order_list_table_prepare_items_query_args', 'ts_filter_orders_by_payment_method');

Output

When the admin wants to filter by any non default payment gateway, such as Paypal, only orders paid via PayPal payment method gets filtered and displayed in the Payment Method column. This eliminates the need to sift through all orders and identify the custom ones manually, saving you time and effort.

How to Add a Payment Gateway Filter Field in WooCommerce >Admin Orders Page? - Tyche Softwares

Conclusion

The above code snippets help you to add a custom dropdown filter field enabling you to filter payment gateways on the admin interface of WooCommerce > Orders both on the WordPress post table and HPOS Custom Order Tables. If you prefer not to filter the payment gateways and would rather view all the payment methods for orders, you can easily achieve this only by adding the Payment Gateway Column to the WooCommerce ‘Orders’ list.

Similarly, filtering specific delivery details of orders is done effectively using the Order Delivery Date Pro for WooCommerce plugin.

How to Add a Payment Gateway Filter Field in WooCommerce >Admin Orders Page? - Tyche Softwares

It helps admins search, view, and sort orders and their delivery details quickly. You can also look at specific order details as per the requirement using the filter option on the WooCommerce orders page.

Browse more in: Code Snippets, WooCommerce How Tos, WooCommerce Tutorials

Share It:

Subscribe
Notify of
14 Comments
Newest
Oldest
Inline Feedbacks
View all comments
Andrea
2 months ago

Hi.
Many thanks for the snippet, it’s very useful.
Do you happen to have a filter for order origin as well, please?
It would be very useful to be able to filter for the origin/referral of orders as well, so that you can immediately know even visually how many came from that particular origin or referral.
Thanks again!

4 months ago

Greetings

How can I integrate the code with other payment methods, in particular:

 credit and debit card
 paypal
 teacher card (activated with an extra plugin)

Thank you

Anand
8 months ago

Filter not working with custom meta data when HPOS enable

Saranya
8 months ago
Reply to  Anand

Hi Anand,

To better assist you, could you please provide more details about the specific custom meta data you’re trying to filter and any error messages you might be encountering?

Anand
8 months ago
Reply to  Saranya

I have created custom field named zirconia for product and try to filter order table based on custom field value choose from dropdown. Also, I have given code below. // add material filter in order table add_action( ‘woocommerce_order_list_table_restrict_manage_orders’, ‘display_admin_shop_order_material_filter’); function display_admin_shop_order_material_filter() {   $screen = get_current_screen();      if( ‘woocommerce_page_wc-orders’ === $screen->id )    {     $domain  = ‘woocommerce’;     $zirconia = array( __(‘Argen HT+’, $domain), __(‘Argen Multilayer HT+’, $domain), __(‘Whitepeaks’, $domain) );     $current  = isset($_GET[‘filter_shop_order_material’])? $_GET[‘filter_shop_order_material’] : ”;          echo ‘<select name=”filter_shop_order_material”>     <option value=””>’ . __(‘Filter By Zorconia ‘, $domain) . ‘</option>’; if( ! empty( $zirconia ) )  { foreach($zirconia as $item )  {  printf( ‘<option… Read more »

Last edited 8 months ago by Anand
Anand
8 months ago
Reply to  Saranya

Hi,
Thanks for quick reply, but still not working actually custom meta query not filter order table when HPOS enable and latest WooCommerce version.

If I pass

$vars[‘payment_method’]=’$selected_payment_method’;

its working fine, but If pass

// Modify the query to filter orders by the selected Zirconia value
    $vars[‘meta_query’] = array(
      array(
        ‘key’   => ‘zirconia’,
        ‘value’  => $selected_zirconia_value,
        ‘compare’ => ‘=’,
      ),
    );

nothing happen also not give any errors. I also attach image of admin order table for your understanding.

Thanks

Orders-‹-Elements-Digital-—-WordPress
Anand
8 months ago
Reply to  Saranya

Hi

Thanks for reply but still not working, actually meta data store in woocommcerce_order_itemmeta table. The query return below array plese check.

Array ( [limit] => 20 [page] => 1 [paginate] => 1 [type] => shop_order [status] => Array ( [0] => wc-pending [1] => wc-processing [2] => wc-on-hold [3] => wc-completed [4] => wc-cancelled [5] => wc-refunded [6] => wc-failed [7] => wc-invoice-generated [8] => wc-order-received ) [orderby] => date [order] => DESC [search_filter] => all [meta_query] => Array ( [0] => Array ( [key] => zirconia [value] => Argen HT+ ) ) )
10 months ago

Hi,
This is not working after the Woocommerce 8.5.1 update.

9 months ago
Reply to  Saranya

Thank You for the update. It works like a charm. 🙂

14
0
Would love your thoughts, please comment.x
()
x