将批量订单下载到PC并在Woocommerce管理员中显示管理员消息

时间:2018-03-31 11:22:26

标签: php woocommerce download bulk orders

在Woocommerce中,我已经实施了from this answer thread to one of my questions下面的代码,允许管理员用户从后端"商店订单列表"批量下载批量订单的标准化信息。为此,用户要么选择两个日期之间的时间段,要么逐个选择所需的顺序;之后,他运行了一个新的下载文件'处理他的请求的行动。

我的代码分为4个钩子函数:

  • 第一个显示html内容,包括日期输入和允许下载操作的新批量操作;

  • 第二个是当用户点击“应用操作”按钮时主要操作发生的位置:收集所有的woocommerce信息并将其写入服务器中的文件中;

  • 第三个请求用户将文件下载到他的PC(我不得不将这个文件从第二部分中分离出来,因为我下载的文件没有混合大量HTML垃圾的唯一方法是添加& #39;退出'结尾;

  • 第四个只是向用户显示一条消息,告知处理和下载了多少订单。

// 1st SUB-SNIPPET: Adding to admin order list bulk dropdown a custom action 'custom_downloads'
add_filter( 'bulk_actions-edit-shop_order', 'downloads_bulk_actions_edit_product', 100, 1 );
function downloads_bulk_actions_edit_product( $actions ) {

    ?><div class="alignleft actions custom">

        <span>From: </span>
        <input type="date" id="download_date_initial" name="download_date_initial"  value="<?php echo date('Y-m-d'); ?>" class="input-date" />
        <span> To: </span>
        <input type="date" id="download_date_final" name="download_date_final"  value="<?php echo date('Y-m-d'); ?>" class="input-date" />

    </div><?php

    $actions['write_downloads'] = __( 'Download orders', 'woocommerce' );
    return $actions;
}

// 2nd SUB-SNIPPET: 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 ) {
    global $attach_download_dir, $attach_download_file;
    global $countries_list, $countries_obj;

    if ( $action !== 'write_downloads' ) return $redirect_to; // Exit

    $processed_ids = array();

    // Opens file
    $myfile = fopen($attach_download_dir . '/' . $attach_download_file, "w") or die("Unable to open file!");

    $date_initial = $_REQUEST['download_date_initial'];
    $date_final = $_REQUEST['download_date_final'];
    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_ = [];

    if ( isset($post_ids) && empty($args_)) $array = $post_ids; else {
        $orders = wc_get_orders( $args_ );
        $array=[]; if (isset($orders)) foreach ($orders as $order) array_push($array, $order->get_id());
    }

    // Goes through each selected order   
    foreach ( $array as $key => $post_id ) {        
        $order = wc_get_order( $post_id );
        $order_data = $order->get_data();

        // Fills in woocommerce orders info as required ...
        fwrite($myfile, XXX);

        $processed_ids[] = $post_id;
    }
    // Closes file
    fclose($myfile);

    // Returns info to be used elsewhere, namely displaying message
    return $redirect_to = add_query_arg( array( 'write_downloads' => '1', 'processed_count' => count( $processed_ids ), ), $redirect_to );

}

// 3rd SUB-SNIPPET: requests admin user to download file to his PC
add_filter( 'handle_bulk_actions-edit-shop_order', 'downloads_handle_bulk_action_edit_shop_order_2', 20, 3 );
function downloads_handle_bulk_action_edit_shop_order_2( $redirect_to, $action, $post_ids ) {
    global $attach_download_dir, $attach_download_file;
    global $root_page;

    // Saves in pc
    $file_url = $root_page . 'wp-admin/' . $attach_download_dir . '/' . $attach_download_file;
    header('Content-Description: File Transfer');
    header('Content-Type: text/plain');
    header('Content-Disposition: attachment; filename=' . basename($file_url)); 
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    ob_clean();
    flush();
    readfile($file_url);
    exit;
}

// 4th SUB-SNIPPET: The results notice from bulk action on orders
add_action( 'admin_notices', 'downloads_bulk_action_admin_notice', 10 );
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 );

}

