Woocommerce订单完成时向订阅用户发邮件及后台输入字段配置需求
Hey there! I’ve built something similar for clients before, so let’s break this down into two straightforward parts: adding custom admin fields to edit your email content, and hooking into WooCommerce’s order completion event to send those emails to all Subscriber users.
First, we’ll add a new settings page under WordPress’s Settings menu where you can edit the email subject, greeting, and body without touching code later. Here’s the code to drop into your theme’s functions.php file (or a custom plugin for better portability):
// Add admin menu page for order completion email settings add_action('admin_menu', 'add_order_completion_email_settings_page'); function add_order_completion_email_settings_page() { add_submenu_page( 'options-general.php', // Parent menu (Settings) 'Order Completion Email Settings', // Page title 'Order Completion Emails', // Menu item text 'manage_options', // Capability required to access 'order-completion-emails', // Menu slug 'render_order_completion_email_settings_page' // Callback to render the page ); } // Register our settings with WordPress add_action('admin_init', 'register_order_completion_email_settings'); function register_order_completion_email_settings() { register_setting('order-completion-email-group', 'oce_email_subject'); register_setting('order-completion-email-group', 'oce_email_greeting'); register_setting('order-completion-email-group', 'oce_email_body'); } // Render the actual settings form function render_order_completion_email_settings_page() { ?> <div class="wrap"> <h1>Order Completion Email Settings</h1> <form method="post" action="options.php"> <?php settings_fields('order-completion-email-group'); do_settings_sections('order-completion-email-group'); ?> <table class="form-table"> <tr valign="top"> <th scope="row">Email Subject</th> <td><input type="text" name="oce_email_subject" value="<?php echo esc_attr(get_option('oce_email_subject', 'New Order Completed!')); ?>" class="regular-text" /></td> </tr> <tr valign="top"> <th scope="row">Email Greeting</th> <td><input type="text" name="oce_email_greeting" value="<?php echo esc_attr(get_option('oce_email_greeting', 'Hi [user_name],')); ?>" class="regular-text" /></td> </tr> <tr valign="top"> <th scope="row">Email Body</th> <td><textarea name="oce_email_body" rows="5" cols="50" class="large-text"><?php echo esc_textarea(get_option('oce_email_body', 'A new order has been completed on our site. Check it out!')); ?></textarea></td> </tr> </table> <?php submit_button(); ?> </form> </div> <?php }
Once you add this, you’ll see a new Order Completion Emails option under the Settings menu in your WP admin. You can edit the three fields here, and WordPress will save them as options automatically.
Next, we’ll hook into WooCommerce’s woocommerce_order_status_completed action, which fires when an order is marked as completed. We’ll fetch all Subscriber users, pull the email content from our settings, and send the email to each one:
// Trigger email send when order is marked completed add_action('woocommerce_order_status_completed', 'send_email_to_subscribers_on_order_completion', 10, 1); function send_email_to_subscribers_on_order_completion($order_id) { // Get the full order object (useful if you want to include order details) $order = wc_get_order($order_id); // Fetch all users with the Subscriber role $subscribers = get_users(array( 'role' => 'subscriber', 'fields' => array('ID', 'user_email', 'display_name') // Only fetch what we need )); // Pull our saved email settings from the admin $subject = get_option('oce_email_subject', 'New Order Completed!'); $greeting = get_option('oce_email_greeting', 'Hi [user_name],'); $body = get_option('oce_email_body', 'A new order has been completed on our site. Check it out!'); // Loop through each subscriber and send personalized email foreach ($subscribers as $subscriber) { // Replace the [user_name] placeholder with the subscriber's actual name $personalized_greeting = str_replace('[user_name]', $subscriber->display_name, $greeting); $personalized_body = str_replace('[user_name]', $subscriber->display_name, $body); // Optional: Add order-specific details to the email $personalized_body .= "\n\nOrder ID: " . $order_id; $personalized_body .= "\nOrder Total: " . $order->get_formatted_order_total(); // Send the email using WordPress's native wp_mail function wp_mail( $subscriber->user_email, $subject, $personalized_greeting . "\n\n" . $personalized_body, array('Content-Type: text/plain; charset=UTF-8') ); } }
- Test First: After adding the code, head to Settings > Order Completion Emails to save your preferred text, then create a test order and mark it as completed. Check your subscribers’ inboxes to ensure everything works.
- Email Deliverability: WordPress’s default email system can be hit-or-miss. For better deliverability, use a plugin like WP Mail SMTP to route emails through a service like Gmail, SendGrid, or Mailgun.
- Customize Placeholders: Want to add more dynamic content? You can add placeholders like
[order_id]or[order_total]in the admin fields, then expand thestr_replace()logic in the email function to replace them with actual order data. - Target Other Roles: If you need to send emails to other user roles, just change
'role' => 'subscriber'in theget_users()call to the role slug you want (e.g.,'customer','editor').
内容的提问来源于stack exchange,提问作者Mukhyyar

