2015-12-25 13 views
5

Tôi đang cố xuất các chi tiết đơn đặt hàng woocommerce với trạng thái đơn đặt hàng cụ thể vào một tệp văn bản. Vấn đề của tôi là nó sẽ chỉ nhận được thông tin chi tiết cho một đơn đặt hàng vì tôi không sử dụng foreach. Có ai biết làm thế nào tôi có thể thay đổi mã của tôi để nó sẽ nhận được tất cả các đơn đặt hàng trong các hàng riêng biệt?Làm thế nào để sử dụng foreach trong fputcsv?

define('WP_USE_THEMES', false); 
require('/var/www/html/wp-blog-header.php'); 

global $wpdb; 
global $woocommerce; 
$args = array(
    'post_type'   => 'shop_order', 
    'post_status'  => 'publish', 
      'posts_per_page' => -1, 
    'tax_query' => array(
       array(
        'taxonomy' => 'shop_order_status', 
        'field' => 'slug', 
        'terms' => array('processing') 
       ) 
      ) 
); 

$my_query = new WP_Query($args); 
$orders = $my_query->posts; 
foreach($orders as $order) 
{ 
$order_id = $order->ID; 
    $billing_first_name =get_post_meta($order_id,'_billing_first_name',true); 
    $billing_last_name = get_post_meta($order_id,'_billing_last_name',true); 
    $billing_name = $billing_first_name. " ". $billing_last_name ; 
    $billing_address = get_post_meta($order_id,'_billing_address_1',true); 
    $billing_address2 = get_post_meta($order_id,'_billing_address_2',true); 
    $billing_city = get_post_meta($order_id,'_billing_city',true); 
    $billing_postcode = get_post_meta($order_id,'_billing_postcode',true); 
    $billing_country = get_post_meta($order_id,'_billing_country',true); 
    $billing_email = get_post_meta($order_id,'_billing_email',true); 
    $billing_phone = get_post_meta($order_id,'_billing_phone',true); 
} 

//headers 
header('Pragma: public'); 
header('Expires: 0'); 
header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
header('Content-Description: File Transfer'); 
header('Content-Type: text/csv'); 
header('Content-Disposition: attachment; filename=export.txt;'); 

//open file pointer to standard output 
$fp = fopen('php://output', 'w'); 
if ($fp) 
{ 
fputcsv($fp, array("[quickid]", "[sender]", "[receiver]", "[orgno]", "[vatno]", "[category1text]", "[category2text]", "[category3text]", "[category1no]", "[category2no]", "[category3no]", "[name]", "[address1]", "[address2]", "[zipcode]", "[city]", "[state]", "[country]", "[contact]", "[email]", "[phone]", "[fax]", "[sms]", "[doorcode]", "[postaladdress1]", "[postaladdress2]", "[postalzipcode]", "[postalcity]", "[postalstate]", "[postalcountry]", "[deliv1]", "[deliv2]", "[deliv3]", "[deliv4]", "[receiverref]", "[senderref]", "[codref]", "[profilegroup]", "[account_1_type]", "[account_1_number]", "[account_1_bic]", "[account_2_type]", "[account_2_number]", "[account_2_bic]", "[account_3_type]", "[account_3_number]", "[account_3_bic]", "[account_4_type]", "[account_4_number]", "[account_4_bic]", "[account_5_type]", "[account_5_number]", "[account_5_bic]", "[partner_plab_custno]", "[partner_plab_receiverid]", "[partner_plab_destcode]", "[partner_hit_custno]", "[partner_hit_retailerid]", "[partner_hit_sortpos]", "[partner_pbrev_custno]", "[partner_pbrev_paymentmethod]"), "\t", " "); 
fputcsv($fp, array("1", "N", "Y", "", "", "", "", "", "", "", "", $billing_name, $billing_address, $billing_address2, $billing_postcode, $billing_city, "", $billing_country, $billing_name, $billing_email, $billing_phone, "", $billing_phone, "", $billing_address, $billing_address2, $billing_postcode, $billing_city, "", $billing_country, "", "", "", "", "Ordernummer", "Ordernummer", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""), "\t", " "); 
} 
fclose($fp); 

Trả lời

1

Bạn dường như đã đóng foreach() đầu - mở file trên vòng lặp, đặt tiêu đề ở phía trên, và có fputcsv() bên trong nó sau đó đóng tệp bên ngoài vòng lặp. Bạn cũng cần phải sử dụng "a" - chắp thêm - chế độ không "w" hoặc nó sẽ ghi đè mọi thứ mỗi lần.

