Reputation: 23
I'm trying to download product stock as .csv file. File will download automatically once opening page is enabled with this below php script.
<?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();
After opening "product_stock.csv" downloaded file it is not properly aligned with expected rows value but shows html script on each rows and cloumns. How to download file product_stock.csv after clicking "Export" button and add expected value in rows?
Upvotes: 1
Views: 643
Reputation: 23
I got solution. Added form with button.
<form style="margin-left: 79%;margin-top: -59px;" method="post" id="export-form" action="">
<?php submit_button('Export Stock Report', 'primary', 'download_csv' ); ?>
</form>
Then added header to align correctly and this avoids html tags in product_stock.csv
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 );
Upvotes: 1