结帐后使用 WooCommerce 计费电子邮件更新 WordPress 帐户电子邮件

问题描述

成功结帐后,我需要使用 woocommerce 帐单电子邮件更新 WordPress 帐户电子邮件。我使用了这段代码,但它不起作用:

/* Update account email based on woocommerce billing email */


add_filter( 'woocommerce_thankyou','custom_update_checkout_fields',10,2 );

 function custom_update_checkout_fields($user_id,$old_user_data ) {
  $current_user = wp_get_current_user();
  
  // Updating Billing info
  
  if($current_user->user_email != $current_user->billing_email)
    update_user_meta($user_id,'billing_email',$current_user->user_email);
}

我使用的是过时的代码吗?

解决方法

有一些错误。请尝试以下操作:

add_filter( 'woocommerce_thankyou','thankyou_update_wordpress_user_email' );
function thankyou_update_wordpress_user_email( $order_id ) {
    $order         = wc_get_order( $order_id );
    $user          = $order->get_user();
    $billing_email = $order->get_billing_email();
  
    // Updating user account email
    if( is_a($user,'WP_User' ) && $user->user_email != $billing_email ) {
        $user_data = wp_update_user( array( 'ID' => $user->ID,'user_email' => $billing_email ) );
    }
}

代码位于活动子主题(或活动主题)的functions.php 文件中。它应该可以工作。

注意:更改用户电子邮件时,WordPress 会向新用户电子邮件发送一封电子邮件。

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...