WC_Payment_Tokens::set_users_default()public staticWC 2.6.0

Loops through all of a users payment tokens and sets is_default to false for all but a specific token.

Method of the class: WC_Payment_Tokens{}

Return

null. Nothing (null).

Usage

$result = WC_Payment_Tokens::set_users_default( $user_id, $token_id );
$user_id(int) (required)
User to set a default for.
$token_id(int) (required)
The ID of the token that should be default.

Changelog

Since 2.6.0 Introduced.

WC_Payment_Tokens::set_users_default() code WC 9.4.2

public static function set_users_default( $user_id, $token_id ) {
	$data_store   = WC_Data_Store::load( 'payment-token' );
	$users_tokens = self::get_customer_tokens( $user_id );
	foreach ( $users_tokens as $token ) {
		if ( $token_id === $token->get_id() ) {
			$data_store->set_default_status( $token->get_id(), true );
			do_action( 'woocommerce_payment_token_set_default', $token_id, $token );
		} else {
			$data_store->set_default_status( $token->get_id(), false );
		}
	}
}