• Resolved frenchomatic

    (@frenchomatic)


    Does anyone have any code snippets to search an additional field on an orders page. I can put a new column onto the orders page with the additional field value displayed but I don’t find a way to search it from the search box.

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @frenchomatic,

    We don’t have any ready code snippet for this; however, you can add search functionality for additional fields on the orders page using the woocommerce_shop_order_search_fields filter. This filter allows you to add custom meta to the list of search fields.

    Here I found some resources that might help you further:

    If you still need assistance, we recommend asking development questions on the #developers channel of the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question. You can also seek help from the following:

    I wish I could help more, but hopefully, this gets you going in the right direction to get the job done.

    Thread Starter frenchomatic

    (@frenchomatic)

    I am really surprised that the hasn’t been an ask by many users of woocommerce. Searching on an additional field makes a lot of sense. I have tried many different approaches without success. This code for me made sense but didn’t work either. Strange because I can get the values and echo them out onto the orders page in a column with no issues at all.

    add_filter( 'woocommerce_shop_order_search_fields', 'woocommerce_shop_order_search_order_total' );
    function woocommerce_shop_order_search_order_total( $search_fields ) {
        $search_fields[] = '_order_total';
    
        $search_fields[] = '_user_id';
    
        $search_fields[] = '_order_number';
    
        return $search_fields;
    } 

    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @frenchomatic,

    The code snippet you shared should indeed allow you to search by _order_total, _user_id, and _order_number. However, it seems like there might be an issue with your implementation.

    Please ensure that the additional fields you’re trying to search are stored as post meta in the wp_postmeta table in your WordPress database. The woocommerce_shop_order_search_fields filter only works with post metadata.

    Also, remember that the search feature in WooCommerce uses a LIKE SQL command, so it will return any order where the meta value contains the search term. You’ll need to enter the exact value if you’re searching for a specific order total, user ID, or order number.

    For reference, these particular forums are meant to provide general support for the core functionality of WooCommerce itself. For development and custom coding questions, it’s best to ask for insight related to those on either the WooCommerce Advanced Facebook group or the WooCommerce Community Slack. Many of our developers hang out there and will be able to offer insights into your question. You can also seek help from the following:

    I wish I could help more, but hopefully, this gets you going in the right direction to get some further insight/information.

    Thread Starter frenchomatic

    (@frenchomatic)

    Turns out to be simple if you know the right hook. Hope this helps someone else.

    add_filter( 'woocommerce_order_table_search_query_meta_keys', 'woocommerce_shop_order_search_order_total' );
    
    function woocommerce_shop_order_search_order_total( $search_fields ) {
    $search_fields[] = '_order_total';
    $search_fields[] = '_user_id';
    $search_fields[] = '_order_number';
    $search_fields[] = 'my_additional_field';
    
    return $search_fields;
    
    }

    Plugin Support Shameem R. a11n

    (@shameemreza)

    Hi @frenchomatic,

    I’m glad you were able to find a solution to your inquiry here and thanks for sharing it with the community too! ??

    Should you have further inquiries, kindly create a new topic here.

    Thanks!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Search Orders List Page’ is closed to new replies.