Automattic\WooCommerce\StoreApi\Utilities

PaymentUtils::get_default_payment_methodpublic staticWC 1.0

Returns the default payment method for a customer.

Method of the class: PaymentUtils{}

No Hooks.

Returns

String.

Usage

$result = PaymentUtils::get_default_payment_method();

PaymentUtils::get_default_payment_method() code WC 9.8.5

public static function get_default_payment_method() {
	$saved_payment_methods = self::get_saved_payment_methods();
	// A saved payment method exists, set as default.
	if ( $saved_payment_methods && ! empty( $saved_payment_methods['default'] ) ) {
		return $saved_payment_methods['default']['method']['gateway'] ?? '';
	}

	$chosen_payment_method = WC()->session->get( 'chosen_payment_method' );

	// If payment method is already stored in session, use it.
	if ( $chosen_payment_method ) {
		return $chosen_payment_method;
	}

	// If no saved payment method exists, use the first enabled payment method.
	$enabled_payment_gateways = self::get_enabled_payment_gateways();
	$first_key                = array_key_first( $enabled_payment_gateways );
	$first_payment_method     = $enabled_payment_gateways[ $first_key ];
	return $first_payment_method->id ?? '';
}