首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当用户更新wordpress用户电子邮件地址时更新WooCommerce订阅计费电子邮件地址

当用户更新wordpress用户电子邮件地址时更新WooCommerce订阅计费电子邮件地址
EN

Stack Overflow用户
提问于 2021-12-16 22:39:25
回答 1查看 349关注 0票数 -2

当用户更新他们的wordpress电子邮件地址发现在他们的帐户详细信息页面(在/my-帐户/编辑帐户),我如何自动更新他们的WooCommerce订阅付费电子邮件地址?

请注意,我指的是WooCommerce 订阅的计费帐户电子邮件地址(以便将来的订阅续订电子邮件订单发生更改等),而不仅仅是他们的WooCommerce订单电子邮件地址,以防这一点。

更新:我终于想出了这个out...see的答案

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-01-10 21:10:30

我终于想出了答案:测试和工作。见以下评论:

代码语言:javascript
复制
// When user updates their wordpress profile email, change 1) WooCommerce billing account email and 
// 2) active WooCommerce Subscriptions billing email to all match

add_action( 'profile_update', function( $user_id, $old_user_data ){
    $old_user_email = $old_user_data->data->user_email;
    $user = get_userdata( $user_id );
    $new_user_email = $user->user_email;

    // check if old and new email are not the same
    if ( $new_user_email !== $old_user_email ) {
        // update woocommerce billing email address to match user's wordpress email address
        update_user_meta( $user_id, 'billing_email', $new_user_email );

        // now loop through the user's woocommerce subscriptions to find the active subscription(s)
        $users_subscriptions = wcs_get_users_subscriptions( $user_id );
        foreach ($users_subscriptions as $subscription){
            if ( $subscription->has_status(array('active')) ) {
                // update the billing email for the active subscription as this is where the email address will be taken for future renewal orders
                update_post_meta( $subscription->get_id(), '_billing_email', $new_user_email );

                // per WooCommerce support, it is not necessary to update the billing email for the parent order of the active subscription, but here is how it would be done if desired
                // $active_sub = wcs_get_subscription( $subscription->get_id() );
                // update_post_meta( $active_sub->get_parent_id(), '_billing_email', $new_user_email );
            }
        }
    }
}, 10, 2 );
票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/70386446

复制
相关文章

相似问题

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