Search code examples
phpwordpresswoocommerceordersbulk

WooCommerce: Shop order bulk actions set variable on click


I've created a custom WooCommerce bulk action for my orders. My question is now: Is it possible to check when the bulk action gets pressed in the backend? Because when my custom action gets triggered I want to add a meta to the selected order. I just need to know if this is possible and if yes how can I hook in the selection function?

Code:

/**
 * Add custom bulk actions in woocommerce order overview
 */
add_filter( 'bulk_actions-edit-shop_order', 'custom_shop_order_bulk_actions', 999 );
function custom_shop_order_bulk_actions( $actions ) {
    //Remove on hold, personal data and processing status mark
    unset( $actions['mark_on-hold'], $actions['remove_personal_data'], $actions['mark_processing'] );

    $actions['invoice-external']    = __( 'PDF Rechnung Extern' );

    return $actions;
}

My suggestion:

add_filter( 'hook_into_bulk_action-invoice-external', 'do_something' )
function do_something() {
    global $abc = 1;
}

Solution

  • You can use handle_bulk_actions-edit-shop_order filter hook this way:

    // Process the bulk action from selected orders
    add_filter( 'handle_bulk_actions-edit-shop_order', 'process_bulk_actions_edit_shop_order', 10, 3 );
    function process_bulk_actions_edit_shop_order( $redirect_to, $action, $post_ids ) {
        if ( $action === 'invoice-external' ){
            // Add (or update) order post meta data
            update_post_meta( $post_id, '_your_meta_key', $some_value );
        }
        return $redirect_to;
    }
    

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

    See this related answer: Process custom bulk action on admin Orders list in Woocommerce