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.
* 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.
*
* @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);
// Send the order email to the fulfillment partner
wp_mail($email_address, $subject, $content);
// Generate the Bill of Lading
$bill_of_lading = wpturbo_generate_bill_of_lading($order_id, $order);
// Save and attach the Bill of Lading to the order
wpturbo_save_bill_of_lading($order_id, $bill_of_lading);
}
/**
* Generates the Bill of Lading for the order.
*
* @param int $order_id Order ID.
* @param object $order Order object.
* @return string Generated Bill of Lading.
*/
function wpturbo_generate_bill_of_lading($order_id, $order) {
// Generate the Bill of Lading logic goes here
// Replace this with your own implementation
$bill_of_lading = "Bill of Lading for Order #" . $order_id . "n";
$bill_of_lading .= "-----------------------------n";
$bill_of_lading .= "Shipping Address:n";
$bill_of_lading .= "-----------------------------n";
$bill_of_lading .= $order->get_formatted_shipping_address();
return $bill_of_lading;
}
/**
* Saves and attaches the Bill of Lading to the order.
*
* @param int $order_id Order ID.
* @param string $bill_of_lading Bill of Lading content.
*/
function wpturbo_save_bill_of_lading($order_id, $bill_of_lading) {
// Save and attach the Bill of Lading to the order
$upload_dir = wp_upload_dir();
$file_path = $upload_dir['path'] . '/bill_of_lading_' . $order_id . '.txt';
// Save the Bill of Lading as a file
file_put_contents($file_path, $bill_of_lading);
// Attach the Bill of Lading 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' => 'text/plain',
'guid' => $upload_dir['url'] . '/bill_of_lading_' . $order_id . '.txt',
);
// 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, I added two new functions: wpturbo_generate_bill_of_lading and wpturbo_save_bill_of_lading.
The wpturbo_generate_bill_of_lading function is responsible for generating the content of the Bill of Lading. You can replace the logic inside this function with your own implementation to generate the actual Bill of Lading based on your requirements.
The wpturbo_save_bill_of_lading function saves the generated Bill of Lading as a file and attaches it to the order. It first saves the Bill of Lading content as a text file in the WordPress uploads directory. Then it creates an attachment post in the database with the file information, and associates it with the order by setting the post_parent to the order ID. Finally, it updates the order meta with the attachment ID for future reference.
Please note that you may need to further customize the code to suit your specific requirements for generating the Bill of Lading.
Remember to test the code thoroughly and make any necessary adjustments to fit your needs before deploying it on a live site.