Hi @fccoc
Are you migrating from another system or plugin? I would recommend using the action woocommerce_scheduled_subscription_payment provided by WooCommerce Subscriptions. You can use that action to update the subscription’s payment method right before the renewal payment is processed.
Here is an Example (this is not tested code, it’s provided as a guide):
add_action('woocommerce_scheduled_subscription_payment', function($subscription_id){
if ( ! is_object( $subscription_id ) ) {
$subscription = wcs_get_subscription( $subscription_id );
} else {
$subscription = $subscription_id;
}
if($subscription && !$subscription->is_manual()){
if(in_array($subscription->get_payment_method(), ['stripe_cc', 'stripe_applepay'])){
$default_payment_method = $subscription->get_meta('_payment_method_token');
if(!$default_payment_method){
$customer = WC_Stripe_Gateway::load()->customers->retrieve($subscription->get_meta('_wc_stripe_customer'));
if(!is_wp_error($customer)){
$default_payment_method = $customer->invoice_settings->default_payment_method;
if($default_payment_method){
$subscription->update_meta('_payment_method_token');
$subscription->save();
}
}
}
}
}
}, 5);
This code checks to see if the subscription has a saved payment method or not. If the payment method token is empty, the plugin fetches the Stripe customer object and assigns their default payment method to the subscription.
Kind Regards
Thread Starter
fccoc
(@fccoc)
Thanks for that – it should do what I need.. I’m migrating from a third party system outside WordPress.