Add Snippet To Project
<?php
/**
* Plugin Name: Order Fulfillment
* Description: Sends order emails to fulfillment partners based on country and postcode.
* 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.
*
* @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
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);
}
Explanation:
This code creates a WordPress plugin that hooks into the woocommerce_order_status_processing action, which is triggered when an order status is set to "Processing" in WooCommerce. The wpturbo_send_order_email function is called when this action occurs.
Inside the wpturbo_send_order_email function, we retrieve the shipping address from the order object and extract the country and postcode. We then check the country and postcode conditions using if/elseif statements.
If the country is Germany and the postcode is between 94036 and 94038, we set the email address to "Postcode1@yachtstrip.de". If the country is Germany and the postcode is between 22299 and 22310, we set the email address to "Postcode2@yachtstrip.de". If the country is United Kingdom and the postcode is between L37 3JH and L39 4JH, we set the email address to "postcode3@yachtstrip.de". If none of these conditions match, we return and do not send an email.
Finally, we construct the email subject and content using the order ID, and then use the wp_mail function to send the order email to the fulfillment partner.
Remember to replace 'DE', 'GB', 'L37 3JH', 'L39 4JH', 'Postcode1@yachtstrip.de', 'Postcode2@yachtstrip.de', and 'postcode3@yachtstrip.de' with the actual values you want to use.
Please note that you should have WooCommerce plugin installed and activated for this code to work properly.
