当您进入 WooCommerce->设置->电子邮件时,您可以设置用于新订单、失败订单、取消订单等的电子邮件地址。
我正在尝试了解如何以编程方式更改所有这些电子邮件,因此我创建了一个带有输入字段和提交按钮的小模板,以一次更改所有电子邮件,但唯一更改的电子邮件地址是电子邮件地址发件人选项(WooCommerce->设置->电子邮件页面底部的设置),而不是新订单、取消订单和失败订单的选项。
检查可能的钩子,我认为以下代码应该可以解决问题:
function change_notification_email_callback() {
// Log form data for debugging
error_log( print_r( $_POST, true ) );
if ( isset( $_POST['new_notification_email'] ) && ! empty( $_POST['new_notification_email'] ) ) {
$new_email = sanitize_email( $_POST['new_notification_email'] );
// Log current value of woocommerce_new_order_recipient option
$old_recipient_email = get_option( 'woocommerce_new_order_recipient' );
error_log( 'Old Recipient Email: ' . $old_recipient_email );
// Update the notification email
update_option( 'woocommerce_email_from_address', $new_email );
update_option( 'woocommerce_new_order_email', $new_email );
update_option( 'woocommerce_cancelled_order_email', $new_email );
update_option( 'woocommerce_failed_order_email', $new_email );
// Log updated value of woocommerce_new_order_recipient option
$updated_recipient_email = get_option( 'woocommerce_new_order_recipient' );
error_log( 'Updated Recipient Email: ' . $updated_recipient_email );
// Log updated value of woocommerce_cancelled_order_recipient option
$updated_recipient_email_cancelled = get_option( 'woocommerce_cancelled_order_recipient' );
error_log( 'Updated Recipient Email for cancelled: ' . $updated_recipient_email_cancelled );
// Set success message
$message = 'Notification email changed successfully.';
$notice_type = 'success';
} else {
// Set error message
$message = 'Failed to change notification email. Please enter a valid email.';
$notice_type = 'error';
}
// Set admin notice
add_flash_notice( $message, $notice_type );
// Redirect back to the WooCommerce dashboard page after email change
wp_redirect( admin_url( 'admin.php?page=wc-dashboard' ) );
exit;
}
再次强调,在 // 更新通知电子邮件下,只有第一个 update_option 有效,即使日志显示所有实例都已更改(woocommerce_email_from_address、woocommerce_new_order_email 等),甚至在 wp_options 表中查找这些字段,它们也显示已更新,但是当我检查 WooCommerce->设置->电子邮件页面时,这些仍然没有变化(下图显示了我的意思)。我做了一个测试来下订单,以确保它不是缓存或其他东西,但它会发送到那里的任何电子邮件。有谁知道我还可以尝试什么?
您没有使用正确的“新订单”、“已取消订单”和“失败订单”选项键。对于这 3 个选项,每个选项都是一组设置……
重要提示:您将需要仅一次,为这 3 个通知中的每一个手动设置一封新电子邮件并更新以创建设置选项。完成后,您的数据库中的 wp_options 下将包含以下内容:
完成后,您可以使用以下修改后的代码:
function change_notification_email_callback() {
if ( isset( $_POST['new_notification_email'] ) && ! empty( $_POST['new_notification_email'] ) ) {
$new_email = sanitize_email( $_POST['new_notification_email'] );
// Array of option keys for email settings
$email_setting_keys = array(
'woocommerce_new_order_settings' => __('New order'),
'woocommerce_cancelled_order_settings' => __('Cancelled order'),
'woocommerce_failed_order_settings' => __('Failed order'),
);
$updated = array(); // Initializing
// Loop through the emails settings keys
foreach ( $email_setting_keys as $key => $label ) {
$settings = get_option( $key, array() ); // Get option settings array for the current notification
if ( ! empty($settings) && isset($settings['recipient']) ) {
$settings['recipient'] = $new_email; // change recipient with the new email
update_option( $key, $settings ); // Update/save settings for the current notification
$updated[] = $label; // Tag as updated
}
}
// Update the "from Address" email
update_option( 'woocommerce_email_from_address', $new_email );
$updated[] = __('From Address'); // Tag as updated
// Set success message
$message = sprintf( __('Email changed successfully for "%s" notifications.' ), implode('", "', $updated) );
$notice_type = 'success';
} else {
// Set error message
$message = __('Failed to change notification email. Please enter a valid email.');
$notice_type = 'error';
}
add_flash_notice( $message, $notice_type ); // Set admin notice
// Redirect back to the WooCommerce dashboard page after email change
wp_redirect( admin_url( 'admin.php?page=wc-dashboard' ) );
exit();
}
代码位于子主题的functions.php 文件中(或插件中)。应该可以。