【问题标题】:Add recipients from product custom fields to Woocommerce new order email notification将产品自定义字段中的收件人添加到 Woocommerce 新订单电子邮件通知
【发布时间】:2018-04-19 00:33:16
【问题描述】:

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

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

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

【问题讨论】:

    标签: php wordpress woocommerce advanced-custom-fields email-notifications


    【解决方案1】:

    对于“新订单”电子邮件通知,这是从订单中的项目添加自定义电子邮件的方法(电子邮件自定义字段在产品中使用 ACF 设置)。

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

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

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

    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

    【讨论】:

    • 感谢您提供此代码,代码运行良好。但我想要这个 mai 抄送
    • @KapilGoyal 要使用 CC 做到这一点,您需要使用另一个钩子,例如在这个答案线程中:stackoverflow.com/questions/49782495/… ... 因为这个答案无论如何都很有用并且有效,如果你喜欢/想要你可以@987654325 @,谢谢。
    • 感谢@Loic TheAztec 提供了将订单电子邮件通知发送到自定义字段电子邮件 ID 的出色解决方案。这对我来说是完美的工作。
    猜你喜欢
    • 2021-01-29
    • 2022-01-10
    • 1970-01-01
    • 2019-04-25
    • 2020-12-07
    • 1970-01-01
    • 2022-01-14
    • 1970-01-01
    相关资源
    最近更新 更多