woocommerce-subscriptions - 如何在 Woocommerce 订阅中将客户续订订单发送到辅助电子邮件

我想将续订订单电子邮件发送到辅助用户电子邮件(我已使用 ACF 在用户编辑页面中添加)。

我尝试了很多方法,woocommerce_subscription_payment_complete 也不适合我。

我尝试过以下代码:

add_action( 'woocommerce_order_status_completed', 'action_on_order_status_completed', 20, 2 );


 function action_on_order_status_completed( $order_id, $order ){

    $order = new WC_Order($order_id);

    // Get the user ID from WC_Order methods
    $user_id = $order->get_user_id(); // or $order->get_customer_id();

    $secondary_recipient = get_field('secondary_email', 'user_'.$user_id );

            $subscriptions_ids = wcs_get_subscriptions_for_order( $order_id, array( 'order_type' => 'any' ) );
    // We get all related subscriptions for this order

    foreach( $subscriptions_ids as $subscription_id => $subscription_obj )
        if($subscription_obj->order->id == $order_id) break; // Stop the loop

        // $subscription_objc = wcs_get_subscription($subscription_id);
        //$userid = $subscription_objc->get_user_id();

    $wc_emails = WC()->mailer()->get_emails();
    $wc_emails['WCS_Email_Processing_Renewal_Order']->recipient =  $secondary_recipient;
    $wc_emails['WCS_Email_Processing_Renewal_Order']->trigger($subscription_id);

    // $to = $secondary_recipient;
    //  $subject = "hi";
    //  $body =$user_id."end".$order_id."hhh".$subscription_id;
        
    //  $headers = array('Content-Type: text/html; charset=UTF-8');
    //  //$headers[] = 'Cc: sarun@cloudspring.in';
    //  wp_mail( $to, $subject, $body, $headers );

   }

仅供参考:如果我使用带注释的 wp_mail 功能,则会发送电子邮件。

回答1

我们可以添加一个辅助电子邮件作为收件人,尝试下面的代码测试并且它有效。

add_filter( 'woocommerce_email_recipient_customer_completed_renewal_order', 'my_email_recipient_filter_function', 10, 2);

function my_email_recipient_filter_function( $recipient, $order ) {
$user_id = $order->get_user_id(); // or $order->get_customer_id();
$secondary_recipient = get_field('secondary_email', 'user_'.$user_id ); 
if(! empty($secondary_recipient)){
$recipient = $recipient   . ', '. $secondary_recipient;
return $recipient;
}else {
return $recipient;    
}
}

相似文章