Search code examples
phpwoocommerceadminordershighperformance

Extend search on WooCommerce admin orders list from metadata when HPOS is enabled


We have created Custom fields the customers need to fill out to recieve their order since they are being sent to someone other then who ordered it. When someone other then who ordered it ask for a updates they always know the reciepient unfortunatly we are unable to look that information up since they are customer fields, and WooCommerce by default does not search meta data. I need a code to create a ability to search for that data. I know its located in the Order_meta folder rather then in the post-meta.

Found another post that was using the following codes:

add_filter( 'woocommerce_shop_order_search_fields', 
return $search_fields; 

(Had more code then this, but since I don't know how to write code, could not get it to accept it hopefully its enough to show what was the key function used in the other post.)

Now I am not sure how that connects to post_meta, but maybe the search field is already connected to post meta, the issue is my data is in the Orders_Meta folder. Is their a code snippet that would allow me to extend that search?


Solution

  • Updated

    Use the following that will work with legacy orders (WordPress "shop_order" post type) and newly High Performance Order Storage (HPOS).

    Replace _meta_key with the desired meta key (to extend search on admin orders).

    // Make order meta data searchable
    add_filter( 'woocommerce_order_table_search_query_meta_keys', 'additional_wc_order_search_meta_keys' ); // HPOS
    add_filter( 'woocommerce_shop_order_search_fields', 'additional_wc_order_search_meta_keys' ); // Legacy
    function additional_wc_order_search_meta_keys( $meta_keys ){
        $meta_keys[] = '_meta_key'; // One line for each meta key (if multiple)
    
        return $meta_keys;
    }
    

    Code goes in functions.php file of your child theme (or in a plugin).

    Tested and work on WooCommerce last version with or without High-Performance Order Storage (HPOS) enabled.