但是我在这里有3个问题:

  1. 如果我有&#34;下载到PC部件&#34;,即第3个子片段到位,则消息将不再显示在后端。如果我拿出来,我可以看到该消息,但不再要求用户将文件下载到他的PC。我尝试了几种替代方案,都没有成功。

  2. 如果想要逐个下载所选的订单,则没有问题。但是如果想要选择两个日期之间的所有订单,则管理员用户也必须至少选择一个订单来运行批量操作。否则,它将无法处理&#39; handle_bulk_actions-edit-shop_order&#39;钩。这很烦人,并且对用户没有任何意义,因为只选择该命令使系统通过该挂钩,尽管在这种情况下只有2个日期与确定要下载哪些订单相关。

  3. 我想要下载&#39;默认情况下在操作按钮中选择的操作,用户无需选择它。我尝试用Javascript做这个但是无济于事......

  4. 有什么想法吗?

1 个答案:

答案 0 :(得分:1)

对于问题点2(两个),批量操作仅适用于所选订单,不处理日期范围选择 ...所以我已部分删除了您的代码在你的第一个钩子功能(下面)......

  

您的日期范围下载功能,需要在其他地方单独添加。

对于问题点3(三个),它已在下面的第一个钩子函数中解决(测试并运行)。

对于问题点1(一)我已从第3个函数中删除了钩子,而是从最后一个显示自定义通知的钩子函数触发它...我希望它可以工作(未经测试)

我已经倒置(重新排序)你的第三个和最后一个功能:

// 1. Adding to admin order list bulk dropdown a custom action 'custom_downloads'
add_filter( 'bulk_actions-edit-shop_order', 'downloads_bulk_actions_edit_product', 100, 1 );
function downloads_bulk_actions_edit_product( $actions ) {
    $reordered_actions = array( 'write_downloads' => __( 'Download orders', 'woocommerce' ) );
    foreach( $actions as $key => $action ){
        // Reinserting reordered actions
        $reordered_actions[$key] = $action;
    }
    ?>
    <script type="text/javascript">
    jQuery( function($){
        $('#bulk-action-selector-top').find('option').eq(0).remove(); // Remove defaut option
        $('#bulk-action-selector-top').val('write_downloads'); // To be sure select "downloads"…
    });
    </script>
    <?php
    return $reordered_actions;
}

// 2. 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 ) {
    global $attach_download_dir, $attach_download_file, $countries_list, $countries_obj;

    if ( $action !== 'write_downloads' ) return $redirect_to; // Exit

    $processed_ids = array();

    // Opens file
    $myfile = fopen($attach_download_dir . '/' . $attach_download_file, "w") or die("Unable to open file!");

    $date_initial = $_REQUEST['download_date_initial'];
    $date_final = $_REQUEST['download_date_final'];
    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_ = [];

    if ( isset($post_ids) && empty($args_)) $array = $post_ids; else {
        $orders = wc_get_orders( $args_ );
        $array=[]; if (isset($orders)) foreach ($orders as $order) array_push($array, $order->get_id());
    }

    // Goes through each selected order   
    foreach ( $array as $key => $post_id ) {        
        $order = wc_get_order( $post_id );
        $order_data = $order->get_data();

        // Fills in woocommerce orders info as required ...
        fwrite($myfile, XXX);

        $processed_ids[] = $post_id;
    }
    // Closes file
    fclose($myfile);

    // Returns info to be used elsewhere, namely displaying message
    return $redirect_to = add_query_arg( array( 'write_downloads' => '1', 'processed_count' => count( $processed_ids ), ), $redirect_to );

}

// 3. Display a results notice from this bulk action on orders and trigger download to PC
add_action( 'admin_notices', 'downloads_bulk_action_admin_notice', 10 );
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 );

    // Execute the request to download file to  PC
    downloads_to_pc();
}

// 4. Simple function that request user to download file to his PC
function downloads_to_pc() {
    global $attach_download_dir, $attach_download_file, $root_page;

    // Saves in pc
    $file_url = $root_page . 'wp-admin/' . $attach_download_dir . '/' . $attach_download_file;
    header('Content-Description: File Transfer');
    header('Content-Type: text/plain');
    header('Content-Disposition: attachment; filename=' . basename($file_url)); 
    header('Expires: 0');
    header('Cache-Control: must-revalidate');
    header('Pragma: public');
    ob_clean();
    flush();
    readfile($file_url);
    exit;
}

代码进入活动子主题(或活动主题)的function.php文件。未经测试,它可以工作。