如何在WooCommerce 3后台订单列表添加自定义批量操作?
在WooCommerce 3后台订单列表添加自定义批量操作(结合write_to_file函数)
嘿,我来帮你把现有的write_to_file函数和WooCommerce后台的订单列表批量操作结合起来,一步步实现:
步骤1:把自定义批量操作选项加到订单列表的下拉菜单里
首先得让管理员在订单列表的批量操作下拉里看到我们的选项,用WooCommerce专门的钩子bulk_actions-edit-shop_order来做:
add_filter( 'bulk_actions-edit-shop_order', 'add_custom_bulk_order_action' ); function add_custom_bulk_order_action( $bulk_actions ) { // 添加上我们的自定义操作,显示文字和对应的标识 $bulk_actions['export_selected_orders'] = '导出选中订单数据'; return $bulk_actions; }
步骤2:处理批量操作的提交逻辑
接下来要处理当管理员选中订单、选择我们的操作并点击「应用」后的逻辑。这里用handle_bulk_actions-edit-shop_order钩子,我给你两种适配方案,看你需求选:
方案A:基于选中订单的日期区间调用原函数
如果你的write_to_file本来就是按日期区间导出订单,那我们可以先获取选中订单里最早和最晚的下单日期,再把这两个日期传给原函数:
add_action( 'handle_bulk_actions-edit-shop_order', 'process_custom_bulk_order_action', 10, 3 ); function process_custom_bulk_order_action( $redirect_to, $action, $post_ids ) { // 先判断是不是我们的自定义操作,不是的话直接返回原页面 if ( $action !== 'export_selected_orders' ) { return $redirect_to; } global $attach_download_dir, $attach_download_file; // 初始化最早和最晚日期 $date_initial = date( 'Y-m-d H:i:s', PHP_INT_MAX ); $date_final = date( 'Y-m-d H:i:s', 0 ); // 遍历选中的订单,找到最早和最晚的下单日期 foreach ( $post_ids as $order_id ) { $order = wc_get_order( $order_id ); if ( $order ) { $order_date = $order->get_date_created()->date( 'Y-m-d H:i:s' ); if ( $order_date < $date_initial ) { $date_initial = $order_date; } if ( $order_date > $date_final ) { $date_final = $order_date; } } } // 调用你已经写好的write_to_file函数 write_to_file( $date_initial, $date_final ); // 给管理员加个成功提示,返回原页面时带上参数 $redirect_to = add_query_arg( array( 'bulk_action' => 'export_completed', 'export_count' => count( $post_ids ), ), $redirect_to ); return $redirect_to; }
方案B:修改函数直接导出选中的订单
如果更想只导出管理员选中的那些订单(而不是日期区间内的所有订单),那可以调整原函数或者新增一个专门处理选中订单的函数:
先新增适配选中订单的导出函数(或者修改你的write_to_file):
function write_selected_orders_to_file( $order_ids ) { global $attach_download_dir, $attach_download_file; // 打开或创建文件,注意服务器要有该目录的写入权限 $myfile = fopen( $attach_download_dir . '/' . $attach_download_file, "w" ) or die( "无法打开文件!" ); // 写入表头,和你原函数保持一致 fwrite( $myfile, 'Date; Parent Order ID; Order ID' . PHP_EOL ); // 遍历每个选中的订单,写入对应数据 foreach ( $order_ids as $order_id ) { $order = wc_get_order( $order_id ); if ( $order ) { $order_date = $order->get_date_created()->date( 'Y-m-d' ); // 处理父订单ID,如果是子订单就取父ID,否则用自身ID $parent_order_id = $order->get_parent_id() ? $order->get_parent_id() : $order_id; $line = "$order_date; $parent_order_id; $order_id" . PHP_EOL; fwrite( $myfile, $line ); } } fclose( $myfile ); // 触发浏览器下载文件(如果你的原函数已经有这段逻辑,就不用重复加) header( 'Content-Type: text/csv' ); header( 'Content-Disposition: attachment; filename="' . $attach_download_file . '"' ); readfile( $attach_download_dir . '/' . $attach_download_file ); exit; }
然后修改批量处理逻辑,调用这个新函数:
add_action( 'handle_bulk_actions-edit-shop_order', 'process_custom_bulk_order_action', 10, 3 ); function process_custom_bulk_order_action( $redirect_to, $action, $post_ids ) { if ( $action !== 'export_selected_orders' ) { return $redirect_to; } // 调用新的选中订单导出函数 write_selected_orders_to_file( $post_ids ); return $redirect_to; }
步骤3:给管理员加个成功提示
最后,在订单页面加个提示,让管理员知道导出完成了:
add_action( 'admin_notices', 'custom_bulk_order_action_notice' ); function custom_bulk_order_action_notice() { // 检查是不是我们的导出操作完成的参数 if ( ! isset( $_GET['bulk_action'] ) || $_GET['bulk_action'] !== 'export_completed' ) { return; } $count = intval( $_GET['export_count'] ); echo '<div class="notice notice-success is-dismissible"> <p>已成功导出 ' . $count . ' 个订单的数据,请查看下载文件。</p> </div>'; }
一些注意点
- 确保
$attach_download_dir和$attach_download_file变量已经正确定义,而且服务器对该目录有写入权限,不然会创建文件失败 - 如果你的原
write_to_file函数已经包含了触发浏览器下载的HTTP头逻辑,就不用在新函数里重复加了 - 测试前建议备份一下订单数据,避免意外情况
内容的提问来源于stack exchange,提问作者Lcapitao
相关产品推荐
相关产品推荐

