payment_gateways(); // Loop through each gateway foreach ( $gateways as $gateway_id => $gateway ) { // Get the option key for the gateway settings $option_key = "woocommerce_{$gateway_id}_settings"; // Set up an action to check if the gateway was disabled add_action( "update_option_{$option_key}", function( $old_value, $new_value ) use ( $gateway ) { // Check if the 'enabled' setting changed from 'yes' to 'no' $old_enabled = $old_value[ 'enabled' ] ?? 'no'; $new_enabled = $new_value[ 'enabled' ] ?? 'no'; // If it was disabled, send notification if ( $old_enabled === 'yes' && $new_enabled !== 'yes' ) { // Get the payment gateway's name $gateway_name = $gateway->get_method_title(); // Get the user who disabled the gateway $current_user = wp_get_current_user(); $user_email = $current_user->user_email; $user_name = $current_user->display_name; // Get the list of selected users $users_ids = get_option( 'inspry_toolkit_payments_test_mode_check_admin_notifications_list' ); // Loop through users to get emails $to = []; if ( ! empty( $users_ids ) ) { foreach ( $users_ids as $user_id ) { $user_info = get_userdata( $user_id ); if ( $user_info && ! empty( $user_info->user_email ) ) { $to[] = $user_info->user_email; } } } else { // Fallback to admin email if no users are selected $to[] = get_option( 'admin_email' ); } // Set up and send the email $subject = '⚠️ WooCommerce Gateway Disabled'; $body = "Attention:\n\nThe " . $gateway_name . " payment gateway has been disabled on your site (" . home_url() . ") by " . $user_name . " <" . $user_email . ">.\n"; $body .= "Please log in to ensure this is intentional."; wp_mail( $to, $subject, $body ); } }, 10, 2 ); } } // Only run in admin if WooCommerce is active if ( is_admin() && is_plugin_active( 'woocommerce/woocommerce.php' ) ) { add_action( 'init', 'inspry_toolkit_check_disabled_payment_gateways' ); }