<?php
// Definir cabeçalhos para a codificação correta
header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=exported_data.csv');
$fileName = $orderId . ".csv";
$file = fopen($fileName, "w+"); // Added + for read/write mode.
// Escreve o BOM (Byte Order Mark) para indicar UTF-8
fputs($file, chr(0xEF) . chr(0xBB) . chr(0xBF));
// Write the header row
$headerRow = ["ID", "Item", "Price", "Amount", "Total", "Description"];
fputcsv($file, $headerRow);
// Loop over each item in the $items array
foreach ($items as $item) {
// Build an array containing information about the item
$itemArray = [
$item->ID,
$item->Name,
$item->Price . $currency,
$item->Amount,
$item->Price * $item->Amount . $currency,
$item->Description
];
// Write the item information to the file
fputcsv($file, $itemArray);
}
// Write the total row
$totalRow = ["Grand total", "", "", $totalAmount, $total . $currency, ""];
fputcsv($file, $totalRow);
// Close the file
fclose($file);
$fileUrl = "https://" . $_SERVER['HTTP_HOST'] . "/" . $fileName;
preferences:
19.26 ms | 408 KiB | 5 Q