自动下载自定义woocommerce导出csv文件

时间:2018-03-14 07:57:20

标签: php wordpress function woocommerce

需要有关设置自动下载的帮助。

这里是我想实现的想法..我有一个自定义函数将woocommerce订单导出到csv文件。它是从商店订单手动下载>>操作>>导出到csv文件。

但是现在我想要在订单完成时... csv文件自动生成并保存在服务器上或发送到任何其他服务器

这是我的自定义功能

    // We Create Function To Register In The Dropdown Post Table
function action_bulk_register_function(){
global $post_type;

// filter if you want only in customposttype
if($post_type == 'shop_order') { ?>
<script type="text/javascript">
    jQuery(document).ready(function() {
        //Add an option to the Bulk actions drop-downs at the top and bottom of the page
        jQuery('<option>').val('TESTCSV').text("<?php _e('Export order in CSV format')?>").appendTo("select[name='action']");
    });
</script>
<?php
  }
   }
     //process the bulk action///
 add_action('load-edit.php', 'process_the_action_customposttype');

function process_the_action_customposttype(){
global $typenow;
if(isset($_REQUEST['post'])) {
    $post_ids = array_map('intval', $_REQUEST['post']);
}

if(empty($post_ids)) return;
// this is based on wp-admin/edit.php


$post_type = $typenow;
if($post_type == 'shop_order') {
    // get the action
    $wp_list_table = _get_list_table('WP_Posts_List_Table');
    $action = $wp_list_table->current_action();
    // Set The Action From Value Option You Created
    $allowed_actions = array("TESTCSV");
    if(!in_array($action, $allowed_actions)) return;
    // Here Add What You Want To

   ///////////////////////////////////////////////////

        header('Content-Type: text/csv');
        header('Content-Disposition: attachment; filename=test.csv');
                $stream = fopen('php://output', 'w+');
                fputcsv($stream, $csv_header);
        foreach($arrCSV as $key => $assoc_row){
            $numeric_row = array();
            foreach ($csv_header as $header_name) {
                mb_convert_variables('UTF-8', $assoc_row[$header_name]);
                $numeric_row[] = $assoc_row[$header_name];
            }
            fputcsv($stream, $numeric_row);
        }
    }



    exit();
} // end shop order
 }

  require_once(get_stylesheet_directory().'test.php');

0 个答案:

没有答案