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.
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.
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.
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.
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.
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.
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!
Hi Andrea,
You’re welcome! To address your requirement, we have published a new post on adding Order Origin Filter Field in the WooCommerce Admin Orders Page. If you have any more questions or need further assistance, feel free to reach out!
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
Hi Nicola,
The post has been updated as per your request to integrate the code with other payment methods. Please find the code snippet below the heading ‘
Filtering Orders by Non-Default Payment Gateways (Like PayPal, Stripe) in WooCommerce Admin.’
Filter not working with custom meta data when HPOS enable
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?
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 »
Hi Anand,
The above code that you have provided does not include any function to add a new column to the WooCommerce orders table. It only adds a filter dropdown to filter orders based on the selected material. Please check out this gist file with the modified code that will add a filter field and the column as shown here (https://prnt.sc/sN30PxnwIO5s). However, since you are using a custom meta field you might need to adjust the meta key values used in the code accordingly to retrieve the material information from your custom meta field.
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
Based on your specific requirement, we have updated the post. Please refer to the code snippet below the heading “Create Custom Column and Filter Orders with Custom Meta Fields for HPOS-Based Order Storage.”
Hi
Hi,
This is not working after the Woocommerce 8.5.1 update.
Hi Narendran,
We have updated the post based on your requirement. Please refer the code below the heading ‘Code Snippet Compatible with HPOS Table Storage in WooCommerce Version 8.5.1’
Thank You for the update. It works like a charm. 🙂