Add Snippet To Project
<?php
/**
* Plugin Name: Order Fulfillment
* Description: Sends order emails to fulfillment partners based on country and postcode and generates a Bill of Lading in PDF format.
* Version: 1.0
* Author: WP Turbo
* Author URI: https://wpturbo.dev
* Text Domain: wpturbo
*/
// Hook into the order status transition
add_action('woocommerce_order_status_processing', 'wpturbo_send_order_email', 10, 2);
/**
* Sends order email to fulfillment partner based on country and postcode
* and generates a Bill of Lading in PDF format.
*
* @param int $order_id Order ID.
* @param object $order Order object.
*/
function wpturbo_send_order_email($order_id, $order) {
// Get the order shipping address
$shipping_address = $order->get_address('shipping');
// Get the country and postcode from the shipping address
$country = $shipping_address['country'];
$postcode = $shipping_address['postcode'];
// Check country and postcode conditions and send email accordingly
if ($country === 'DE' && $postcode >= '94036' && $postcode <= '94038') {
$email_address = 'Postcode1@yachtstrip.de';
} elseif ($country === 'DE' && $postcode >= '22299' && $postcode <= '22310') {
$email_address = 'Postcode2@yachtstrip.de';
} elseif ($country === 'GB' && $postcode >= 'L37 3JH' && $postcode <= 'L39 4JH') {
$email_address = 'postcode3@yachtstrip.de';
} else {
// No matching conditions, do not send email or generate Bill of Lading
return;
}
// Get the order email subject and content
$subject = sprintf(__('Order #%d - Fulfillment Partner Notification', 'wpturbo'), $order_id);
$content = sprintf(__('Order #%d has been processed and needs to be fulfilled.', 'wpturbo'), $order_id);
// Generate the Bill of Lading in PDF format
$bill_of_lading = wpturbo_generate_bill_of_lading($order_id, $order);
// Save and attach the Bill of Lading PDF to the order
wpturbo_save_bill_of_lading($order_id, $bill_of_lading);
// Send the order email with the attached PDF
wp_mail($email_address, $subject, $content, array($bill_of_lading['path']), array('Content-Type: application/pdf'));
}
/**
* Generates the Bill of Lading for the order in PDF format.
*
* @param int $order_id Order ID.
* @param object $order Order object.
* @return array Generated Bill of Lading in PDF format.
*/
function wpturbo_generate_bill_of_lading($order_id, $order) {
// Generate the Bill of Lading logic goes here
// Replace this with your own implementation to generate the PDF
// Example using the TCPDF library to generate the PDF
require_once('tcpdf/tcpdf.php');
// Create new PDF document
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
// Set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('WP Turbo');
$pdf->SetTitle('Bill of Lading - Order #' . $order_id);
$pdf->SetSubject('Bill of Lading - Order #' . $order_id);
$pdf->SetKeywords('Bill of Lading, Order');
// Set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE . ' ' . $order_id, PDF_HEADER_STRING);
// Set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));
// Set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
// Set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
// Set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);
// Set font
$pdf->SetFont('dejavusans', '', 10);
// Add a page
$pdf->AddPage();
// Generate the content of the Bill of Lading
$pdf->Cell(0, 10, 'Bill of Lading for Order #' . $order_id, 0, 1, 'C');
$pdf->Cell(0, 10, '-----------------------------', 0, 1, 'C');
$pdf->Cell(0, 10, 'Shipping Address:', 0, 1, 'C');
$pdf->Cell(0, 10, '-----------------------------', 0, 1, 'C');
$pdf->MultiCell(0, 10, $order->get_formatted_shipping_address(), 0, 'L');
// Close and output PDF
$pdf->Output('bill_of_lading_' . $order_id . '.pdf', 'I');
// Return the path of the generated PDF
$pdf_path = $pdf->getOutputFromMemory();
$pdf_temp_path = tempnam(sys_get_temp_dir(), 'bill_of_lading_');
file_put_contents($pdf_temp_path, $pdf_path);
return array(
'path' => $pdf_temp_path,
'name' => 'bill_of_lading_' . $order_id . '.pdf',
);
}
/**
* Saves and attaches the Bill of Lading PDF to the order.
*
* @param int $order_id Order ID.
* @param array $bill_of_lading Bill of Lading PDF information.
*/
function wpturbo_save_bill_of_lading($order_id, $bill_of_lading) {
// Save and attach the Bill of Lading PDF to the order
$upload_dir = wp_upload_dir();
$file_path = $upload_dir['path'] . '/' . $bill_of_lading['name'];
// Move the temporary PDF file to the uploads directory
rename($bill_of_lading['path'], $file_path);
// Attach the Bill of Lading PDF to the order
$attachment = array(
'post_title' => 'Bill of Lading - Order #' . $order_id,
'post_content' => '',
'post_type' => 'attachment',
'post_parent' => $order_id,
'post_mime_type' => 'application/pdf',
'guid' => $upload_dir['url'] . '/' . $bill_of_lading['name'],
);
// Insert the attachment
$attachment_id = wp_insert_attachment($attachment, $file_path, $order_id);
// Update the order meta with the attachment ID
update_post_meta($order_id, '_bill_of_lading_id', $attachment_id);
}
Explanation:
In this updated code, we have added functionality to generate a PDF of the Bill of Lading and attach it to the order email.
To generate the PDF, we are using the TCPDF library, which is a popular PHP library for creating PDF documents. You will need to download the TCPDF library and include it in your plugin directory.
The wpturbo_generate_bill_of_lading function has been updated to use TCPDF to generate the PDF. We specify the document information, set the header and footer fonts, set margins, and add a page to the PDF. Then we generate the content of the Bill of Lading by using the TCPDF Cell and MultiCell methods. Finally, we close and output the PDF.
The generated PDF is saved temporarily using tempnam and then moved to the WordPress uploads directory in the wpturbo_save_bill_of_lading function. The PDF is attached to the order using the wp_insert_attachment function, and the attachment ID is stored in the order meta.
When sending the order email, we use the wp_mail function and include the PDF as an attachment by passing the path to the PDF file in the attachments parameter. We also specify the content type as application/pdf.
Please note that you will need to download and include the TCPDF library in your plugin directory for this code to work. Additionally, make sure to test the code thoroughly and make any necessary adjustments to fit your specific requirements.
If you have any further questions, please let me know!