将 Woocommerce 订单发送到产品页面上列出的电子邮件地址
Send Woocommerce Order to email address listed on product page
我正在使用 Woocommerce 和 Product Addons 插件向产品添加额外的字段。其中一个字段是一个电子邮件地址,让人们可以将订单确认发送到与结帐页面上显示的账单地址不同的地址。电子邮件应发送到两个地址。
关于如何修改 functions.php 文件以执行此操作有任何想法吗?
您可以在 function.php
中添加以下代码
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'your_email_recipient_filter_function', 10, 2);
function your_email_recipient_filter_function($recipient, $object) {
$recipient = $recipient . ', me@myemail.com';
return $recipient;
}
如果您想使用 BCC 发送电子邮件,请尝试以下代码:
add_filter( 'woocommerce_email_headers', 'mycustom_headers_filter_function', 10, 2);
function mycustom_headers_filter_function( $headers, $object ) {
if ($object == 'customer_completed_order') {
$headers .= 'BCC: My name <my@email.com>' . "\r\n";
}
在 woocommerce_email_recipient_{$this->id}
过滤器挂钩中,您可以使用 $order
参数来获取您的第二封电子邮件。
但首先让我们使用 Product Add-ons 插件全局添加一个电子邮件字段……
- 产品上的添加字段(填写字段并添加到购物车):
- 订单收到(谢谢)页面中的这个 "Email" 字段,结帐后:
您可以注意到该字段的标签是 "Email"…
现在,如果我在 wp_woocommerce_order_itemmeta
的数据库中查找此订单,我可以看到 meta_key "Email"
meta_value "loic@TheAztec.com"
:
现在我可以在下面的代码中设置正确的 meta_key
来接收我的电子邮件。
下面是添加此额外电子邮件收件人以处理和完成客户订单电子邮件通知的代码:
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'additional_customer_email_recipient', 10, 2 ); // Processing Order
add_filter( 'woocommerce_email_recipient_customer_processing_order', 'additional_customer_email_recipient', 10, 2 ); // Completed Order
function additional_customer_email_recipient( $recipient, $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
$additional_recipients = array(); // Initializing…
// Iterating though each order item
foreach( $order->get_items() as $item_id => $item_data ){
// HERE set the the correct meta_key (like 'Email') to get the correct value
$email = wc_get_order_item_meta( $item_id, 'Email', true );
// Avoiding duplicates (if many items with many emails)
// or an existing email in the recipient
if( ! in_array( $email, $additional_recipients ) && strpos( $recipient, $email ) === false )
$additional_recipients[] = $email;
}
// Convert the array in a coma separated string
$additional_recipients = implode( ',', $additional_recipients);
// If an additional recipient exist, we add it
if( count($additional_recipients) > 0)
$recipient .= ','.$additional_recipients;
return $recipient;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
已测试并有效。
我正在使用 Woocommerce 和 Product Addons 插件向产品添加额外的字段。其中一个字段是一个电子邮件地址,让人们可以将订单确认发送到与结帐页面上显示的账单地址不同的地址。电子邮件应发送到两个地址。
关于如何修改 functions.php 文件以执行此操作有任何想法吗?
您可以在 function.php
中添加以下代码add_filter( 'woocommerce_email_recipient_customer_completed_order', 'your_email_recipient_filter_function', 10, 2);
function your_email_recipient_filter_function($recipient, $object) {
$recipient = $recipient . ', me@myemail.com';
return $recipient;
}
如果您想使用 BCC 发送电子邮件,请尝试以下代码:
add_filter( 'woocommerce_email_headers', 'mycustom_headers_filter_function', 10, 2);
function mycustom_headers_filter_function( $headers, $object ) {
if ($object == 'customer_completed_order') {
$headers .= 'BCC: My name <my@email.com>' . "\r\n";
}
在 woocommerce_email_recipient_{$this->id}
过滤器挂钩中,您可以使用 $order
参数来获取您的第二封电子邮件。
但首先让我们使用 Product Add-ons 插件全局添加一个电子邮件字段……
- 产品上的添加字段(填写字段并添加到购物车):
- 订单收到(谢谢)页面中的这个 "Email" 字段,结帐后:
您可以注意到该字段的标签是 "Email"…
现在,如果我在 wp_woocommerce_order_itemmeta
的数据库中查找此订单,我可以看到 meta_key "Email"
meta_value "loic@TheAztec.com"
:
现在我可以在下面的代码中设置正确的 meta_key
来接收我的电子邮件。
下面是添加此额外电子邮件收件人以处理和完成客户订单电子邮件通知的代码:
add_filter( 'woocommerce_email_recipient_customer_completed_order', 'additional_customer_email_recipient', 10, 2 ); // Processing Order
add_filter( 'woocommerce_email_recipient_customer_processing_order', 'additional_customer_email_recipient', 10, 2 ); // Completed Order
function additional_customer_email_recipient( $recipient, $order ) {
if ( ! is_a( $order, 'WC_Order' ) ) return $recipient;
$additional_recipients = array(); // Initializing…
// Iterating though each order item
foreach( $order->get_items() as $item_id => $item_data ){
// HERE set the the correct meta_key (like 'Email') to get the correct value
$email = wc_get_order_item_meta( $item_id, 'Email', true );
// Avoiding duplicates (if many items with many emails)
// or an existing email in the recipient
if( ! in_array( $email, $additional_recipients ) && strpos( $recipient, $email ) === false )
$additional_recipients[] = $email;
}
// Convert the array in a coma separated string
$additional_recipients = implode( ',', $additional_recipients);
// If an additional recipient exist, we add it
if( count($additional_recipients) > 0)
$recipient .= ','.$additional_recipients;
return $recipient;
}
代码进入您的活动子主题(或主题)的 function.php 文件或任何插件文件。
已测试并有效。