Skip to content

Instantly share code, notes, and snippets.

@asheroto
Last active April 18, 2024 06:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save asheroto/1eadb36c8e27430a810a7a89d93f5524 to your computer and use it in GitHub Desktop.
Save asheroto/1eadb36c8e27430a810a7a89d93f5524 to your computer and use it in GitHub Desktop.
Disable WooCommerce payment method using warning/alert message instead of removing. Show the payment method, just disable it with a warning/alert message.

Disable WooCommerce Payment Method Using Message

image

Functionality

  • Inserts a custom message if the user selects that payment method
  • Actually disables WooCommerce from using a specific payment method (no user HTML workarounds)
  • Prevents the Place order button from being clicked for that payment method, allows other methods to proceed
  • Skips over the payment method automatically when the checkout page loads

Install

  1. Add the PHP code into your theme's functions.php file or by using a plugin like Theme Customisations.
  2. Change the DISABLE_GATEWAY_ID to the ID of whichever payment gateway you want to disable.
  3. Change the DISABLE_MESSAGE to whatever message you want to appear on the disabled payment gateway.
  4. Optionally use the CSS in style.css to adjust the margins of the alert box
/****************************************************************************************
* START - Disable a payment gateway and show a custom message in WooCommerce
****************************************************************************************/
/**
* Define constants at the top for easy configuration.
*/
define('DISABLE_GATEWAY_ID', 'plisio');
define('DISABLE_MESSAGE', '<div class="woocommerce-error">Cryptocurrency payments are temporarily unavailable while we improve the payment process.</div>');
add_filter('woocommerce_available_payment_gateways', 'disable_payment_gateway', 10, 1);
function disable_payment_gateway($available_gateways) {
if (isset($available_gateways[DISABLE_GATEWAY_ID])) {
// Disable the payment method but still display it
$available_gateways[DISABLE_GATEWAY_ID]->enabled = false;
// Add custom message to the description
$available_gateways[DISABLE_GATEWAY_ID]->description = DISABLE_MESSAGE . $available_gateways[DISABLE_GATEWAY_ID]->description;
}
return $available_gateways;
}
add_action('wp_footer', 'disable_checkout_button_for_payment_method');
function disable_checkout_button_for_payment_method() {
if (is_checkout()) {
?>
<script type="text/javascript">
jQuery(document).ready(function($) {
// Continuously check for changes in payment method selection
$('form.checkout').on('change', 'input[name="payment_method"]', function() {
// Check if the disabled payment method is selected
if ($('input[name="payment_method"]:checked').val() === '<?php echo DISABLE_GATEWAY_ID; ?>') {
// Disable the place order button
$('#place_order').prop('disabled', true).css('opacity', '0.5');
} else {
// Enable the place order button if other methods are selected
$('#place_order').prop('disabled', false).css('opacity', '1');
}
});
// Trigger the change event on page load in case the method is already selected
$('input[name="payment_method"]:checked').trigger('change');
});
</script>
<?php
}
}
/****************************************************************************************
* END - Disable a payment gateway and show a custom message in WooCommerce
****************************************************************************************/
.woocommerce-message, .woocommerce-info, .woocommerce-error {
margin-bottom: 1em;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment