while adding a new card , if user click particular card to make it "as default" all subscriptions from other cards should be moved to selected card. All of my logic is working fine.
$new_default_card : This variable has new card data (object)
$subscription : This variable has subscription data (object)
I want to update the payment method from other cards to new one.
set_payment_method : This method isn't updating anything rather it remove subscriptions from other cards.
// Get the new default payment method details
$new_default_card = WC_Payment_Tokens::get($token_id);
$payment_method = WC_Payment_Tokens::get_customer_tokens(get_current_user_id());
$user_id = get_current_user_id(); // Get the current user ID
// Query subscriptions using WC_Subscriptions function
$args = array(
'subscriptions_per_page' => -1, // Get all subscriptions
'status' => array( 'active', 'on-hold', 'pending', 'cancelled', 'switched', 'expired' ), //
'customer_id' => $user_id, // Filter by customer ID
);
$subscription = wcs_get_subscriptions( $args );
$subscription->set_payment_method($payment_method->get_data()['token']);
$subscription->save();
As
wcs_get_subscriptions( $args );is not a WC_Subscription object but an array of WC_Subscription objects, so the methodset_payment_method()can't be applied to a non WC_Subscription object and should throw an error…From your code, try to replace:
with the following:
It should better work…