1

I'm trying to add a search bar to the WooCommerce Orders page on the frontend customer account dashboard for specific user roles (don't want showing for all customers).

I feel like I'm close, but the below code is just not working. I only need to search by order number. This code produces a "No order has been made yet." result despite there being many orders in my test account.

I also notice that when conducting the search, the URL doesn't change like I'd normally expect it to so I'm wondering if that's a tip-off to what's wrong with the code...?

function add_search_to_orders() {
 $allowed_roles = array('administrator', 'shop_manager');
 $user = wp_get_current_user();
 $user_roles = (array) $user->roles;
 if (array_intersect($allowed_roles, $user_roles)) {
 echo '<form method="post" id="orders-search-form">
 <input type="text" name="search_orders" placeholder="Search by order number..." />
 <input type="hidden" name="order_status" value="wc-completed" />
 <button type="submit">Search</button>
 </form>';
 }
}
add_action('woocommerce_before_account_orders', 'add_search_to_orders');
function search_all_orders_by_number($args) {
 if (isset($_POST['search_orders']) && !empty($_POST['search_orders'])) {
 $search_term = sanitize_text_field($_POST['search_orders']);
 $args['meta_query'] = array(
 array(
 'key' => '_order_number',
 'value' => $search_term,
 'compare' => 'LIKE'
 )
 );
 }
 return $args;
}
add_filter('woocommerce_my_account_my_orders_query', 'search_all_orders_by_number');
LoicTheAztec
257k25 gold badges404 silver badges451 bronze badges
asked Jan 8, 2025 at 13:41

2 Answers 2

2

Your provided code works just fine for "_order_number" meta_key, but since High Performance Order Storage is now enabled by default and uses a WooCommerce custom database table, all your order numbers need to be saved using WC_Data add_meta_data() or update_meta_data() methods, instead of using WordPress post meta functions.

See: Order custom field added via checkout is lost after WooCommerce HPOS sync

answered Jan 8, 2025 at 16:09
Sign up to request clarification or add additional context in comments.

2 Comments

So even though I have the compatibility mode enabled, this wouldn't work? No way to modify my code in some way so it can see the data in the HPOS?
Be aware: Compatibility mode allow only to save the data in both tables (WP post meta and WC order meta) when using WC_Data add_meta_data() or update_meta_data() methods, but not if you save(d) those order numbers using WordPress Post meta functions...
1

For those that stumble on this and are interested, here is the final code I got to work successfully with a WC HPOS store. Worked perfectly and I added some other mods to have a reset ability.

// Add search field to the top of WooCommerce My Account > Orders list
function add_search_to_orders() {
 $allowed_roles = array('administrator', 'shop_manager'); // Add or remove any other user roles here
 $user = wp_get_current_user();
 $user_roles = (array) $user->roles;
 if (array_intersect($allowed_roles, $user_roles)) {
 $show_clear_link = isset($_GET['search_orders']) && !empty($_GET['search_orders']);
 echo '<form method="get" id="orders-search-form">
 <input type="text" name="search_orders" placeholder="Search by order number..." value="' . (isset($_GET['search_orders']) ? esc_attr($_GET['search_orders']) : '') . '" />
 <button type="submit">Search</button>';
 if ($show_clear_link) {
 echo '<a href="' . esc_url(remove_query_arg('search_orders')) . '">Reset Search</a>';
 }
 echo '</form>';
 }
}
add_action('woocommerce_before_account_orders', 'add_search_to_orders');
// Make the search field function with a WooCommerce HPOS shop
function search_all_orders_by_number($query) {
 if (isset($_GET['search_orders']) && !empty($_GET['search_orders'])) {
 $search_term = sanitize_text_field($_GET['search_orders']);
 
 $query['post__in'] = array($search_term);
 }
 return $query;
}
add_filter('woocommerce_my_account_my_orders_query', 'search_all_orders_by_number');
answered Jan 18, 2025 at 16:05

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.