$Payments = new Payments();
$Payments->get_payment_providers( $location, $for_display, $remove_shells ): array;
$location(строка) (обязательный)
The location for which the providers are being determined. This is an ISO 3166-1 alpha-2 country code.
$for_display(true|false)
Whether the payment providers list is intended for display purposes or it is meant to be used for internal business logic. Primarily, this means that when it is not for display, we will use the raw payment gateways list (all the registered gateways), not just the ones that should be shown to the user on the Payments Settings page. This complication is for backward compatibility as it relates to legacy settings hooks being fired or not. По умолчанию: true
$remove_shells(true|false)
Whether to remove the payment providers shells from the list. If the $for_display is true, this will be ignored since the display logic will handle the shells itself. По умолчанию: false
Код Payments::get_payment_providers() Payments::get payment providers WC 10.3.4
public function get_payment_providers( string $location, bool $for_display = true, bool $remove_shells = false ): array {
$payment_gateways = $this->providers->get_payment_gateways( $for_display );
if ( ! $for_display && $remove_shells ) {
$payment_gateways = $this->providers->remove_shell_payment_gateways( $payment_gateways );
}
$providers_order_map = $this->providers->get_order_map();
$payment_providers = array();
// Only include suggestions if the requesting user can install plugins.
$suggestions = array();
if ( current_user_can( 'install_plugins' ) ) {
$suggestions = $this->providers->get_extension_suggestions( $location, self::SUGGESTIONS_CONTEXT );
}
// If we have preferred suggestions, add them to the providers list.
if ( ! empty( $suggestions['preferred'] ) ) {
// Sort them by priority, ASC.
usort(
$suggestions['preferred'],
function ( $a, $b ) {
return $a['_priority'] <=> $b['_priority'];
}
);
// By default, we will add the preferred suggestions at the top of the list.
$last_preferred_order = -1;
// If WooPayments is already present, we add the preferred suggestions after it.
// This way we ensure default installed WooPayments is at the same place as its suggestion would be.
if ( isset( $providers_order_map[ WooPaymentsService::GATEWAY_ID ] ) ) {
$last_preferred_order = $providers_order_map[ WooPaymentsService::GATEWAY_ID ];
}
foreach ( $suggestions['preferred'] as $suggestion ) {
$suggestion_order_map_id = $this->providers->get_suggestion_order_map_id( $suggestion['id'] );
// Determine the suggestion's order value.
// If we don't have an order for it, add it to the top but keep the relative order:
// PSP first, APM after PSP, offline PSP after PSP and APM.
if ( ! isset( $providers_order_map[ $suggestion_order_map_id ] ) ) {
$providers_order_map = Utils::order_map_add_at_order( $providers_order_map, $suggestion_order_map_id, $last_preferred_order + 1 );
}
// Save the preferred provider's order to know where we should be inserting next.
// But only if the last preferred order is less than the current one.
if ( $last_preferred_order < $providers_order_map[ $suggestion_order_map_id ] ) {
$last_preferred_order = $providers_order_map[ $suggestion_order_map_id ];
}
// Change suggestion details to align it with a regular payment gateway.
$suggestion['_suggestion_id'] = $suggestion['id'];
$suggestion['id'] = $suggestion_order_map_id;
$suggestion['_type'] = PaymentsProviders::TYPE_SUGGESTION;
$suggestion['_order'] = $providers_order_map[ $suggestion_order_map_id ];
unset( $suggestion['_priority'] );
$payment_providers[] = $suggestion;
}
}
foreach ( $payment_gateways as $payment_gateway ) {
// Determine the gateway's order value.
// If we don't have an order for it, add it to the end.
if ( ! isset( $providers_order_map[ $payment_gateway->id ] ) ) {
$providers_order_map = Utils::order_map_add_at_order( $providers_order_map, $payment_gateway->id, count( $payment_providers ) );
}
$payment_providers[] = $this->providers->get_payment_gateway_details(
$payment_gateway,
$providers_order_map[ $payment_gateway->id ],
$location
);
}
// Add offline payment methods group entry if we have offline payment methods.
if ( in_array( PaymentsProviders::TYPE_OFFLINE_PM, array_column( $payment_providers, '_type' ), true ) ) {
// Determine the item's order value.
// If we don't have an order for it, add it to the end.
if ( ! isset( $providers_order_map[ PaymentsProviders::OFFLINE_METHODS_ORDERING_GROUP ] ) ) {
$providers_order_map = Utils::order_map_add_at_order( $providers_order_map, PaymentsProviders::OFFLINE_METHODS_ORDERING_GROUP, count( $payment_providers ) );
}
$payment_providers[] = array(
'id' => PaymentsProviders::OFFLINE_METHODS_ORDERING_GROUP,
'_type' => PaymentsProviders::TYPE_OFFLINE_PMS_GROUP,
'_order' => $providers_order_map[ PaymentsProviders::OFFLINE_METHODS_ORDERING_GROUP ],
'title' => esc_html__( 'Take offline payments', 'woocommerce' ),
'description' => esc_html__( 'Accept payments offline using multiple different methods. These can also be used to test purchases.', 'woocommerce' ),
'icon' => plugins_url( 'assets/images/payment_methods/cod.svg', WC_PLUGIN_FILE ),
// The offline PMs (and their group) are obviously from WooCommerce, and WC is always active.
'plugin' => array(
'_type' => 'wporg',
'slug' => 'woocommerce',
'file' => '', // This pseudo-provider should have no use for the plugin file.
'status' => PaymentsProviders::EXTENSION_ACTIVE,
),
'management' => array(
'_links' => array(
'settings' => array(
'href' => Utils::wc_payments_settings_url( '/' . ( class_exists( '\WC_Settings_Payment_Gateways' ) ? \WC_Settings_Payment_Gateways::OFFLINE_SECTION_NAME : 'offline' ) ),
),
),
),
);
}
// Determine the final, standardized providers order map.
$providers_order_map = $this->providers->enhance_order_map( $providers_order_map );
// Enforce the order map on all providers, just in case.
foreach ( $payment_providers as $key => $provider ) {
$payment_providers[ $key ]['_order'] = $providers_order_map[ $provider['id'] ];
}
// NOTE: For now, save it back to the DB. This is temporary until we have a better way to handle this!
$this->providers->save_order_map( $providers_order_map );
// Sort the payment providers by order, ASC.
usort(
$payment_providers,
function ( $a, $b ) {
return $a['_order'] <=> $b['_order'];
}
);
// Only process payment provider states if we are displaying the providers.
// This is to ensure we don't introduce any performance issues outside the Payments settings page.
if ( $for_display ) {
$this->process_payment_provider_states( $payment_providers );
}
return $payment_providers;
}