Automattic\WooCommerce\StoreApi\Utilities

PaymentUtils::get_saved_payment_methodspublic staticWC 1.0

Returns enabled saved payment methods for a customer and the default method if there are multiple.

Method of the class: PaymentUtils{}

No Hooks.

Returns

Array.

Usage

$result = PaymentUtils::get_saved_payment_methods();

PaymentUtils::get_saved_payment_methods() code WC 9.9.4

public static function get_saved_payment_methods() {
	if ( ! is_user_logged_in() ) {
		return;
	}

	add_filter( 'woocommerce_payment_methods_list_item', [ self::class, 'include_token_id_with_payment_methods' ], 10, 2 );

	$enabled_payment_gateways = self::get_enabled_payment_gateways();
	$saved_payment_methods    = wc_get_customer_saved_methods_list( get_current_user_id() );
	$payment_methods          = [
		'enabled' => [],
		'default' => null,
	];

	// Filter out payment methods that are not enabled.
	foreach ( $saved_payment_methods as $payment_method_group => $saved_payment_methods ) {
		$payment_methods['enabled'][ $payment_method_group ] = array_values(
			array_filter(
				$saved_payment_methods,
				function ( $saved_payment_method ) use ( $enabled_payment_gateways, &$payment_methods ) {
					if ( true === $saved_payment_method['is_default'] && null === $payment_methods['default'] ) {
						$payment_methods['default'] = $saved_payment_method;
					}
					return in_array( $saved_payment_method['method']['gateway'], array_keys( $enabled_payment_gateways ), true );
				}
			)
		);
	}

	remove_filter( 'woocommerce_payment_methods_list_item', [ self::class, 'include_token_id_with_payment_methods' ], 10, 2 );

	return $payment_methods;
}