WooCommerce:商店订单批量操作在点击时设置变量

WooCommerce: Shop order bulk actions set variable on click

我为我的订单创建了自定义 WooCommerce 批量操作。我现在的问题是:是否可以检查后端何时按下批量操作?因为当我的自定义操作被触发时,我想向所选订单添加元数据。我只需要知道这是否可行,如果可行,我该如何挂钩选择功能?

代码:

/**
 * 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;
}

我的建议:

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

您可以这样使用 handle_bulk_actions-edit-shop_order 过滤器钩子:

// 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;
}

代码进入您的活动子主题(或活动主题)的 function.php 文件。它应该有效。

查看相关回答: