Created
April 25, 2025 10:46
-
-
Save xlplugins/873bcbd02710a5b40c67499ff2d7f3b6 to your computer and use it in GitHub Desktop.
Update customer in stripe when email modified in WP
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Update customer in stripe when email modified in WP | |
**/ | |
add_action( 'profile_update', 'sync_stripe_email_with_woocommerce', 10, 2 ); | |
function sync_stripe_email_with_woocommerce( $user_id, $old_user_data ) { | |
if(!class_exists('FKWCS_Gateway_Stripe')) { | |
return; | |
} | |
// Check if the email address has been changed | |
$user = get_user_by( 'id', $user_id ); | |
$new_email = $user->user_email; | |
$old_email = $old_user_data->user_email; | |
if ( $new_email !== $old_email ) { | |
// Fetch the Stripe customer ID from the user's meta data | |
$customer = WC()->payment_gateways()->payment_gateways()['fkwcs_stripe']->filter_customer_id( get_user_option( '_fkwcs_customer_id', $user_id ) ); | |
if ( empty( $customer ) ) { | |
$compatibility_keys = \FKWCS\Gateway\Stripe\Helper::get_compatibility_keys( '_fkwcs_customer_id' ); | |
if ( ! empty( $compatibility_keys ) ) { | |
foreach ( $compatibility_keys as $key ) { | |
$customer = WC()->payment_gateways()->payment_gateways()['fkwcs_stripe']->filter_customer_id( get_user_option( $key, $user_id ) ); | |
if ( ! empty( $customer ) ) { | |
break; | |
} | |
} | |
} | |
if ( empty( $customer ) ) { | |
return []; | |
} | |
} | |
if ( ! empty( $customer ) ) { | |
$client = WC()->payment_gateways()->payment_gateways()['fkwcs_stripe']->get_client(); | |
try { | |
$client->customers( 'update', [ $customer, [ 'email' => $new_email ] ] ); | |
} catch ( Exception $e ) { | |
// Handle Stripe API error | |
error_log( 'Error updating Stripe email: ' . $e->getMessage() ); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment