I’m trying to make that with each new order that is received, the export of the plugin is generated automatically with the default settings that already has (in this case a pdf with a php code to configure the output), but I can’t make the sending of the same.
I tried this code but I can’t get it to send, and it’s clearly beyond my level.
add_action( 'woocommerce_new_order', 'new_order_export_and_email' );
function new_order_export_and_email( $order_id ) {
// Check if the "WooCommerce Order Export Lite" plugin is installed and active
if ( ! class_exists( 'WOO_Order_Export_Lite' ) ) {
return;
}
// Generate the PDF export using the plugin's default settings
$exporter = new WOO_Order_Export_Lite();
$export_data = $exporter->export_order( $order_id );
// Prepare the PDF file as an attachment
$tempfile = tmpfile();
fwrite( $tempfile, $export_data );
$attachment = stream_get_meta_data( $tempfile )['uri'];
// Prepare the email details
$subject = "Nuevo pedido de WooCommerce - #" . $order_id . '"';
$message = "Se ha generado un nuevo pedido en su tienda WooCommerce.nn";
$message .= "Detalles del pedido:n";
$message .= $export_data; // Include export data in the email body (optional)
// Configure the email sending
$to_email = "[email protected]"; // Replace with the recipient email address
$from_email = "[email protected]"; // Replace with the store's email address
$headers = "From: {$from_email}n";
// Send the email with the PDF attachment
wp_mail( $to_email, $subject, $message, $headers, array( $attachment => 'pedido-' . $order_id . '.pdf' ) );
// Close and delete the temporary file
fclose( $tempfile );
}