【问题标题】:Update WooCommerce Subscriptions billing email address when user updates their wordpress user email address当用户更新其 wordpress 用户电子邮件地址时,更新 WooCommerce 订阅账单电子邮件地址
【发布时间】:2022-01-19 23:34:42
【问题描述】:

当用户更新其帐户详细信息页面(位于 /my-account/edit-account )上的 wordpress 电子邮件地址时,我如何自动更新他们的 WooCommerce 订阅账单电子邮件地址?

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

更新:我终于想通了...请参阅下面的答案

【问题讨论】:

    标签: woocommerce woocommerce-subscriptions


    【解决方案1】:

    我终于想通了;测试和工作。见内的cmets:

    // 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 );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-02-23
      • 2023-01-27
      • 2011-09-09
      • 1970-01-01
      • 2017-12-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多