首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >将产品自定义字段中的收件人添加到Woocommerce新订单电子邮件通知

将产品自定义字段中的收件人添加到Woocommerce新订单电子邮件通知
EN

Stack Overflow用户
提问于 2018-04-19 08:33:17
回答 1查看 835关注 0票数 1

在Woocommerce中,我想添加一个带有高级自定义字段插件的电子邮件自定义字段到产品帖子类型。

如果客户下订单,我想添加每个订单项目的相应电子邮件地址到新订单电子邮件通知。

如何将产品自定义字段中的收件人添加到Woocommerce新订单电子邮件通知中?

EN

回答 1

Stack Overflow用户

发布于 2018-04-20 01:08:29

对于"New order“电子邮件通知,下面是从订单中的项目添加自定义电子邮件的方法(电子邮件自定义字段在产品中设置为ACF )。

因此,您将首先在ACF中为"product“帖子类型设置一个自定义字段:

然后,您将在后端产品编辑页面中:

完成后,当产品自定义字段都设置了电子邮件地址时,您将使用此代码将订单中每个相应项目的电子邮件添加到"New order“通知中:

代码语言:javascript
运行
复制
add_filter( 'woocommerce_email_recipient_new_order', 'add_item_email_to_recipient', 10, 2 );
function add_item_email_to_recipient( $recipient, $order ) {
    if( is_admin() ) return $recipient;

    $emails = array();

    // Loop though  Order IDs
    foreach( $order->get_items() as $item_id => $item ){
        // Get the student email
        $email = get_field( 'product_email', $item->get_product_id() );
        if( ! empty($email) )
            $emails[] = $email; // Add email to the array
    }

    // If any student email exist we add it
    if( count($emails) > 0 ){
        // Remove duplicates (if there is any)
        $emails = array_unique($emails);
        // Add the emails to existing recipients
        $recipient .= ',' . implode( ',', $emails );
    }
    return $recipient;
}

代码放在活动子主题(或活动主题)的function.php文件中。经过测试,效果良好。

相关:Send Woocommerce Order to email address listed on product page

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/49910924

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档