【问题标题】:How to download as CSV file from woocommerce product displayed by php table script?如何从 php 表脚本显示的 woocommerce 产品中下载为 CSV 文件?
【发布时间】:2020-04-14 11:01:26
【问题描述】:

我正在尝试将产品库存下载为 .csv 文件。 使用下面的 php 脚本启用打开页面后,文件将自动下载。

<?php

$args = array(
    'status' => 'publish',
    'limit' => 100,
    'orderby' => 'title',
    'order'   => 'ASC'
);
$products = wc_get_products( $args );
$counter = 1;

echo "<input type='submit' value='Export' name='Export'>";
if (count($products)) {
    echo  "<table align='center'><tr>";
    echo "<td>";
    // Open the table
    echo "<table style ='background-color:#ffffff;border-collapse: collapse; margin-right:10px'><thead><tr>
            <th width='50' style='border:1px solid black'>Sl No.</th>
            <th width='400' style='border:1px solid black'>Product Name</th>
            <th style='border:1px solid black' width='54'>Stock</th>
            </tr></thead>";
    // Cycle through the array
    foreach ($products as $product) {
        // Output a row
        if($product->stock_status == 'instock'){
        echo "<tr>";
        echo "<td align='center' style='border:1px solid black'>$counter</td>";
        echo "<td style='border:1px solid black'>$product->name</td>";
        echo "<td align='center' style='border:1px solid black'>$product->stock_quantity</td>";
        echo "</tr>";
        $counter++;
        $user_arr[] = array($counter,$product->name,$product->stock_quantity);
    }
    }
    // Close the table
    echo "</table>";
    echo "</td>";
    //-----------------------out of stock------------
    echo "<td valign='top'>";
    // Open the table
    echo "<table style = 'background-color:#ffffff;border-collapse: collapse;'><thead><tr>
            <th width='50' style='border:1px solid black'>Sl No.</th>
            <th width='400' style='border:1px solid black'>Out of stock products</th>
            <th style='border:1px solid black' width='54'>Stock</th>
            </tr></thead>"; 
    // Close the table
    echo "</table>";
    echo "</td>";
    echo "</tr></table>";
}

$serialize_user_arr = serialize($user_arr);
$filename = 'product_stock.csv';
$export_data = unserialize($serialize_user_arr);

// file creation
$file = fopen($filename,"w");

foreach ($export_data as $line){
 fputcsv($file,$line);
}

fclose($file);

// download
header("Content-Description: File Transfer");
header("Content-Disposition: attachment; filename=".$filename);
header("Content-Type: application/csv; "); 

readfile($filename);

// deleting file
unlink($filename);
exit();

打开“product_stock.csv”下载的文件后,它没有与预期的行值正确对齐,但在每行和每列上显示 html 脚本。 单击“导出”按钮后如何下载文件product_stock.csv并在行中添加期望值?

【问题讨论】:

    标签: php wordpress csv woocommerce download


    【解决方案1】:

    我得到了解决方案。 添加了带按钮的表单。

    <form style="margin-left: 79%;margin-top: -59px;" method="post" id="export-form" action="">
            <?php submit_button('Export Stock Report', 'primary', 'download_csv' ); ?>
        </form>
    

    然后添加标题以正确对齐,这避免了 product_stock.csv 中的 html 标记

    header('Content-Type: text/csv; charset=utf-8'); //For .csv
    header('Content-Disposition: attachment; filename=product-stock-report-' . date('d-m-Y') . '.csv');
    $output = fopen('php://output', 'w');
    $headings = array( 'Sl. No.','Product Name', 'Stock' );
    fputcsv($output, $headings );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-07-28
      • 2013-04-21
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 2023-01-04
      • 1970-01-01
      相关资源
      最近更新 更多