phpwordpresswoocommercebackendorders

Add a button on top of WooCommerce admin orders list (HPOS)


I have been struggling to find a hook that allows me to add a button to the top of the woocommerce admin "orders page", but so far unsuccessfully. I have found hooks to add action buttons to the action column, as well as inside each orders page ... but not where I need now.

If there is no hook, then an alternative approach.

More specifically, I attach an image with the place I am referring to

enter image description here

Any suggestions?


Solution

  • Update for High Performance Order Storage (HPOS).

    Because this is related to Wordpress and not specific to Woocommerce as Orders are just a custom post type. so the following code will display a custom button on the top zone just after existing fields and buttons:

    add_action( 'manage_posts_extra_tablenav', 'admin_order_list_top_bar_button', 20, 1 );
    function admin_order_list_top_bar_button( $which ) {
        global $typenow;
    
        if ( 'shop_order' === $typenow && 'top' === $which ) {
            printf('<div class="alignleft actions custom">
            <button type="submit" name="custom" style="height:32px;" class="button" value="%s">%s</button>
            </div>', 'yes', esc_html__('Custom', 'woocommerce') );
        }
    }
    

    To make is work when High Performance Order Storage is enabled, use:

    add_action( 'woocommerce_order_list_table_extra_tablenav', 'admin_hpos_wc_order_list_top_bar_button', 20, 2 ); 
    function admin_hpos_wc_order_list_top_bar_button( $order_type, $which ) {
        if( 'shop_order' === $order_type && 'top' === $which ) {
            printf('<div class="alignright actions custom">
            <button type="submit" name="custom" style="height:32px;" class="button" value="%s">%s</button>
            </div>', 'yes', esc_html__('Custom', 'woocommerce') );
        }
    }
    

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

    Screenshots:

    On WooCommerce legacy orders (Stored on WordPress Post tables):

    enter image description here

    On WooCommerce with HPOS enabled (Stored on WooCommerce custom tables):

    enter image description here

    Related: Filtering orders list in WooCommerce with HPOS


    Continuation: Run a function on custom button click in WooCommerce admin order list (HPOS)