WC_AJAX::toggle_gateway_enabled
Toggle payment gateway on or off via AJAX.
Method of the class: WC_AJAX{}
Hooks from the method
Returns
null. Nothing (null).
Usage
$result = WC_AJAX::toggle_gateway_enabled();
Changelog
| Since 3.4.0 | Introduced. |
WC_AJAX::toggle_gateway_enabled() WC AJAX::toggle gateway enabled code WC 10.4.3
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();
}