将自定义批量操作添加到Woocommerce 3中的管理订单列表

Lca*_*tao 3 php backend bulk orders woocommerce

在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()
        )
    }
}
Run Code Online (Sandbox Code Playgroud)

单击按钮时触发此功能...

我想从管理订单列表批量选择功能中启用类似的功能.因此,店铺经理在管理员订单列表中选择的订单(参见下面的屏幕截图)将被发送到类似的自定义脚本,然后下载.

在这种情况下,所选订单将覆盖订单检索中的指定日期(如果有).

在此输入图像描述

但是,我找不到要访问的变量,告诉我管理员用户当时选择了哪些订单.

任何帮助将不胜感激…

Loi*_*tec 5

以下是使您的功能适用于批量订单列表操作选择的完整方法,而不是日期范围:

// 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 );
}
Run Code Online (Sandbox Code Playgroud)

代码位于活动子主题(或活动主题)的function.php文件中.经过测试和工作.

在此输入图像描述

在此输入图像描述

在返回的网址中,我有类似的内容(对于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'] (名称动作:truefalse)
  • $_GET['processed_count'] (所选订单数量)
  • $_GET['processed_ids'] (订单ID由url编码的昏迷分隔%2C)

所以你可以执行你的脚本:

  • 我的代码函数里面(比如在我的代码中)......或者......
  • 之外,一旦触发动作就使用可用变量...

从下拉订单批量操作中删除特定操作

例如,我们要删除"暂停"状态更改:

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;
}
Run Code Online (Sandbox Code Playgroud)

所有状态更改键都以mark_+状态slug (不带wc-)开头.