Giữ WordPress của bạn và phần Query ở phía trên và thử lệnh này cho mã, do đó vòng lặp đi qua các fputcsv() mỗi lần nó được một mục mới:

Nếu bạn cần phải đặt tiêu đề bên trong tập tin này có mã phù hợp https://www.jonasjohn.de/snippets/php/post-request.htm

//open file pointer to standard output 
$fp = fopen('php://output', 'a'); 
if ($fp){ 
    if (filesize($fp) == 0){ 
    // headers 
    // add your header rows if it is a new file 
    // you may need to move these header(...); to the top of your page 
    // if you get "Headers already sent" errors as you have already included (required) 
    // a header - otherwise print them using fputs(); to the output file 
    // if that is where you want them. 
    // for example fputs($fp, "Pragma: public\r\n"); 
    //    fputs($fp, "Content-type: text/csv\r\n"); 

    header('Pragma: public'); 
    header('Expires: 0'); 
    header('Cache-Control: must-revalidate, post-check=0, pre-check=0'); 
    header('Content-Description: File Transfer'); 
    header('Content-Type: text/csv'); 
    header('Content-Disposition: attachment; filename=export.txt;'); 
    // add your CSV header row if it is a new file 
    fputcsv($fp, array("[quickid]", "[sender]", "[receiver]", "[orgno]", "[vatno]", "[category1text]", "[category2text]", "[category3text]", "[category1no]", "[category2no]", "[category3no]", "[name]", "[address1]", "[address2]", "[zipcode]", "[city]", "[state]", "[country]", "[contact]", "[email]", "[phone]", "[fax]", "[sms]", "[doorcode]", "[postaladdress1]", "[postaladdress2]", "[postalzipcode]", "[postalcity]", "[postalstate]", "[postalcountry]", "[deliv1]", "[deliv2]", "[deliv3]", "[deliv4]", "[receiverref]", "[senderref]", "[codref]", "[profilegroup]", "[account_1_type]", "[account_1_number]", "[account_1_bic]", "[account_2_type]", "[account_2_number]", "[account_2_bic]", "[account_3_type]", "[account_3_number]", "[account_3_bic]", "[account_4_type]", "[account_4_number]", "[account_4_bic]", "[account_5_type]", "[account_5_number]", "[account_5_bic]", "[partner_plab_custno]", "[partner_plab_receiverid]", "[partner_plab_destcode]", "[partner_hit_custno]", "[partner_hit_retailerid]", "[partner_hit_sortpos]", "[partner_pbrev_custno]", "[partner_pbrev_paymentmethod]"), "\t", " ", "\n"); 
    } // end of adding headers if new file 

    $my_query = new WP_Query($args); 
    $orders = $my_query->posts; 
    foreach($orders as $order) 
    { 
    $order_id = $order->ID; 
     $billing_first_name =get_post_meta($order_id,'_billing_first_name',true); 
     $billing_last_name = get_post_meta($order_id,'_billing_last_name',true); 
     $billing_name = $billing_first_name. " ". $billing_last_name ; 
     $billing_address = get_post_meta($order_id,'_billing_address_1',true); 
     $billing_address2 = get_post_meta($order_id,'_billing_address_2',true); 
     $billing_city = get_post_meta($order_id,'_billing_city',true); 
     $billing_postcode = get_post_meta($order_id,'_billing_postcode',true); 
     $billing_country = get_post_meta($order_id,'_billing_country',true); 
     $billing_email = get_post_meta($order_id,'_billing_email',true); 
     $billing_phone = get_post_meta($order_id,'_billing_phone',true); 

     fputcsv($fp, array("1", "N", "Y", "", "", "", "", "", "", "", "", $billing_name, $billing_address, $billing_address2, $billing_postcode, $billing_city, "", $billing_country, $billing_name, $billing_email, $billing_phone, "", $billing_phone, "", $billing_address, $billing_address2, $billing_postcode, $billing_city, "", $billing_country, "", "", "", "", "Ordernummer", "Ordernummer", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", "", ""), "\t", " ", "\n"); 

    } // end of foreach 
fclose($fp); 
}else{ 
echo "Failed to open file $fp"; 
} // end of if ($fp) 

Ross Smith II trả lời ở đây có thể có ích fputcsv doesn't write any data in a CSV file

và bạn có thể cần phải thêm một "\ n" ở cuối dòng (dấu ngoặc kép chỉ) để làm chắc chắn họ không xuất hiện trên cùng một dòng mà cuộn qua hàng dặm. Có thể là tài liệu tham khảo có ích http://php.net/manual/en/function.fputcsv.php

Chương và câu trên foreach(): How does PHP 'foreach' actually work?

Các vấn đề liên quan