In a recent Business Bloomer Club Slack thread, a developer working with the woocommerce_payment_complete
hook encountered an issue where the “Processing order” email was sent with outdated order data.
While the order edits were correctly saved before external processing, WooCommerce sent the email using pre-edited data.
Here’s how to ensure that customers receive emails with accurate order details by customizing email triggers.
1. Understanding the woocommerce_payment_complete
Hook Behavior
WooCommerce triggers the “Processing order” email as soon as the order status changes to “Processing.” In core WooCommerce, this happens before the woocommerce_payment_complete
hook, causing the email to include data from before any custom adjustments are applied to the order.
To address this, you can either:
- Disable the default “Processing order” email and trigger a custom email after completing the edits, or
- Use an alternative hook to make changes before the email is sent, though this can be more challenging if payment completion fails.
2. Solution 1: Disable the Default “Processing Order” Email and Send a Custom Email
If the default “Processing order” email isn’t essential, you can disable it and then trigger a custom email after completing the edits. Here’s how:
- Disable the Default Email: Go to WooCommerce > Settings > Emails and disable the “Processing order” email.
- Send a Custom Email After Edits: Use the same
woocommerce_payment_complete
hook to trigger a custom email after your adjustments are saved.
Example code to trigger a custom “Processing order” email:
add_action( 'woocommerce_payment_complete', 'custom_processing_email_after_edits', 20, 1 );
function custom_processing_email_after_edits( $order_id ) {
$order = wc_get_order( $order_id );
// Perform necessary edits to the order
// Example: $order->set_shipping_total(10);
$order->save();
// Send custom email using WooCommerce email classes
WC()->mailer()->emails['WC_Email_Customer_Processing_Order']->trigger( $order_id );
}
This approach ensures the “Processing order” email reflects your updates, as it’s sent only after the edits are complete.
3. Solution 2: Use the woocommerce_pre_payment_complete
Hook for Pre-Email Changes
If you need to avoid sending custom emails manually, try making your edits earlier, with the woocommerce_pre_payment_complete
hook, which runs before WooCommerce triggers the “Processing order” email.
Example:
add_action( 'woocommerce_pre_payment_complete', 'adjust_order_before_email', 10, 1 );
function adjust_order_before_email( $order_id ) {
$order = wc_get_order( $order_id );
// Apply necessary adjustments to the order
$order->set_shipping_total(10); // Example change
$order->save();
}
- Pros: Allows WooCommerce to handle the “Processing order” email automatically, ensuring it contains the updated order data.
- Cons: If payment fails, adjustments will already be applied, which may not be ideal.
Conclusion
By disabling the default email and sending a custom email with updated data, or adjusting the order earlier with woocommerce_pre_payment_complete
, you can ensure customers receive accurate information. Both approaches give you flexibility to tailor the order process without compromising the order email’s integrity.