WC_AJAX::toggle_gateway_enabled()public staticWC 3.4.0

Toggle payment gateway on or off via AJAX.

Метод класса: WC_AJAX{}

Возвращает

null. Ничего (null).

Использование

$result = WC_AJAX::toggle_gateway_enabled();

Список изменений

С версии 3.4.0 Введена.

Код WC_AJAX::toggle_gateway_enabled() WC 8.7.0

public static function toggle_gateway_enabled() {
	if ( current_user_can( 'manage_woocommerce' ) && check_ajax_referer( 'woocommerce-toggle-payment-gateway-enabled', 'security' ) && isset( $_POST['gateway_id'] ) ) {
		// Set current tab.
		$referer = wp_get_referer();
		if ( $referer ) {
			global $current_tab;
			parse_str( wp_parse_url( $referer, PHP_URL_QUERY ), $queries );
			$current_tab = $queries['tab'] ?? '';
		}

		// Load gateways.
		$payment_gateways = WC()->payment_gateways->payment_gateways();

		// Get posted gateway.
		$gateway_id = wc_clean( wp_unslash( $_POST['gateway_id'] ) );

		foreach ( $payment_gateways as $gateway ) {
			if ( ! in_array( $gateway_id, array( $gateway->id, sanitize_title( get_class( $gateway ) ) ), true ) ) {
				continue;
			}
			$enabled = $gateway->get_option( 'enabled', 'no' );
			$option  = array(
				'id' => $gateway->get_option_key(),
			);

			if ( ! wc_string_to_bool( $enabled ) ) {
				if ( $gateway->needs_setup() ) {
					wp_send_json_error( 'needs_setup' );
					wp_die();
				} else {
					do_action( 'woocommerce_update_option', $option );
					$gateway->update_option( 'enabled', 'yes' );
				}
			} else {
				do_action( 'woocommerce_update_option', $option );
				// Disable the gateway.
				$gateway->update_option( 'enabled', 'no' );
			}
			do_action( 'woocommerce_update_options' );
			wp_send_json_success( ! wc_string_to_bool( $enabled ) );
			wp_die();
		}
	}

	wp_send_json_error( 'invalid_gateway_id' );
	wp_die();
}