将 Woocommerce 订单发送到产品页面上列出的电子邮件地址 [英] Send Woocommerce Order to email address listed on product page

查看:73
本文介绍了将 Woocommerce 订单发送到产品页面上列出的电子邮件地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Woocommerce 和 Product Addons 插件向产品添加额外的字段.其中一个字段是电子邮件地址,让人们将订单确认发送到与结帐页面上显示的帐单地址不同的地址.电子邮件应发送到两个地址.

I'm using Woocommerce and the Product Addons plugin to add extra fields to a product. One of those fields is an email address to let people send the confirmation of the order to a DIFFERENT address than the billing address shown on the checkout page. Emails should be sent to both addresses.

关于如何修改functions.php文件来做到这一点的任何想法?

Any thoughts on maybe how to modify the functions.php file to do this?

推荐答案

woocommerce_email_recipient_{$this->id} 过滤钩子中,可以使用 $order 参数以获取您的第二封电子邮件.

In the woocommerce_email_recipient_{$this->id} filter hook, you can use the $order argument to get your 2nd email.

但首先让我们使用 Product Add-ons 插件全局添加一个电子邮件字段......

But first Lets add globally an email field with the Product Add-ons plugin…

  1. 产品上的附加字段(填写该字段并添加到购物车):

  1. 结帐后,订单接收(谢谢)页面中的电子邮件"字段:

正如您所看到的,该字段的标签是电子邮件"...

As you can notice the label of this field is "Email"…

现在,如果我在 wp_woocommerce_order_itemmeta 中查看此订单的数据库,我可以看到 meta_key "Email"meta_value"loic@TheAztec.com" :

Now if I look in the database in wp_woocommerce_order_itemmeta for this order I can see for the meta_key "Email" the meta_value "loic@TheAztec.com" :

现在我可以在下面的代码中设置正确的 meta_key 来获取我的电子邮件.

Now I can set the correct meta_key in the code below to get my email.

以下代码将添加此额外的电子邮件收件人以处理和完成客户订单电子邮件通知:

Here is the code that will add this additional email recipient for processing and completed customer order email notifications:

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 订单发送到产品页面上列出的电子邮件地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