Change WooCommerce checkout error text on Checkout Page

Viewed 5112

I want to add my own custom payment error text (we use Account Funds and want to add an extra prompt)

How can I change, the following which shows when there is not enough funds, from:

Sorry, it seems that there are no available payment methods for your state. Please contact us if you require assistance or wish to make alternate arrangements.

to an error message with a link like below:

You do not have sufficent funds to process this order, please <a href="/my-account">top up</a> or upgrade. Thank you.

The text seems to be stored in templates/checkout/payment.php

https://github.com/woocommerce/woocommerce/blob/ef05bfccfc01bb2c621ef1293e61f7c57950670f/templates/checkout/payment.php

How can I change this without it getting wiped out by a Woocommerce version upgrade?

3 Answers

In WordPress, filters are functions that can be hooked to an event (called hooks). During the execution when the event is triggered the filter is applied to the data output generated by the event hook. It is important to remember that filters perform their actions on data they receive and then return that data before it is displayed in the browser.

In the file you attached (payment.php) you have

apply_filters( 'woocommerce_no_available_payment_methods_message' ....

So you can use the filter "woocommerce_no_available_payment_methods_message" to change the text

Create a custom function and add it to functions file or a small plugin.

First we hook our own function with woocommerce event

add_filter( 'woocommerce_no_available_payment_methods_message', 'your_custom_function_name_here' );

Now we define what our function would do.

function your_custom_function_name_here( $content ) {
    //your changes here
    $content = "bla bla";
    // Returns the content.
    return $content;
}

You just have to add a filter and apply your change in your child-theme's functions.php file:

add_filter( 'woocommerce_no_available_payment_methods_message', function( $no_gateways_message ) {
    return 'You do not have sufficent funds to process this order, please <a href="' . home_url( '/my-account/' ) . '">top up</a> or upgrade. Thank you.';
});

Let me know if that worked.

add_filter( 'woocommerce_no_available_payment_methods_message', 'change_payment_message', 10, 2);
function change_payment_message( $value, $arg2 ) {
    $message = WC()->customer->get_billing_country()?'You do not have sufficent funds to process this order, please<a
  href="/my-account">top up</a> or upgrade. Thank you':'Please fill in your details above to see available payment methods.';
    return $message;
}
Related