foreach ($orders as $order) {
 $billing\_first\_name = $order->get\_billing\_first\_name();
 $billing\_last\_name = $order->get\_billing\_last\_name();
 $discount = $order->get\_discount\_total();
 $taxes = [];
 $items = $order->get\_items();
 foreach ($order->get\_taxes() as $tax) {
 $taxes[] =
 $tax->get\_name() . ": " . wc\_price($tax->get\_amount());
 }
 foreach ($items as $item\_id => $item) {
 $variation\_data = $item->get\_variation\_attributes();
 $product = $item->get\_product();
 $product\_name = $product->get\_name();
 $item\_total = $item->get\_total();
 $item\_subtotal = $item->get\_subtotal();
 $item\_tax = $item->get\_total\_tax();
 $item\_qty = $item->get\_quantity();
 $rows[] = [
 $order->get\_date\_created()->format("d/m/Y"),
 get\_post\_meta(
 $order->get\_id(),
 "\_wcpdf\_invoice\_number",
 true
 ),
 "",
 $billing\_first\_name .
 " " .
 $billing\_last\_name .
 ", " .
 $product\_name .
 " " .
 implode(", ", $variation\_data),
 "",
 wc\_price($item\_total + $item\_tax),
 wc\_price(abs($item\_total + $item\_tax)),
 ];
 }
 foreach ($order->get\_items("fee") as $item\_id => $item) {
 $item\_total = $item->get\_total();
 $item\_tax = $item->get\_total\_tax();
 if ($item->get\_name() === "Remise") {
 $rows[] = [
 $order->get\_date\_created()->format("d/m/Y"),
 get\_post\_meta(
 $order->get\_id(),
 "\_wcpdf\_invoice\_number",
 true
 ),
 "",
 $item->get\_name(),
 wc\_price(abs($item\_total)),
 "",
 wc\_price(abs($item\_total)),
 ];
 }
 }
 foreach ($order->get\_items("tax") as $item\_id => $item) {
 $tax\_name = $item->get\_name();
 $tax\_total = $item->get\_total();
 if ($tax\_name !== "TVA") {
 $rows[] = [
 $order->get\_date\_created()->format("d/m/Y"),
 get\_post\_meta(
 $order->get\_id(),
 "\_wcpdf\_invoice\_number",
 true
 ),
 "",
 $tax\_name,
 "",
 wc\_price(abs($tax\_total)),
 wc\_price(abs($tax\_total)),
 ];
 }
 }
 }

 $file = fopen("php://output", "w");
 fwrite($file, "\xEF\xBB\xBF"); // ajouter l'encodage UTF-8 BOM
 fputcsv($file, $headers);

 foreach ($rows as $row) {
 fputcsv($file, $row);
 }

 fclose($file);
 exit();
 }
}

add\_action("init", "export\_csv");
user