Slow WooCommerce Searches? Have lots of orders? Speed it up!
-
Is your Order search slow? Do you have a large number of orders? As the number of orders grows, search performance can degrade significantly. Limiting the search to specific fields like order number, last name, and email is a smart approach to improve efficiency.
I’m going to list two separate ways to achieve what you’re looking for:
Solution 1: Easier Approach:
This code snippet does the following:
- First, it removes the default search fields from WooCommerce orders.
- Then, it adds three custom search fields: Order ID (
_order_key
), Billing Last Name (_billing_last_name
), and Billing Email (_billing_email
).
function custom_woocommerce_shop_order_search_fields( $search_fields ) { // Remove default search fields $search_fields = array(); // Add custom search fields: Order ID, Billing Last Name, and Billing Email $search_fields[] = '_order_key'; // Order ID $search_fields[] = '_billing_last_name'; // Billing Last Name $search_fields[] = '_billing_email'; // Billing Email return $search_fields; } add_filter( 'woocommerce_shop_order_search_fields', 'custom_woocommerce_shop_order_search_fields' );
Solution 2: Comprehensive Approach:
This code snippet combines everything into one. It will:
- Use JavaScript to add a dropdown for selecting the search type (Order ID or Email) right before the search input in your WooCommerce orders page.
- Modify the WooCommerce order search functionality to search based on the selected option.
The JavaScript is added to the admin footer and will execute on the order search page, adding the dropdown dynamically.
This approach ensures that all modifications are contained within a single function and are executed in the correct context.
function custom_woocommerce_order_search_dropdown_and_custom_search() { // Add JavaScript to insert the dropdown ?> <script type="text/javascript"> jQuery(document).ready(function($) { var dropdownHtml = '<select name="search_by" id="search_by" style="margin-right: 8px;">' + '<option value="order_id">Order ID</option>' + '<option value="email">Email</option>' + '</select>'; $(dropdownHtml).insertBefore('#post-search-input'); }); </script> <?php // Modify the search query based on the selected option in the dropdown add_filter( 'woocommerce_shop_order_search_fields', function ( $search_fields ) { if ( isset( $_GET['search_by'] ) && 'email' === $_GET['search_by'] ) { $search_fields = array('_billing_email'); // Search by Billing Email } elseif ( isset( $_GET['search_by'] ) && 'order_id' === $_GET['search_by'] ) { $search_fields = array('_order_key'); // Search by Order ID } return $search_fields; }, 10, 2 ); } add_action( 'admin_footer', 'custom_woocommerce_order_search_dropdown_and_custom_search' );
As always, testing on a staging environment first is highly recommended.
After adding either of these two solutions to your
functions.php
file, your WooCommerce order search should only look through these specified fields, which could potentially speed up the search process.
- The topic ‘Slow WooCommerce Searches? Have lots of orders? Speed it up!’ is closed to new replies.