Hello i have try to code my own plugin for wordpress and woocommerce but i have somes little problems : wen the user send the order
<?php
/**
* Plugin Name: WooCommerce Order to Excel
* Description: Replaces WooCommerce order emails with an Excel file attachment
* Version: 1.0
* Author: Marin
* Author URI: monsite.com
* License: GPL2
*/
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\IOFactory;
// Ajouter un filtre pour remplacer l'email de commande par un fichier Excel
add_filter( 'woocommerce_email_attachments', 'replace_order_email_with_excel', 10, 3 );
function replace_order_email_with_excel( $attachments, $email_id, $order ) {
if ( 'new_order' === $email_id && $order ) {
$excel_file = generate_excel_from_order( $order );
$attachments[] = $excel_file;
}
return $attachments;
}
function generate_excel_from_order( $order ) {
// Répertoire de sauvegarde
$dir = './backup/';
// Vérifier si le répertoire existe et est accessible en écriture
if (!is_dir($dir)) {
// Répertoire n'existe pas, le créer
if (!mkdir($dir, 0777, true)) {
trigger_error('Impossible de créer le répertoire de sauvegarde', E_USER_WARNING);
return false;
}
}
if (!is_writable($dir)) {
// Répertoire n'est pas accessible en écriture
trigger_error('Répertoire de sauvegarde non accessible en écriture', E_USER_WARNING);
return false;
}
$locale = get_locale();
switch ( $locale ) {
case 'fr_FR':
$template_file = 'template-fr.xlsx';
break;
case 'zh_CN':
$template_file = 'template-zh.xlsx';
break;
case 'en_US':
default:
$template_file = 'template-en.xlsx';
break;
}
// Créer un nouveau classeur
$spreadsheet = new Spreadsheet();
// Obtenir la première feuille
$sheet = $spreadsheet->getActiveSheet();
// Définir le numéro de commande
$sheet->setCellValue( 'G1', $order->get_order_number() );
// Définir la date de la commande
$sheet->setCellValue( 'A4', $order->get_date_created()->format( 'Y-m-d' ) );
// Définir les informations client
$sheet->setCellValue( 'C4', $order->get_billing_company() );
$sheet->setCellValue( 'A5', $order->get_billing_first_name() . ' ' . $order->get_billing_last_name() );
$sheet->setCellValue( 'C5', $order->get_billing_email() );
$sheet->setCellValue( 'E5', $order->get_billing_phone() );
$sheet->setCellValue( 'G5', $order->get_billing_address_1() . ' ' . $order->get_billing_address_2() );
$sheet->setCellValue( 'G6', $order->get_billing_city() . ', ' . $order->get_billing_state() . ' ' . $order->get_billing_postcode() );
$sheet->setCellValue( 'G7', $order->get_billing_country() );
// Obtenir les articles de la commande
$items = $order->get_items();
$row = 18;
foreach ( $items as $item ) {
$sheet->setCellValue( 'B' . $row, $item->get_name() );
$sheet->setCellValue( 'H' . $row, $item->get_quantity() );
$sheet->setCellValue( 'K' . $row, $item->get_total() );
// Add the serial number
$sheet->setCellValue( 'G' . $row, get_serial_number( $item ) );
$row++;
}
// Enregistrer le fichier Excel
$file_name = $dir . $order->get_order_number() . '.xlsx';
$writer = IOFactory::createWriter( $spreadsheet, 'Xlsx' );
$writer->save( $file_name );
return $file_name;
}
?>
I have coded a plugin to replace the email received at each order by the woocommerce administrator, I want to replace this email with an excel file generated with the order information using an excel file template that I put in the plugin folder
The problem is that when I place an order I have an error message in front-end as the order does not pass, but I still received a confirmation email but without the excel file.
I tried to debug but without really having any result, do you think my code is a problem?
I already installed the PHPspreadseeet library, manually but also with a plugin : CBX PhpSpreedSheet Library