将自定义批量操作添加到 Woocommerce 3 中的管理员订单列表
Add custom bulk actions to admin orders list in Woocommerce 3
在 Woocommerce 后端 (admin),我有一个功能允许商店经理下载两个日期之间的所有订单,其中包含一组特定的所需数据:
function write_to_file($date_initial, $date_final) {
global $attach_download_dir, $attach_download_file;
// Opens/creates file
$myfile = fopen($attach_download_dir . '/' . $attach_download_file, "w") or die("Unable to open file!");
// Populates first line
fwrite($myfile, 'Date; Parent Order ID; Order ID' . PHP_EOL);
// Retrieves orders data
if ( isset($date_initial) && isset($date_final) ) $args = array( 'date_created' => $date_initial . '...' . $date_final );
if ( isset($date_initial) && empty($date_final) ) $args = array( 'date_created' => '>=' . $date_initial );
if ( empty($date_initial) && isset($date_final) ) $args = array( 'date_created' => '<=' . $date_final );
if ( empty($date_initial) && empty($date_final) ) $args = array( );
$orders = wc_get_orders( $args );
// Populates file with orders data
foreach ($orders as $order) {
$order_data = $order->get_data();
fwrite($myfile,
// Date of order creation
$order_data['date_created']->date('d/M/Y') . '; ' .
// Parent Order ID
'#' . ( ( $order->get_type() === 'shop_order' ) ? $order->get_id() : $order->get_parent_id() ) . '; ' .
// Order ID
'#' . $order->get_id()
)
}
}
单击按钮会触发此功能...
我想从管理订单列表批量选择功能中启用类似功能。因此店长在管理员订单列表(见下面的截图)中选择的订单将被发送到一个类似的自定义脚本,然后下载。
在这种情况下,所选订单将覆盖订单检索中的指定日期(如果有)。
但是,我找不到要访问的变量来告诉我管理员用户当时选择了哪些订单。
任何帮助将不胜感激……
这是使您的功能适用于批量订单列表操作选择而不是日期范围的完整方法:
// 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 );
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。 已测试并有效。
在返回的 url 中,我有类似的内容(对于 2 个已选择/已处理的订单):
wp-admin/edit.php?post_type=shop_order&paged=1&write_downloads=1&processed_count=2&processed_ids=847%2C846
我无法测试您包含的脚本,但这是在 Woocommerce 订单管理员列表中执行此操作的方法。
变量:
如您所见,可用变量由 add_query_arg()
函数设置。触发动作时,您通过 GET 方法获取 URL 中的那些变量...
你也可以自己设置任何变量…
在此示例中,您可以将 $_GET
或 $_REQUEST
与:
一起使用
$_GET['write_downloads']
(名称操作: true
或 false
)
$_GET['processed_count']
(已选择的订单数)
$_GET['processed_ids']
(订单 ID 由 url-encoded 逗号 %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…
从下拉列表中删除特定操作订单批量操作
例如我们要删除"On hold"状态变化:
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-
).
在 Woocommerce 后端 (admin),我有一个功能允许商店经理下载两个日期之间的所有订单,其中包含一组特定的所需数据:
function write_to_file($date_initial, $date_final) {
global $attach_download_dir, $attach_download_file;
// Opens/creates file
$myfile = fopen($attach_download_dir . '/' . $attach_download_file, "w") or die("Unable to open file!");
// Populates first line
fwrite($myfile, 'Date; Parent Order ID; Order ID' . PHP_EOL);
// Retrieves orders data
if ( isset($date_initial) && isset($date_final) ) $args = array( 'date_created' => $date_initial . '...' . $date_final );
if ( isset($date_initial) && empty($date_final) ) $args = array( 'date_created' => '>=' . $date_initial );
if ( empty($date_initial) && isset($date_final) ) $args = array( 'date_created' => '<=' . $date_final );
if ( empty($date_initial) && empty($date_final) ) $args = array( );
$orders = wc_get_orders( $args );
// Populates file with orders data
foreach ($orders as $order) {
$order_data = $order->get_data();
fwrite($myfile,
// Date of order creation
$order_data['date_created']->date('d/M/Y') . '; ' .
// Parent Order ID
'#' . ( ( $order->get_type() === 'shop_order' ) ? $order->get_id() : $order->get_parent_id() ) . '; ' .
// Order ID
'#' . $order->get_id()
)
}
}
单击按钮会触发此功能...
我想从管理订单列表批量选择功能中启用类似功能。因此店长在管理员订单列表(见下面的截图)中选择的订单将被发送到一个类似的自定义脚本,然后下载。
在这种情况下,所选订单将覆盖订单检索中的指定日期(如果有)。
但是,我找不到要访问的变量来告诉我管理员用户当时选择了哪些订单。
任何帮助将不胜感激……
这是使您的功能适用于批量订单列表操作选择而不是日期范围的完整方法:
// 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 );
}
代码进入您的活动子主题(或活动主题)的 function.php 文件。 已测试并有效。
在返回的 url 中,我有类似的内容(对于 2 个已选择/已处理的订单):
wp-admin/edit.php?post_type=shop_order&paged=1&write_downloads=1&processed_count=2&processed_ids=847%2C846
我无法测试您包含的脚本,但这是在 Woocommerce 订单管理员列表中执行此操作的方法。
变量:
如您所见,可用变量由 add_query_arg()
函数设置。触发动作时,您通过 GET 方法获取 URL 中的那些变量...
你也可以自己设置任何变量…
在此示例中,您可以将 $_GET
或 $_REQUEST
与:
$_GET['write_downloads']
(名称操作:true
或false
)$_GET['processed_count']
(已选择的订单数)$_GET['processed_ids']
(订单 ID 由 url-encoded 逗号%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…
从下拉列表中删除特定操作订单批量操作
例如我们要删除"On hold"状态变化:
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 (withoutwc-
).