Add custom bulk actions to admin orders list in Woocommerce 3

前端 未结 1 977
青春惊慌失措
青春惊慌失措 2021-01-01 03:50

In Woocommerce backend (admin), I have a function that allows the shop-manager to download all orders between two dates with a specific bunch of required data:

相关标签:
1条回答
  • 2021-01-01 04:22

    Here is the complete way to make your functionality work on bulk order list action selection, instead of date range:

    // Adding to admin order list bulk dropdown a custom action 'custom_downloads'
    add_filter( 'bulk_actions-edit-shop_order', 'downloads_bulk_actions_edit_product', 20, 1 );
    function downloads_bulk_actions_edit_product( $actions ) {
        $actions['write_downloads'] = __( 'Download orders', 'woocommerce' );
        return $actions;
    }
    
    // Make the action from selected orders
    add_filter( 'handle_bulk_actions-edit-shop_order', 'downloads_handle_bulk_action_edit_shop_order', 10, 3 );
    function downloads_handle_bulk_action_edit_shop_order( $redirect_to, $action, $post_ids ) {
        if ( $action !== 'write_downloads' )
            return $redirect_to; // Exit
    
        global $attach_download_dir, $attach_download_file; // ???
    
        $processed_ids = array();
    
        foreach ( $post_ids as $post_id ) {
            $order = wc_get_order( $post_id );
            $order_data = $order->get_data();
    
            // Your code to be executed on each selected order
            fwrite($myfile,
                $order_data['date_created']->date('d/M/Y') . '; ' .
                '#' . ( ( $order->get_type() === 'shop_order' ) ? $order->get_id() : $order->get_parent_id() ) . '; ' .
                '#' . $order->get_id()
            );
            $processed_ids[] = $post_id;
        }
    
        return $redirect_to = add_query_arg( array(
            'write_downloads' => '1',
            'processed_count' => count( $processed_ids ),
            'processed_ids' => implode( ',', $processed_ids ),
        ), $redirect_to );
    }
    
    // The results notice from bulk action on orders
    add_action( 'admin_notices', 'downloads_bulk_action_admin_notice' );
    function downloads_bulk_action_admin_notice() {
        if ( empty( $_REQUEST['write_downloads'] ) ) return; // Exit
    
        $count = intval( $_REQUEST['processed_count'] );
    
        printf( '<div id="message" class="updated fade"><p>' .
            _n( 'Processed %s Order for downloads.',
            'Processed %s Orders for downloads.',
            $count,
            'write_downloads'
        ) . '</p></div>', $count );
    }
    

    Code goes in function.php file of your active child theme (or active theme). Tested and works.

    In the returned url I have something like (for 2 selected / processed orders): wp-admin/edit.php?post_type=shop_order&paged=1&write_downloads=1&processed_count=2&processed_ids=847%2C846

    I can't test your included script but it's the way to do it on Woocommerce Orders Admin list.


    VARIABLES:

    The available variables are set by add_query_arg() function as you will see. When the action is triggered, you get those variables in the URL through GET method…

    You can set any variable yourself too…

    In this example you can use $_GET or $_REQUEST with:

    • $_GET['write_downloads'] (the name action: true or false)
    • $_GET['processed_count'] (the number of selected orders)
    • $_GET['processed_ids'] (the Order Ids separated by an url-encoded coma %2C)

    So you can either execute your script:

    • inside my code function (like in my code) … or …
    • outside it, using the available variables once the action is triggered…

    To remove a specific action from the dropdown orders bulk actions

    For example we want to remove "On hold" status change:

    add_filter( 'bulk_actions-edit-shop_order', 'remove_a_bulk_order_action', 20, 1 );
    function remove_a_bulk_order_action( $actions ) {
        unset($actions['mark_on-hold']);
    
        return $actions;
    }
    

    All statuses change keys start with mark_ + the status slug (without wc-).

    0 讨论(0)
提交回复
热议问题