Skip to content

Instantly share code, notes, and snippets.

@goranefbl
Created December 8, 2024 13:09
Show Gist options
  • Save goranefbl/1288257c1f6e0237ee3a7183f88f2a9e to your computer and use it in GitHub Desktop.
Save goranefbl/1288257c1f6e0237ee3a7183f88f2a9e to your computer and use it in GitHub Desktop.
UTM Friends Email notification
<?php
add_action('woocommerce_checkout_order_processed', 'send_custom_order_email', 10, 1);
function send_custom_order_email($order_id) {
$order = wc_get_order($order_id);
$utm_source = get_post_meta($order_id, '_wpg_first_source', true);
// Only send email if utm_source exists and is an email address
if (!empty($utm_source) && filter_var($utm_source, FILTER_VALIDATE_EMAIL)) {
$to = $utm_source; // Send to the friend's email (utm_source)
$subject = 'Great news! Your referral just made a purchase!';
$body = "Hello Friend,\n\n";
$body .= "Exciting news! Someone just made a purchase using your promotional link. Here are the details:\n\n";
$body .= "Order Number: #" . $order_id . "\n";
$body .= "Order Total: " . $order->get_total() . " " . $order->get_currency() . "\n";
$body .= "Date: " . $order->get_date_created()->format('F j, Y, g:i a') . "\n\n";
$body .= "This purchase will be added to your commission. Keep up the great work!\n\n";
$body .= "Thank you for your continued support in promoting our store. Your efforts are truly appreciated.\n\n";
$body .= "Best regards,\n";
$body .= "The " . get_bloginfo('name') . " Team";
wp_mail($to, $subject, $body);
// Optional: Send a copy to yourself
$admin_email = get_option('admin_email');
wp_mail($admin_email, "Copy: " . $subject, $body);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment